import java.util.*;
import java.io.*;

public class Solution2 {

    private void solve() throws IOException {
        int n = in.nextInt();
        int m = in.nextInt();
        int ans = 0;
        int[] a = new int[n];
        int[] b = new int[m];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        for (int i = 0; i < m; i++) {
            b[i] = in.nextInt();
        }
        int sum1 = 0;
        int sum2 = 0;
        int i = 0;
        int j = 0;
        while (i < n || j < m) {
            if (sum1 < sum2) {
                sum1 += a[i];
                i++;
            }
            else if (sum1 > sum2) {
                sum2 += b[j];
                j++;
            }
            if (sum1 == sum2) {
                ans++;
                if (i < n) sum1 = a[i];
                if (j < m) sum2 = b[j];
                i++;
                j++;
            }
        }
        if (n == 1 && m == 1) {
            System.out.println(1);
        }
        else
            System.out.println(ans - 1);
    }

    private String filename = "";
    private PrintWriter out;
    MyScanner in;

    private void run() throws IOException {
        in = new MyScanner();
        out = new PrintWriter(System.out);
        solve();
        in.close();
        out.close();
    }

    private class MyScanner {
        private BufferedReader br;
        private StringTokenizer st;

        public MyScanner() throws IOException {
            this.br = new BufferedReader(new InputStreamReader(System.in));
        }

        public MyScanner(String fileTitle) throws IOException {
            this.br = new BufferedReader(new FileReader(fileTitle));
        }

        public String nextLine() throws IOException {
            String s = br.readLine();
            return s == null ? "-1" : s;
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                String s = br.readLine();
                if (s == null) {
                    return "-1";
                }
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        public Integer nextInt() throws IOException {
            return Integer.parseInt(this.next());
        }

        public Long nextLong() throws IOException {
            return Long.parseLong(this.next());
        }

        public Double nextDouble() throws IOException {
            return Double.parseDouble(this.next());
        }

        public void close() throws IOException {
            this.br.close();
        }
    }

    public static void main(String[] args) throws IOException {
        Locale.setDefault(Locale.US);
        new Solution2().run();
    }
}


import java.util.*;
import java.io.*;

public class Solution2 {

    private void solve() throws IOException {
        int n = in.nextInt();
        int m = in.nextInt();
        int ans = 0;
        int[] a = new int[n];
        int[] b = new int[m];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        for (int i = 0; i < m; i++) {
            b[i] = in.nextInt();
        }
        int sum1 = a[0];
        int sum2 = b[0];
        int i = 1;
        int j = 1;
        while (i < n || j < m) {
            if (sum1 < sum2) {
                 sum1 += a[i];
                 i++;
            }
            else if (sum1 > sum2) {
                sum2 += b[j];
                j++;
            }
            if (sum1 == sum2) {
                ans++;
                if (i < n) sum1 = a[i];
                if (j < m) sum2 = b[j];
                i++;
                j++;
            }
        }
        System.out.println(ans);
    }

    private String filename = "";
    private PrintWriter out;
    MyScanner in;

    private void run() throws IOException {
        in = new MyScanner();
        out = new PrintWriter(System.out);
        solve();
        in.close();
        out.close();
    }

    private class MyScanner {
        private BufferedReader br;
        private StringTokenizer st;

        public MyScanner() throws IOException {
            this.br = new BufferedReader(new InputStreamReader(System.in));
        }

        public MyScanner(String fileTitle) throws IOException {
            this.br = new BufferedReader(new FileReader(fileTitle));
        }

        public String nextLine() throws IOException {
            String s = br.readLine();
            return s == null ? "-1" : s;
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                String s = br.readLine();
                if (s == null) {
                    return "-1";
                }
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        public Integer nextInt() throws IOException {
            return Integer.parseInt(this.next());
        }

        public Long nextLong() throws IOException {
            return Long.parseLong(this.next());
        }

        public Double nextDouble() throws IOException {
            return Double.parseDouble(this.next());
        }

        public void close() throws IOException {
            this.br.close();
        }
    }

    public static void main(String[] args) throws IOException {
        Locale.setDefault(Locale.US);
        new Solution2().run();
    }
}


import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int x[] = new int[n];
        int y[] = new int[m];
        int ans=0;
        for(int i=0;i<n;i++){
            x[i] = sc.nextInt();
        }
        for(int i=0;i<m;i++){
            y[i] = sc.nextInt();
        }
        int nopx=0,nopy=0, sumx=0, sumy=0;
        while(nopx!=n || nopy!=m){
            if(sumx==0 && sumy==0){
                sumx=x[nopx++];
                sumy = y[nopy++];
            }
            if(sumx<sumy){
                sumx+=x[nopx++];
            }
            if(sumx>sumy){
                sumy+=y[nopy++];
            }
            if(sumx!=0 && sumy!=0 && sumx==sumy){
                ans++;

                sumx=0;
                sumy=0;
            }
            /*System.out.println("sumx: "+ sumx);
            System.out.println("sumy: "+ sumy);
            System.out.println("nopx: "+ nopx);
            System.out.println("nopy: "+ nopy);*/

        }
        System.out.println(ans);
    }
}


import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int x[] = new int[n];
        int y[] = new int[m];
        int ans=0;
        for(int i=0;i<n;i++){
            x[i] = sc.nextInt();
        }
        for(int i=0;i<m;i++){
            y[i] = sc.nextInt();
        }
        int nopx=0,nopy=0, sumx=0, sumy=0;
        while(nopx!=n || nopy!=m){
            if(sumx==0 && sumy==0){
                sumx=x[nopx++];
                sumy = y[nopy++];
            }
            if(sumx<sumy){
                sumx+=x[nopx++];
            }
            if(sumx>sumy){
                sumy+=y[nopy++];
            }
            if(sumx!=0 && sumy!=0 && sumx==sumy){
                ans++;

                sumx=0;
                sumy=0;
            }
            System.out.println("sumx: "+ sumx);
            System.out.println("sumy: "+ sumy);
            System.out.println("nopx: "+ nopx);
            System.out.println("nopy: "+ nopy);

        }
        System.out.println(ans);
    }
}


import java.util.*;
public class HelloWorld{

     public static void main(String []args){
        //System.out.println("Hello World");
        long n,m,i,j,sum_1=0,sum_2=0,f=0,p=0;
        Scanner sc=new Scanner(System.in);
        n=sc.nextLong();
        m=sc.nextLong();
        long x[]=new long[(int)n];
        long y[]=new long[(int)m];
        for(i=0;i<n;i++)
        {
            x[(int)i]=sc.nextLong();
        }
        for(j=0;j<m;j++)
        {
            y[(int)j]=sc.nextLong();
        }
        j=0;
        for(i=0;i<n;i++)
        {
             //System.out.println(j);
            sum_1=x[(int)i]+sum_1;
            while(j<=m-1)
            {

                if(p==0)
                {
                    sum_2=y[(int)j]+sum_2;
                }
                if(sum_1==sum_2)
                {
                    ++f;
                    sum_1=0;
                    sum_2=0;
                    j++;
                    p=0;
                }
                if(sum_1<sum_2)
                {
                    p++;
                    break;
                }
                if(sum_1>sum_2)
                {
                    j++;
                    p=0;
                }

            }

        }
        System.out.println(f);
        //System.out.println("Hello World");
     }
}


import java.util.Scanner;

public class Main {
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while (scanner.hasNext()) {
			int n = scanner.nextInt();
			int m = scanner.nextInt();
			int arr[] = new int[n];
			int brr[] = new int [m];
			for (int i = 0; i < arr.length; i++) {
				arr[i] = scanner.nextInt();
			}
			for (int i = 0; i < brr.length; i++) {
				brr[i] = scanner.nextInt();
			}
			int suma = 0;
			int sumb = 0;
			int flag = 0;
			int j = 0;
			for (int i = 0; i < arr.length; i++) {
				suma += arr[i];
				while (sumb < suma && j < brr.length) {
					sumb += brr[j];
					j++;
				}
				if (suma == sumb) {
					flag++;
					suma = 0;
					sumb = 0;
				}
			}
			System.out.println(flag);

		}
	}
}


/*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class d {

	 public static void main(String[] args)  {

		 Scanner s=new Scanner(System.in);
		 int n=s.nextInt();
		 int m=s.nextInt();
		 int[] arr=new int[n];
		 int[] brr=new int[m];
		 for(int i=0;i<n;i++) {
			 arr[i]=s.nextInt();
			 }
		 for(int i=0;i<m;i++) {
			 brr[i]=s.nextInt();
		 }

		 int o=0;int p=0;
		 long ans=0;
		 long sum1=arr[0];long sum2=brr[0];
		 while(o<n&&p<m) {
			 if(sum1==sum2) {
				 ans++;
				 o++; p++;
				 if(o!=n&&p!=m) {
				 sum1=arr[o];sum2=brr[p];
				 }
			 }else {
				 if(sum1<sum2) {
					o++;
					if(o!=n) {
						 sum1=sum1+arr[o];
						 }

				 }else {
					 p++;
					 if(p!=m) {
						 sum2=sum2+brr[p];
						 }

				 }
			 }

		 }

		 System.out.println(ans);

	 }
}


/*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class d {

	 public static void main(String[] args)  {

		 Scanner s=new Scanner(System.in);
		 int n=s.nextInt();
		 int m=s.nextInt();
		 int[] arr=new int[n];
		 int[] brr=new int[m];
		 for(int i=0;i<n;i++) {
			 arr[i]=s.nextInt();
			 }
		 for(int i=0;i<m;i++) {
			 brr[i]=s.nextInt();
		 }

		 int o=0;int p=0;
		 int ans=0;
		 int sum1=arr[0];int sum2=brr[0];
		 while(o<n&&p<m) {
			 if(sum1==sum2) {
				 ans++;
				 o++; p++;
				 if(o!=n&&p!=n) {
				 sum1=arr[o];sum2=brr[p];
				 }
			 }else {
				 if(sum1<sum2) {
					o++;
					if(o!=n) {
						 sum1=sum1+arr[o];
						 }

				 }else {
					 p++;
					 if(p!=m) {
						 sum2=sum2+brr[p];
						 }

				 }
			 }

		 }

		 System.out.println(ans);

	 }
}


/*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class d {

	 public static void main(String[] args)  {

		 Scanner s=new Scanner(System.in);
		 int n=s.nextInt();
		 int m=s.nextInt();
		 int[] arr=new int[n];
		 int[] brr=new int[m];
		 for(int i=0;i<n;i++) {
			 arr[i]=s.nextInt();
			 }
		 for(int i=0;i<m;i++) {
			 brr[i]=s.nextInt();
		 }

		 int o=0;int p=0;
		 long ans=0;
		 long sum1=arr[0];long sum2=brr[0];
		 while(o<n&&p<m) {
			 if(sum1==sum2) {
				 ans++;
				 o++; p++;
				 if(o!=n&&p!=n) {
				 sum1=arr[o];sum2=brr[p];
				 }
			 }else {
				 if(sum1<sum2) {
					o++;
					if(o!=n) {
						 sum1=sum1+arr[o];
						 }

				 }else {
					 p++;
					 if(p!=m) {
						 sum2=sum2+brr[p];
						 }

				 }
			 }

		 }

		 System.out.println(ans);

	 }
}


import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

public class B950 {

    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader(System.in);
        int N = in.readInt();
        int M = in.readInt();
        int[] A = in.readIntArray(N);
        int[] B = in.readIntArray(M);
        Set<Long> set = new HashSet<>();
        long sum = 0;
        for (int a : A) {
            sum += a;
            set.add(sum);
        }
        int answer = 0;
        sum = 0;
        for (int b : B) {
            sum += b;
            if (set.contains(sum)) {
                answer++;
            }
        }
        System.out.println(answer);
    }

    static final class InputReader {
        private final InputStream stream;
        private final byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private int read() throws IOException {
            if (curChar >= numChars) {
                curChar = 0;
                numChars = stream.read(buf);
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public final int readInt() throws IOException {
            return (int)readLong();
        }

        public final long readLong() throws IOException {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
                if (c == -1) throw new IOException();
            }
            boolean negative = false;
            if (c == '-') {
                negative = true;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return negative ? -res : res;
        }

        public final int[] readIntArray(int size) throws IOException {
            int[] array = new int[size];
            for (int i=0; i<size; i++) {
                array[i] = readInt();
            }
            return array;
        }

        public final long[] readLongArray(int size) throws IOException {
            long[] array = new long[size];
            for (int i=0; i<size; i++) {
                array[i] = readLong();
            }
            return array;
        }

        public final String readString() throws IOException {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.append((char)c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    }

}


import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;

public class q5 {

	public static void main(String[] args) throws IOException {

		Reader.init(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int n=Reader.nextInt(),m=Reader.nextInt();
		int[] arr1=new int[n],arr2=new int[m];
		for(int i=0;i<n;i++) arr1[i]=Reader.nextInt();
		for(int i=0;i<m;i++) arr2[i]=Reader.nextInt();
		int i1=0,i2=0;
		int ans=0;
		long s1=0;
		long s2=0;
		while(i1<n && i2<m) {
			if(s1+arr1[i1]==s2+arr2[i2]) {
				ans++;
				i1++;
				i2++;
				s1=0;s2=0;
			}
			else {
				if(s1+arr1[i1]<s2+arr2[i2]) {
					s1+=arr1[i1++];
				}
				else {
					s2+=arr2[i2++];
				}
			}
		}
		if(i1<n || i2<m) ans++;
		out.print(ans);

		out.flush();
	}
}

class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    /** call this method to initialize reader for InputStream */
    static void init() throws IOException {
    	 reader = new BufferedReader(
                 new FileReader("input.txt"));
    tokenizer = new StringTokenizer("");
    }
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
    /** get next word */
    static String nextLine() throws IOException{
    	return reader.readLine();
    }
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}


import java.util.*;

public class B {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int [] first = new int[n];
		int [] second = new int[m];

		for (int x = 0; x < n; x++)
			first[x]=sc.nextInt();
		for (int x = 0; x < m; x++)
			second[x] = sc.nextInt();

		int fi = 1;
		int si = 1;
		int fsum = first[0];
		int ssum = second[0];
		int count = 0;

		while (fi < n && si < m) {
			if (fsum == ssum && fsum != 0) {
				count++;
				fsum = first[fi];
				ssum = second[si];
			} else if (fsum > ssum) {
				ssum = ssum+second[si];
				si++;
			} else {
				fsum = fsum + first[si];
				fi++;
			}
		}

		System.out.println(count+1);
	}

}


import java.util.*;

public class B {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int [] first = new int[n];
		int [] second = new int[m];

		for (int x = 0; x < n; x++)
			first[x]=sc.nextInt();
		for (int x = 0; x < m; x++)
			second[x] = sc.nextInt();

		int fi = 1;
		int si = 1;
		int fsum = first[0];
		int ssum = second[0];
		int count = 0;

		while (fi < n && si < m) {
			if (fsum == ssum && fsum != 0) {
				count++;
				fsum = first[fi];
				ssum = second[si];
				fi++;
				si++;
			} else if (fsum > ssum) {
				ssum = ssum+second[si];
				si++;
			} else {
				fsum = fsum + first[fi];
				fi++;
			}
		}

		System.out.println(count+1);
	}

}


import java.io.*;
import java.util.StringTokenizer;

public class B {

    private void solution() throws IOException {
        int n = in.nextInt();
        int m = in.nextInt();
        int[] x = new int[n];
        for (int i = 0; i < n; i++) {
            x[i] = in.nextInt();
        }
        int[] y = new int[m];
        for (int i = 0; i < m; i++) {
            y[i] = in.nextInt();
        }
        long sumX = x[0];
        long sumY = y[0];
        int indX = 1, indY = 1, cnt = 0;
        while (indX <= n && indY <= m) {
            if (sumX > sumY && indY < m) {
                sumY += y[indY++];
            } else if (sumX < sumY && indX < n) {
                sumX += x[indX++];
            } else if (sumX == sumY) {
                cnt++;
                if (indX < n && indY < m) {
                    sumX = x[indX++];
                    sumY = y[indY++];
                } else {
                    break;
                }
            }
        }
        out.println(cnt);
    }

    private void run() throws IOException {
        in = new Scanner(System.in);
        out = new PrintWriter(System.out);
        solution();
        in.close();
        out.close();
    }

    private Scanner in;
    private PrintWriter out;

    public static void main(String[] args) throws IOException {
        new B().run();
    }

    private class Scanner {

        BufferedReader br;
        StringTokenizer st;

        private Scanner(InputStream is) {
            br = new BufferedReader(new InputStreamReader(is));
        }

        private String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
            return st.nextToken();
        }

        private Integer nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        private Long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        private Double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        private void close() throws IOException {
            st = null;
            br.close();
        }
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author 4kara
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt(), m = in.nextInt(), x[] = new int[n], y[] = new int[m], sum = 0, cnt = 0;
            for (int i = 0; i < n; i++) x[i] = in.nextInt();
            for (int i = 0; i < m; i++) y[i] = in.nextInt();
            for (int i = 0, j = 0; i < n || j < m; ) {
                if (sum > 0) {
                    sum -= x[i++];
                } else {
                    sum += y[j++];
                }
                if (sum == 0) {
                    cnt++;
                }
            }
            out.print(cnt);
        }

    }

    static class Scanner {
        private BufferedReader br;
        private StringTokenizer st;

        public Scanner(InputStream is) {
            br = new BufferedReader(new InputStreamReader(is));
        }

        public String next() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }

        public Integer nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        // write your code here
        Reader.init(System.in);
        int n = Reader.nextInt();
        int m = Reader.nextInt();
        int a[] = new int[n];
        int b[] = new int[m];

        for (int i = 0; i< n; i++) {
            a[i] = Reader.nextInt();
        }

        for (int i = 0; i< m; i++) {
            b[i] = Reader.nextInt();
        }

        int i =0; int j =0;
        int sumA = a[0];
        int sumB = b[0];
        int count = 0;

        while(i<n && j <m) {
            if(sumA < sumB && i < n) {
                sumA += a[++i];
            } else if (sumA > sumB && j  < m) {
                sumB += b[++j];
            } else if (sumA == sumB) {
                count++;
                ++i;
                ++j;
                if(i <= (n-1)) {
                    sumA = a[i];
                }
                if(j <= (m-1)) {
                    sumB = b[j];
                }
            }
        }

        System.out.print(count);

    }

}

class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    /** call this method to initialize reader for InputStream */

    static void init(InputStream input) {
        reader = new BufferedReader(new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");

    }

    static String nextToken() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {

                tokenizer = new StringTokenizer(reader.readLine());

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    static String next() throws IOException {

        while ( ! tokenizer.hasMoreTokens() ) {

            tokenizer = new StringTokenizer(

                    reader.readLine() );

        }
        return tokenizer.nextToken();

    }

    static int nextInt() throws IOException {

        return Integer.parseInt( next() );

    }

    static double nextDouble() throws IOException {

        return Double.parseDouble( next() );

    }

    static long nextLong() {

        return Long.parseLong(nextToken());
    }
}


import java.util.*;

public class CodeForces469_div2 {
	public static Scanner scn = new Scanner(System.in);

	public static void main(String[] args) {
		// Q1();
		Q2();
	}

	private static void Q2() {
		int n = scn.nextInt();
		int m = scn.nextInt();
		int[] m1 = new int[n];
		int[] m2 = new int[m];
		for (int i = 0; i < n; i++) {
			m1[i] = scn.nextInt();
		}
		for (int i = 0; i < m; i++) {
			m2[i] = scn.nextInt();
		}
		int i = 0, j = 0;
		int count = 0;
		long temp1 = 0;
		long temp2 = 0;
		while (i < n && j < m) {
				if (temp1 + m1[i] == m2[j]+temp2) {
					count++;
					i++;
					j++;
					temp1 = 0;
					temp2 = 0;
					continue;
				} else if (temp1 + m1[i] > m2[j]+temp2) {
					temp2 +=m2[j];
					j++;
					continue;
				}else if(temp1 + m1[i] < m2[j]+temp2) {
					temp1 +=m1[i];
					i++;
					continue;
				}
		}
		System.out.println(count);
	}

	private static void Q1() {
		int l = scn.nextInt();
		int r = scn.nextInt();
		int a = scn.nextInt();
		int max = 0;
		int cond = Math.abs(l - r);
		if (cond <= a) {
			max = 2 * ((a - cond) / 2) + 2 * Math.max(l, r);
		} else {
			max = 2 * (Math.min(l, r) + a);
		}
		System.out.println(max);
	}

}


import java.io.*;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Collections.*;
import static java.util.Arrays.*;

public class Main {
    /**
     * Solution
     */
    static class Solution {

        public void solve(Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            long[] x = new long[n];
            long[] y = new long[m];
            x[0] = in.nextInt();
            for (int i = 1; i < n; ++i) {
                x[i] += x[i - 1] + in.nextInt();
            }
            y[0] = in.nextInt();
            for (int i = 1; i < m; ++i) {
                y[i] += y[i - 1] + in.nextInt();
            }
            Arrays.sort(x);
            Arrays.sort(y);
            int ans = 0;
            for(int i = 0, k = 0; i != n && k != m;) {
                if(x[i] == y[k]) {
                    ans++;
                }
                if(x[i] <= y[k]) {
                    ++i;
                } else if(x[i] > y[k]) {
                    ++k;
                }
            }
            out.print(ans);
        }
    }

    /**
     * Online judge checker
     */
    public static void main(String[] args) {
        boolean onlineJudge = System.getProperty("ONLINE_JUDGE") != null;
        if (onlineJudge) {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            Scanner inputReader = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);
            Solution solution = new Solution();
            solution.solve(inputReader, printWriter);
            printWriter.close();
        } else {
            InputStream inputStream = null;
            try {
                inputStream = new FileInputStream("./src/com/company/in.txt");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            OutputStream outputStream = System.out;
            Scanner inputReader = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);
            Solution solution = new Solution();
            byte test = 1;
            while (true) {
                try {
                    if (!inputReader.bufferedReader.ready()) break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                printWriter.print("Test#" + test + ":\n");
                solution.solve(inputReader, printWriter);
                printWriter.print("\n\n");
                test++;
            }
            printWriter.close();
        }
    }

    /**
     * Fast input
     */
    private static class Scanner {
        private BufferedReader bufferedReader;
        private StringTokenizer stringTokenizer;

        private Scanner(InputStream inputStream) {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            stringTokenizer = null;
        }

        public String next() {
            while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
                try {
                    stringTokenizer = new StringTokenizer(bufferedReader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return stringTokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

    }
}


import java.io.*;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Collections.*;
import static java.util.Arrays.*;

public class Main {
    /**
     * Solution
     */
    static class Solution {

        public void solve(Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            long[] x = new long[n];
            long[] y = new long[m];
            x[0] = in.nextInt();
            for (int i = 1; i < n; ++i) {
                x[i] += x[i - 1] + in.nextInt();
            }
            y[0] = in.nextInt();
            for (int i = 1; i < m; ++i) {
                y[i] += y[i - 1] + in.nextInt();
            }
            Arrays.sort(x);
            Arrays.sort(y);
            int ans = 0;
            for(int i = 0, k = 0;;) {
                if(x[i] == y[k]) {
                    ans++;
                }
                if(x[i] <= y[k] && i < n) {
                    ++i;
                } else if(x[i] > y[k] && k < m) {
                    ++k;
                }
                if(i == n || k == m) break;
            }
            out.print(ans);
        }
    }

    /**
     * Online judge checker
     */
    public static void main(String[] args) {
        boolean onlineJudge = System.getProperty("ONLINE_JUDGE") != null;
        if (onlineJudge) {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            Scanner inputReader = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);
            Solution solution = new Solution();
            solution.solve(inputReader, printWriter);
            printWriter.close();
        } else {
            InputStream inputStream = null;
            try {
                inputStream = new FileInputStream("./src/com/company/in.txt");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            OutputStream outputStream = System.out;
            Scanner inputReader = new Scanner(inputStream);
            PrintWriter printWriter = new PrintWriter(outputStream);
            Solution solution = new Solution();
            byte test = 1;
            while (true) {
                try {
                    if (!inputReader.bufferedReader.ready()) break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                printWriter.print("Test#" + test + ":\n");
                solution.solve(inputReader, printWriter);
                printWriter.print("\n\n");
                test++;
            }
            printWriter.close();
        }
    }

    /**
     * Fast input
     */
    private static class Scanner {
        private BufferedReader bufferedReader;
        private StringTokenizer stringTokenizer;

        private Scanner(InputStream inputStream) {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            stringTokenizer = null;
        }

        public String next() {
            while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
                try {
                    stringTokenizer = new StringTokenizer(bufferedReader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return stringTokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

    }
}


import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Codeforces
{
	public static void main(String[] args) throws IOException
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int X[] = new int[n];
		int Y[] = new int[m];
		int i , j;
		for(i = 0; i < n; i++)
			X[i] = sc.nextInt();
		for(i = 0; i < m; i++)
			Y[i] = sc.nextInt();
		long sx = X[0] , sy = Y[0];
		int c = 0;
		for(i = 1 , j = 1; i < n || j < m;)
		{
			if(sx > sy)
			{
				sy += Y[j];
				j++;
			}
			else if(sx < sy)
			{
				sx += X[i];
				i++;
			}
			else
			{
				//System.out.println(sx);
				c++;
				sx = X[i];
				sy = Y[j];
				i++;
				j++;
			}
		}
		System.out.println(c+1);
    }
}


import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Codeforces
{
	public static void main(String[] args) throws IOException
	{
		Scanner sc = new Scanner(System.in);
		int l = sc.nextInt();
		int r = sc.nextInt();
		int a = sc.nextInt();
		int res = 2 * Math.min(l , r);
		if(a - Math.abs(l-r) >= 0)
		{
			res += 2 * Math.abs(l-r);
			a -= Math.abs(l-r);
		}
		else
		{
			res += 2*a;
			a = 0;
		}
		res += 2 * (a / 2);
		System.out.println(res);

    }
}


import java.util.Scanner;

public class s2 {
static int []a;
static int []b;
static int n,m,ans,max,temp1,temp2;
static void dfs(int j,int k){
if(j==a.length&&k==b.length){
	if(max<ans){
		max=ans;
	}
	return;}
else if(j==a.length&&k<b.length){
for(int i=k;i<b.length;i++)
	temp2+=b[i];
	if(temp1==temp2)
		ans++;
	dfs(a.length,b.length);
}
else if(j<a.length&&k==b.length){
for(int i=j;i<a.length;i++)
	temp1+=a[i];
if(temp1==temp2)
	ans++;
dfs(a.length,b.length);
}
else{
if(temp1==temp2){
	temp1=a[j];
	temp2=b[k];
	ans++;
	dfs(j+1,k+1);
}
else if(temp1<temp2){
temp1+=a[j];
dfs(j+1,k);}
else if(temp1>temp2){
temp2+=b[k];
dfs(j,k+1);}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n=in.nextInt();
m=in.nextInt();
a=new int[n];
b=new int[m];
for(int i=0;i<n;i++)
	a[i]=in.nextInt();
for(int i=0;i<m;i++)
	b[i]=in.nextInt();
temp1=a[0];
temp2=b[0];
max=-111;
dfs(1,1);
System.out.println(max);
}
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		int m = cin.nextInt();
		int[] a = new int[n];
		int[] b = new int [m];
		int count = 0, flag = 0;
		for(int i=0; i<n; i++)
			a[i] = cin.nextInt();
		for(int i=0; i<m; i++)
			b[i] = cin.nextInt();
		for(int i=0; i<n&&flag<m;) {
			if(a[i] == b[flag]) {
				count ++;
				i ++ ;
				flag ++;
			}else if(a[i] < b[flag]) {
				a[i+1] = a[i] + a[i+1];
				i ++;
			}else {
				b[flag+1] = b[flag+1] + b[flag];
				flag ++;
			}
		}
		System.out.println(count);
	}
}


import java.util.Scanner;

public class s2 {
static int []a;
static int []b;
static int n,m,ans,max,temp1,temp2;
static void dfs(int j,int k){
if(j==a.length&&k==b.length){
	if(max<ans){
		max=ans;
	}
	return;}
else if(j==a.length&&k<b.length){
for(int i=k;i<b.length;i++)
	temp2+=b[i];
	if(temp1==temp2)
		ans++;
	dfs(a.length,b.length);
}
else if(j<a.length&&k==b.length){
for(int i=j;i<a.length;i++)
	temp1+=a[i];
if(temp1==temp2)
	ans++;
dfs(a.length,b.length);
}
else{
if(temp1==temp2){
	temp1=a[j];
	temp2=b[k];
	ans++;
}
else{
temp1+=a[j];
dfs(j+1,k);
temp2+=b[k];
dfs(j,k+1);
temp1+=a[j];
temp2+=b[k];
dfs(j+1,k+1);
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n=in.nextInt();
m=in.nextInt();
a=new int[n];
b=new int[m];
for(int i=0;i<n;i++)
	a[i]=in.nextInt();
for(int i=0;i<m;i++)
	b[i]=in.nextInt();
temp1=a[0];
temp2=b[0];
max=-111;
dfs(1,1);
System.out.println(max);
}
}


package codefores469;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int f = 0;
        int cf = 0;
        int cs = 0;
        Scanner sc = new Scanner(System.in);
        List<Integer> listf = new ArrayList<>();
        List<Integer> lists = new ArrayList<>();
        int l1 = sc.nextInt();
        int l2 = sc.nextInt();
        while (l1 != 0) {
            listf.add(sc.nextInt());
            l1--;
        }
        while (l2 != 0) {
            lists.add(sc.nextInt());
            l2--;
        }
        int i = 0;
        int j = 0;
        if (listf.get(i) == lists.get(j)){
            f++;
            i++;
            j++;
        }
        cf += listf.get(i);
        cs += lists.get(j);
        while (i < listf.size() && j < lists.size()) {
            if (cs == cf) {
                f++;
                i++;
                j++;
                if (i != listf.size()) cf = listf.get(i);
                if (j != lists.size()) cs = lists.get(j);

            }
            else if (cs > cf) {
                i++;
                cf += listf.get(i);
            }
            else {
                j++;
                cs += lists.get(j);
            }
        }

        System.out.println(f);
    }
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int f = 0;
        int cf = 0;
        int cs = 0;
        Scanner sc = new Scanner(System.in);
        List<Integer> listf = new ArrayList<>();
        List<Integer> lists = new ArrayList<>();
        int l1 = sc.nextInt();
        int l2 = sc.nextInt();
        while (l1 != 0) {
            listf.add(sc.nextInt());
            l1--;
        }
        while (l2 != 0) {
            lists.add(sc.nextInt());
            l2--;
        }
        int i = 0;
        int j = 0;
        if (listf.get(i) == lists.get(j)){
            f++;
            i++;
            j++;
        }
        cf += listf.get(i);
        cs += lists.get(j);
        while (i < listf.size() && j < lists.size()) {
            if (cs == cf) {
                f++;
                i++;
                j++;
                if (i != listf.size()) cf = listf.get(i);
                if (j != lists.size()) cs = lists.get(j);

            }
            else if (cs > cf) {
                i++;
                cf += listf.get(i);
            }
            else {
                j++;
                cs += lists.get(j);
            }
        }

        System.out.println(f);
    }
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int f = 0;
        int cf = 0;
        int cs = 0;
        Scanner sc = new Scanner(System.in);
        List<Integer> listf = new ArrayList<>();
        List<Integer> lists = new ArrayList<>();
        int l1 = sc.nextInt();
        int l2 = sc.nextInt();
        while (l1 != 0) {
            listf.add(sc.nextInt());
            l1--;
        }
        while (l2 != 0) {
            lists.add(sc.nextInt());
            l2--;
        }
        int i = 0;
        int j = 0;
        if (listf.get(i) == lists.get(j)){
            f++;
            i++;
            j++;
        }
        cf += listf.get(i);
        cs += lists.get(j);
        while (i < listf.size() && j < lists.size()) {
            if (cs == cf) {
                f++;
                i++;
                j++;
                if (i != listf.size()) cf = listf.get(i);
                if (j != lists.size()) cs = lists.get(j);

            }
            else if (cs > cf) {
                i++;
                cf += listf.get(i);
            }
            else {
                j++;
                cs += lists.get(j);
            }
        }

        System.out.println(f);
    }


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int f = 0;
        int cf = 0;
        int cs = 0;
        Scanner sc = new Scanner(System.in);
        List<Integer> listf = new ArrayList<>();
        List<Integer> lists = new ArrayList<>();
        int l1 = sc.nextInt();
        int l2 = sc.nextInt();
        while (l1 != 0) {
            listf.add(sc.nextInt());
            l1--;
        }
        while (l2 != 0) {
            lists.add(sc.nextInt());
            l2--;
        }
        int i = 0;
        int j = 0;
        if (listf.get(i) == lists.get(j)){
            f++;
            i++;
            j++;
        }
        if (i >= listf.size() || j >= lists.size()) {
            System.out.println(f);
            return;
        }
        cf += listf.get(i);
        cs += lists.get(j);
        while (i < listf.size() && j < lists.size()) {
            if (cs == cf) {
                f++;
                i++;
                j++;
                if (i != listf.size()) cf = listf.get(i);
                if (j != lists.size()) cs = lists.get(j);

            }
            else if (cs > cf) {
                i++;
                cf += listf.get(i);
            }
            else {
                j++;
                cs += lists.get(j);
            }
        }

        System.out.println(f);
    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
         Scanner s=new Scanner(System.in);
         int n=s.nextInt();
         int m=s.nextInt();
         int []mes1=new int[n];
         int []mes2=new int[m];
         int sum1=0,p1=0,p2=0,sum2=0,count=0;
         for(int i=0;i<n;i++)
           {
           mes1[i]=s.nextInt();
                    }
            for(int i=0;i<m;i++)
           {
           mes2[i]=s.nextInt();
                     }
           sum2=mes2[0];
           sum1=mes1[0];
     while(p1<=n&&p2<=m)
           {

       if(p1==p2)
          {
      count++;
      p1++;
      p2++;
      if(p1 == n || p2 == m)
          break;
      sum1=mes1[p1];
      sum2=mes2[p2];

          }
        if(sum1<sum2)
       {
              sum1=sum1+mes1[p1];
      p1++;
        if(p1 == n)
            break;

       }

       if(sum2<sum1)
       {
       p2++;
        if(p2 == m)
            break;
       sum2+=mes2[p2];

       }
           }
           System.out.println(count);
    }

}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
         Scanner s=new Scanner(System.in);
         int n=s.nextInt();
         int m=s.nextInt();
         int []mes1=new int[n];
         int []mes2=new int[m];
         int sum1=0,p1=0,p2=0,sum2=0,count=0;
         for(int i=0;i<n;i++)
           {
           mes1[i]=s.nextInt();
                    }
            for(int i=0;i<m;i++)
           {
           mes2[i]=s.nextInt();
                     }
           sum2=mes2[0];
           sum1=mes1[0];
     while(p1<=n&&p2<=m)
           {

       if(sum1==sum2)
          {
      count++;
      p1++;
      p2++;
      if(p1 == n || p2 == m)
          break;
      sum1=mes1[p1];
      sum2=mes2[p2];

          }
       if(sum1<sum2)
        {
          p1++;
           if(p1 == n)
            break;
          sum1=sum1+mes1[p1];

       }

       if(sum2<sum1)
       {
       p2++;
      if(p2 == m)
            break;
       sum2+=mes2[p2];

       }
           }
           System.out.println(count);
    }

}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
         Scanner s=new Scanner(System.in);
         int n=s.nextInt();
         int m=s.nextInt();
         int []mes1=new int[n];
         int []mes2=new int[m];
         int sum1=0,p1=0,p2=0,sum2=0,count=0;
         for(int i=0;i<n;i++)
           {
           mes1[i]=s.nextInt();
                    }
            for(int i=0;i<m;i++)
           {
           mes2[i]=s.nextInt();
                     }
           sum2=mes2[0];
           sum1=mes1[0];
           while(p1<=n&&p2<=m)
           {

       if(p1==p2)
          {
      count++;
      p1++;
      p2++;
      if(p1 == n || p2 == m)
          break;
      sum1=mes1[p1];
      sum2=mes2[p2];

          }
        if(sum1<sum2)
       {
      p1++;
        if(p1 == n)
            break;
       sum1=sum1+mes1[p1];

       }

       if(sum2<sum1)
       {
       p2++;
        if(p2 == m)
            break;
       sum2+=mes2[p2];

       }
           }
           System.out.println(count);
    }

}


#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

string toBinary(int a) {
	if (a == 0) return "";
	string base;
	if (a%2 == 0) base = "0";
	else base = "1";
	string prefix = toBinary(a/2);
	return prefix + base;
}

int INF = 100000000;
int best = INF;
vector<string> suffixes;
vector<string> prefixes;

int f(string comp){
	int dist = 0;
	bool tooBig = true;
	//cout << "comp is :" << comp << endl;
	for (string s : suffixes)
		if (s.size() >= comp.size()) tooBig = false;
	if (tooBig) return INF;

	for (string s: suffixes) {
		int i = 0;
		int j = 0;
		while (i < comp.size() && j < s.size()) {
			if (comp[i] != s[j]) break;
			i++; j++;
		}
		for (int k = j; k < comp.size(); k++) {
			if (comp[k] == '1') return INF;
		}
		dist += (s.size() - j);
		dist += (comp.size() - i);
		//cout << s << ": " << (s.size() - j)+(comp.size() - i) << endl;

	}
	//cout << comp << ": " << dist << endl;
	f (comp + '0');
	//f(comp + '1');

	if (dist < best) best = dist;
}

int main(){
	int n;
	cin >> n;
	if (n == 1) {
		cout << 0 << endl;
		return 0;
	}
	vector<string> full;
	for (int i = 0; i < n; i++ ){
		int x;
		cin >> x;
		full.push_back(toBinary(x));
	}
	int startOfDiff = -1;
	bool matches = true;
	do {
		startOfDiff++;
		char c = full[0][startOfDiff];
		for (string s: full)
			if (s[startOfDiff] != c){
				matches = false;
			}

	} while (matches);
	for (int i = 0; i < n; i++) {
		suffixes.push_back(full[i].substr(startOfDiff));
	}
	sort(suffixes.begin(), suffixes.end());
	string s = suffixes[0];
	for (int i = 0; i < s.size()+1; i++) {
		string t = s.substr(0,i);
		//cout << "t: " << t << endl;
		f(t);
	}

	cout << best << endl;

}


#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

string toBinary(int a) {
	if (a == 0) return "";
	string base;
	if (a%2 == 0) base = "0";
	else base = "1";
	string prefix = toBinary(a/2);
	return prefix + base;
}

int INF = 100000000;
int best = INF;
vector<string> suffixes;
vector<string> prefixes;

int f(string comp){
	int dist = 0;
	bool tooBig = true;
	//cout << "comp is :" << comp << endl;
	for (string s : suffixes)
		if (s.size() > comp.size()) tooBig = false;
	if (tooBig) return INF;

	for (string s: suffixes) {
		int i = 0;
		int j = 0;
		while (i < comp.size() && j < s.size()) {
			if (comp[i] != s[j]) break;
			i++; j++;
		}
		for (int k = j; k < comp.size(); k++) {
			if (comp[k] == '1') return INF;
		}
		dist += (s.size() - j);
		dist += (comp.size() - i);
		//cout << s << ": " << (s.size() - j)+(comp.size() - i) << endl;

	}
	//cout << comp << ": " << dist << endl;
	f (comp + '0');
	//f(comp + '1');

	if (dist < best) best = dist;
}

int main(){
	int n;
	cin >> n;
	if (n == 1) {
		cout << 0 << endl;
		return 0;
	}
	vector<string> full;
	for (int i = 0; i < n; i++ ){
		int x;
		cin >> x;
		full.push_back(toBinary(x));
	}
	int startOfDiff = -1;
	bool matches = true;
	do {
		startOfDiff++;
		char c = full[0][startOfDiff];
		for (string s: full)
			if (s[startOfDiff] != c){
				matches = false;
			}

	} while (matches);
	for (int i = 0; i < n; i++) {
		suffixes.push_back(full[i].substr(startOfDiff));
	}
	sort(suffixes.begin(), suffixes.end());
	string s = suffixes[0];
	for (int i = 0; i < s.size(); i++) {
		string t = s.substr(0,i);
		f(t);
	}

	cout << best << endl;

}


#include <string>
#include <iostream>
#include <vector>

using namespace std;

string toBinary(int a) {
	if (a == 0) return "";
	string base;
	if (a%2 == 0) base = "0";
	else base = "1";
	string prefix = toBinary(a/2);
	return prefix + base;
}

int INF = 100000000;
int best = INF;
vector<string> suffixes;
vector<string> prefixes;

int f(string comp){
	int dist = 0;
	bool tooBig = true;
	//cout << "comp is :" << comp << endl;
	for (string s : suffixes)
		if (s.size() > comp.size()) tooBig = false;
	if (tooBig) return INF;

	for (string s: suffixes) {
		int i = 0;
		int j = 0;
		while (i < comp.size() && j < s.size()) {
			if (comp[i] != s[j]) break;
			i++; j++;
		}
		for (int k = j; k < comp.size(); k++) {
			if (comp[k] == '1') return INF;
		}
		dist += (s.size() - j);
		dist += (comp.size() - i);
		//cout << s << ": " << (s.size() - j)+(comp.size() - i) << endl;

	}
	//cout << comp << ": " << dist << endl;
	f (comp + '0');
	//f(comp + '1');

	if (dist < best) best = dist;
}

int main(){
	int n;
	cin >> n;
	if (n == 1) {
		cout << 0 << endl;
		return 0;
	}
	vector<string> full;
	for (int i = 0; i < n; i++ ){
		int x;
		cin >> x;
		full.push_back(toBinary(x));
	}
	int startOfDiff = -1;
	bool matches = true;
	do {
		startOfDiff++;
		char c = full[0][startOfDiff];
		for (string s: full)
			if (s[startOfDiff] != c){
				matches = false;
			}

	} while (matches);
	for (int i = 0; i < n; i++) {
		suffixes.push_back(full[i].substr(startOfDiff));
	}
	sort(suffixes.begin(), suffixes.end());
	string s = suffixes[0];
	for (int i = 0; i < suffixes.size(); i++) {
		string t = s.substr(0,i);
		f(t);
	}

	cout << best << endl;

}


#include <string>
#include <iostream>
#include <vector>

using namespace std;

string toBinary(int a) {
	if (a == 0) return "";
	string base;
	if (a%2 == 0) base = "0";
	else base = "1";
	string prefix = toBinary(a/2);
	return prefix + base;
}

int INF = 100000000;
int best = INF;
vector<string> suffixes;

int f(string comp){
	int dist = 0;
	bool tooBig = true;
	//cout << "comp is :" << comp << endl;
	for (string s : suffixes)
		if (s.size() > comp.size()) tooBig = false;
	if (tooBig) return INF;

	for (string s: suffixes) {
		int i = 0;
		int j = 0;
		while (i < comp.size() && j < s.size()) {
			if (comp[i] != s[j]) break;
			i++; j++;
		}
		for (int k = j; k < comp.size(); k++) {
			if (comp[k] == '1') return INF;
		}
		dist += (s.size() - j);
		dist += (comp.size() - i);
		//cout << s << ": " << (s.size() - j)+(comp.size() - i) << endl;

	}
	//cout << comp << ": " << dist << endl;
	f (comp + '0');
	f(comp + '1');

	if (dist < best) best = dist;
}

int main(){
	int n;
	cin >> n;
	if (n == 1) {
		cout << 0 << endl;
		return 0;
	}
	vector<string> full;
	for (int i = 0; i < n; i++ ){
		int x;
		cin >> x;
		full.push_back(toBinary(x));
	}
	int startOfDiff = -1;
	bool matches = true;
	do {
		startOfDiff++;
		char c = full[0][startOfDiff];
		for (string s: full)
			if (s[startOfDiff] != c){
				matches = false;
			}

	} while (matches);
	for (int i = 0; i < n; i++) {
		suffixes.push_back(full[i].substr(startOfDiff));
	}

	f("");
	cout << best << endl;

}


#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

string toBinary(int a) {
	if (a == 0) return "";
	string base;
	if (a%2 == 0) base = "0";
	else base = "1";
	string prefix = toBinary(a/2);
	return prefix + base;
}

int INF = 100000000;
int best = INF;
vector<string> suffixes;
vector<string> prefixes;

int f(string comp){
	int dist = 0;
	bool tooBig = true;
	//cout << "comp is :" << comp << endl;
	for (string s : suffixes)
		if (s.size() > comp.size()) tooBig = false;
	if (tooBig) return INF;

	for (string s: suffixes) {
		int i = 0;
		int j = 0;
		while (i < comp.size() && j < s.size()) {
			if (comp[i] != s[j]) break;
			i++; j++;
		}
		for (int k = j; k < comp.size(); k++) {
			if (comp[k] == '1') return INF;
		}
		dist += (s.size() - j);
		dist += (comp.size() - i);
		//cout << s << ": " << (s.size() - j)+(comp.size() - i) << endl;

	}
	//cout << comp << ": " << dist << endl;
	f (comp + '0');
	//f(comp + '1');

	if (dist < best) best = dist;
}

int main(){
	int n;
	cin >> n;
	if (n == 1) {
		cout << 0 << endl;
		return 0;
	}
	vector<string> full;
	for (int i = 0; i < n; i++ ){
		int x;
		cin >> x;
		full.push_back(toBinary(x));
	}
	int startOfDiff = -1;
	bool matches = true;
	do {
		startOfDiff++;
		char c = full[0][startOfDiff];
		for (string s: full)
			if (s[startOfDiff] != c){
				matches = false;
			}

	} while (matches);
	for (int i = 0; i < n; i++) {
		suffixes.push_back(full[i].substr(startOfDiff));
	}
	sort(suffixes.begin(), suffixes.end());
	string s = suffixes[0];
	for (int i = 0; i < suffixes.size(); i++) {
		string t = s.substr(0,i);
		f(t);
	}

	cout << best << endl;

}


#include <iostream>
#include <vector>
using namespace std;

string toBinary(int a) {
	if (a == 0) return "";
	string base = to_string(a%2);
	string prefix = toBinary(a/2);
	return prefix + base;
}

int INF = 100000000;
int best = INF;
vector<string> suffixes;

int f(string comp){
	int dist = 0;
	bool tooBig = true;
	//cout << "comp is :" << comp << endl;
	for (string s : suffixes)
		if (s.size() > comp.size()) tooBig = false;
	if (tooBig) return INF;

	for (string s: suffixes) {
		int i = 0;
		int j = 0;
		while (i < comp.size() && j < s.size()) {
			if (comp[i] != s[j]) break;
			i++; j++;
		}
		for (int k = j; k < comp.size(); k++) {
			if (comp[k] == '1') return INF;
		}
		dist += (s.size() - j);
		dist += (comp.size() - i);
		//cout << s << ": " << (s.size() - j)+(comp.size() - i) << endl;

	}
	//cout << comp << ": " << dist << endl;
	f (comp + '0');
	f(comp + '1');

	if (dist < best) best = dist;
}

int main(){
	int n;
	cin >> n;
	if (n == 1) {
		cout << 0 << endl;
		return 0;
	}
	vector<string> full;
	for (int i = 0; i < n; i++ ){
		int x;
		cin >> x;
		full.push_back(toBinary(x));
	}
	int startOfDiff = -1;
	bool matches = true;
	do {
		startOfDiff++;
		char c = full[0][startOfDiff];
		for (string s: full)
			if (s[startOfDiff] != c){
				matches = false;
			}

	} while (matches);
	for (int i = 0; i < n; i++) {
		suffixes.push_back(full[i].substr(startOfDiff));
	}

	f("");
	cout << best << endl;

}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

int n, a[N], dist[N], pos[N], ans[N];

void bfs(int x)
{
    memset(dist,0x3f3f3f,sizeof dist);
    dist[x] = 0;
    queue<int> q; q.push(x);
    while (q.size())
    {
        int u = q.front(); q.pop();
        pos[u]++;
        if (u/2>0 && dist[u/2] > dist[u] + 1)
        {
            dist[u/2] = dist[u] + 1;
            q.push(u/2);
        }
        if (u*2<=1e5 && dist[u*2] > dist[u] + 1)
        {
            dist[u*2] = dist[u] + 1;
            q.push(u*2);
        }
    }
    for (int i=1; i<=1e5; i++) ans[i] += dist[i];
}

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++) scanf("%d",a+i);
    for (int i=1; i<=n; i++) bfs(a[i]);
    int res = 1e9;
    for (int i=1; i<=1e5; i++) if (pos[i]>=n)
    {
        res = min(res, ans[i]);
        //printf("%d %d %d\n",i,pos[i],ans[i]);
    }
    printf("%d",res);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

int a[N], n, dist[2*N], pos[2*N];

int gcd(int a, int b)
{
    while (b)
    {
        a %= b; swap(a, b);
    }
    return a;
}

int lcm(int a, int b)
{
    return (a * b)/gcd(a, b);
}

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++) scanf("%d",a+i);
    for (int i=1; i<=n; i++)
    {
        int x = a[i];
        pos[x]++;
        int cnt = 0;
        while (x)
        {
            x >>= 1;
            ++cnt;
            dist[x] += cnt;
            pos[x]++;
        }
        x = a[i], cnt = 0;
        while (x<=1e5)
        {
            x <<= 1;
            ++cnt;
            dist[x] += cnt;
            pos[x]++;
        }
    }
    int l = a[1];
    for (int i=2; i<=n; i++) l = lcm(l, a[i]);
    int ok = 1;
    for (int i=1; i<=n; i++)
    {
        if (l%a[i]!=0)
        {
            ok = 0;
            break;
        }
    }
    int res = 1e9;
    if (ok)
    {
        int cnt = 0;
        for (int i=1; i<=n; i++) cnt += l/a[i];
        res = min(res, cnt);
    }
    for (int i=1; i<=1e5; i++) if (pos[i]>=n)
    {
        res = min(res, dist[i]);
        //printf("%d ",i);
    }
    printf("%d",res);
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

int n, a[N], pos[N], dist[N], ans[N], mx;

void bfs(int x)
{
    memset(dist,0x3f,sizeof dist);
    dist[x] = 0;
    queue<int> q; q.push(x);
    while (q.size())
    {
        int u = q.front(); q.pop();
        ans[u] += dist[u];
        pos[u]++;
        if (u/2>0 && dist[u/2] > dist[u] + 1)
        {
            dist[u/2] = dist[u] + 1;
            q.push(u/2);
        }
        if (u*2<=mx && dist[u*2] > dist[u] + 1)
        {
            dist[u*2] = dist[u] + 1;
            q.push(u*2);
        }
    }
}

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++)
    {
        scanf("%d",a+i);
        mx = max(mx, a[i]);
    }
    for (int i=1; i<=n; i++) bfs(a[i]);
    int res = 1e9;
    for (int i=1; i<=mx; i++) if (pos[i]>=n)
    {
        res = min(res, ans[i]);
    }
    printf("%d",res);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e6 + 5;

int64_t a[N], dist[2*N], pos[2*N];
int n;

int64_t gcd(int64_t a, int64_t b)
{
    while (b)
    {
        a %= b; swap(a, b);
    }
    return a;
}

int64_t lcm(int64_t a, int64_t b)
{
    return (a * b)/gcd(a, b);
}

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++) scanf("%I64d",a+i);
    sort(a+1,a+n+1);
    for (int i=1; i<=n; i++)
    {
        int64_t x = a[i];
        pos[x]++;
        int64_t cnt = 0;
        x >>= 1;
        while (x)
        {
            ++cnt;
            dist[x] += cnt;
            pos[x]++;
            x >>= 1;
        }
        x = a[i], cnt = 0;
        x <<= 1;
        while (x<=2e6)
        {
            ++cnt;
            dist[x] += cnt;
            pos[x]++;
            x <<= 1;
        }
    }
    int ok = 1;
    for (int i=1; i<=n; i++)
    {
        if (a[n]%a[i]!=0)
        {
            ok = 0;
            break;
        }
    }
    int64_t res = 1e18;
    if (ok)
    {
        int64_t cnt = 0;
        for (int i=1; i<=n; i++) cnt += a[n]/a[i];
        res = min(res, cnt);
    }
    for (int i=1; i<=2e6; i++) if (pos[i]>=n) res = min(res, dist[i]);
    printf("%I64d",res);
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

int n, a[N], dist[N], pos[N], ans[N], mx;

void bfs(int x)
{
    for (int i=1; i<=mx; i++) dist[i] = 1e9;
    dist[x] = 0;
    queue<int> q; q.push(x);
    while (q.size())
    {
        int u = q.front(); q.pop();
        ans[u] += dist[u];
        pos[u]++;
        if (u/2>0 && dist[u/2] > dist[u] + 1)
        {
            dist[u/2] = dist[u] + 1;
            q.push(u/2);
        }
        if (u*2<=mx && dist[u*2] > dist[u] + 1)
        {
            dist[u*2] = dist[u] + 1;
            q.push(u*2);
        }
    }
}

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++)
    {
        scanf("%d",a+i);
        mx = max(mx, a[i]);
    }
    for (int i=1; i<=n; i++) bfs(a[i]);
    int res = 1e9;
    for (int i=1; i<=mx; i++) if (pos[i]>=n)
    {
        res = min(res, ans[i]);
    }
    printf("%d",res);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

int n, a[N], dist[N], pos[N], ans[N];

void bfs(int x)
{
    memset(dist,0x3f3f3f,sizeof dist);
    dist[x] = 0;
    queue<int> q; q.push(x);
    while (q.size())
    {
        int u = q.front(); q.pop();
        ans[u] += dist[u];
        pos[u]++;
        if (u/2>0 && dist[u/2] > dist[u] + 1)
        {
            dist[u/2] = dist[u] + 1;
            q.push(u/2);
        }
        if (u*2<=1e5 && dist[u*2] > dist[u] + 1)
        {
            dist[u*2] = dist[u] + 1;
            q.push(u*2);
        }
    }
}

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++) scanf("%d",a+i);
    for (int i=1; i<=n; i++) bfs(a[i]);
    int res = 1e9;
    for (int i=1; i<=1e5; i++) if (pos[i]>=n)
    {
        res = min(res, ans[i]);
    }
    printf("%d",res);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5;

template <class T>

void read(T &x)
{
    char c; int sign = 1;
    while ((c=getchar())>'9'||c<'0') if (c=='-') sign = -1; x = c - 48;
    while ((c=getchar())>='0'&&c<='9') x = (x<<1) + (x<<3) + c - 48; x *= sign;
}

int n, res = 1e9, cnt[N], num[N];

int main()
{
    read(n);
    for (int i=1; i<=n; i++)
    {
        int x; read(x);
        for (int j=0, div = x; div>0; j++, div/=2)
        {
            for (int k=0, mul = div; mul<=1e5; k++, mul*=2) if (num[mul]==i-1)
            {
                num[mul]++;
                cnt[mul] += k + j;
                if (i==n) res = min(res, cnt[mul]);
            }
        }
    }
    cout << res;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

int64_t a[N], dist[2*N], pos[2*N];
int n;

int64_t gcd(int64_t a, int64_t b)
{
    while (b)
    {
        a %= b; swap(a, b);
    }
    return a;
}

int64_t lcm(int64_t a, int64_t b)
{
    return (a * b)/gcd(a, b);
}

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++) scanf("%I64d",a+i);
    for (int i=1; i<=n; i++)
    {
        int64_t x = a[i];
        pos[x]++;
        int64_t cnt = 0;
        while (x)
        {
            x >>= 1;
            ++cnt;
            dist[x] += cnt;
            pos[x]++;
        }
        x = a[i], cnt = 0;
        while (x<=1e5)
        {
            x <<= 1;
            ++cnt;
            dist[x] += cnt;
            pos[x]++;
        }
    }
    int64_t l = a[1];
    for (int i=2; i<=n; i++) l = lcm(l, a[i]);
    int ok = 1;
    for (int i=1; i<=n; i++)
    {
        if (l%a[i]!=0)
        {
            ok = 0;
            break;
        }
    }
    int64_t res = 1e18;
    if (ok)
    {
        int64_t cnt = 0;
        for (int i=1; i<=n; i++) cnt += l/a[i];
        res = min(res, cnt);
    }
    for (int i=1; i<=1e5; i++) if (pos[i]>=n)
    {
        res = min(res, dist[i]);
        //printf("%d ",i);
    }
    printf("%I64d",res);
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

int64_t a[N], dist[2*N], pos[2*N];
int n;

int64_t gcd(int64_t a, int64_t b)
{
    while (b)
    {
        a %= b; swap(a, b);
    }
    return a;
}

int64_t lcm(int64_t a, int64_t b)
{
    return (a * b)/gcd(a, b);
}

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++) scanf("%I64d",a+i);
    sort(a+1,a+n+1);
    for (int i=1; i<=n; i++)
    {
        int64_t x = a[i];
        pos[x]++;
        int64_t cnt = 0;
        x >>= 1;
        while (x)
        {
            ++cnt;
            dist[x] += cnt;
            pos[x]++;
        }
        x = a[i], cnt = 0;
        x <<= 1;
        while (x<=2e5)
        {
            ++cnt;
            dist[x] += cnt;
            pos[x]++;
        }
    }
    int ok = 1;
    for (int i=1; i<=n; i++)
    {
        if (a[n]%a[i]!=0)
        {
            ok = 0;
            break;
        }
    }
    int64_t res = 1e18;
    if (ok)
    {
        int64_t cnt = 0;
        for (int i=1; i<=n; i++) cnt += a[n]/a[i];
        res = min(res, cnt);
    }
    for (int i=1; i<=1e5; i++) if (pos[i]>=n) res = min(res, dist[i]);
    printf("%I64d",res);
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

int64_t a[N], dist[2*N], pos[2*N];
int n;

int64_t gcd(int64_t a, int64_t b)
{
    while (b)
    {
        a %= b; swap(a, b);
    }
    return a;
}

int64_t lcm(int64_t a, int64_t b)
{
    return (a * b)/gcd(a, b);
}

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++) scanf("%I64d",a+i);
    sort(a+1,a+n+1);
    for (int i=1; i<=n; i++)
    {
        int64_t x = a[i];
        pos[x]++;
        int64_t cnt = 0;
        while (x)
        {
            x >>= 1;
            ++cnt;
            dist[x] += cnt;
            pos[x]++;
        }
        x = a[i], cnt = 0;
        while (x<=1e5)
        {
            x <<= 1;
            ++cnt;
            dist[x] += cnt;
            pos[x]++;
        }
    }
    int64_t l = a[n];
    int ok = 1;
    for (int i=1; i<=n; i++)
    {
        if (l%a[i]!=0)
        {
            ok = 0;
            break;
        }
    }
    int64_t res = 1e18;
    if (ok)
    {
        int64_t cnt = 0;
        for (int i=1; i<=n; i++) cnt += l/a[i];
        res = min(res, cnt);
    }
    for (int i=1; i<=1e5; i++) if (pos[i]>=n)
    {
        res = min(res, dist[i]);
        //printf("%d ",i);
    }
    printf("%I64d",res);
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

int64_t a[N], dist[2*N], pos[2*N];
int n;

int64_t gcd(int64_t a, int64_t b)
{
    while (b)
    {
        a %= b; swap(a, b);
    }
    return a;
}

int64_t lcm(int64_t a, int64_t b)
{
    return (a * b)/gcd(a, b);
}

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++) scanf("%I64d",a+i);
    sort(a+1,a+n+1);
    for (int i=1; i<=n; i++)
    {
        int64_t x = a[i];
        pos[x]++;
        int64_t cnt = 0;
        x >>= 1;
        while (x)
        {
            ++cnt;
            dist[x] += cnt;
            pos[x]++;
            x >>= 1;
        }
        x = a[i], cnt = 0;
        x <<= 1;
        while (x<=2e5)
        {
            ++cnt;
            dist[x] += cnt;
            pos[x]++;
            x <<= 1;
        }
    }
    int ok = 1;
    for (int i=1; i<=n; i++)
    {
        if (a[n]%a[i]!=0)
        {
            ok = 0;
            break;
        }
    }
    int64_t res = 1e18;
    if (ok)
    {
        int64_t cnt = 0;
        for (int i=1; i<=n; i++) cnt += a[n]/a[i];
        res = min(res, cnt);
    }
    for (int i=1; i<=2e5; i++) if (pos[i]>=n) res = min(res, dist[i]);
    printf("%I64d",res);
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

int n, a[N], dist[N], pos[N], ans[N];

void bfs(int x)
{
    memset(dist,0x3f3f3f,sizeof dist);
    dist[x] = 0;
    queue<int> q; q.push(x);
    while (q.size())
    {
        int u = q.front(); q.pop();
        pos[u]++;
        if (u/2>0 && dist[u/2] > dist[u] + 1)
        {
            dist[u/2] = dist[u] + 1;
            ans[u/2] += dist[u/2];
            q.push(u/2);
        }
        if (u*2<=1e5 && dist[u*2] > dist[u] + 1)
        {
            dist[u*2] = dist[u] + 1;
            ans[u*2] += dist[u*2];
            q.push(u*2);
        }
    }
}

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++) scanf("%d",a+i);
    for (int i=1; i<=n; i++) bfs(a[i]);
    int res = 1e9;
    for (int i=1; i<=1e5; i++) if (pos[i]>=n)
    {
        res = min(res, ans[i]);
        //printf("%d %d %d\n",i,pos[i],ans[i]);
    }
    printf("%d",res);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;

int n, a[N], pos[N], ans[N], mx;

void bfs(int x)
{
    vector<int> dist(mx + 1, 1e9);
    dist[x] = 0;
    queue<int> q; q.push(x);
    while (q.size())
    {
        int u = q.front(); q.pop();
        ans[u] += dist[u];
        pos[u]++;
        if (u/2>0 && dist[u/2] > dist[u] + 1)
        {
            dist[u/2] = dist[u] + 1;
            q.push(u/2);
        }
        if (u*2<=mx && dist[u*2] > dist[u] + 1)
        {
            dist[u*2] = dist[u] + 1;
            q.push(u*2);
        }
    }
}

int main()
{
    scanf("%d",&n);
    for (int i=1; i<=n; i++)
    {
        scanf("%d",a+i);
        mx = max(mx, a[i]);
    }
    for (int i=1; i<=n; i++) bfs(a[i]);
    int res = 1e9;
    for (int i=1; i<=mx; i++) if (pos[i]>=n)
    {
        res = min(res, ans[i]);
    }
    printf("%d",res);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
ifstream fin("input.txt");
#define cin fin
typedef pair <ll,ll> p;
vector <ll> a(100500);
vector <ll> b(400500),c(400500);
ll n,m=0;
int main()
{

    ll i,temp,cnt;
    for(i=0; i<400500; i++)
    {
        b[i]=0;
        c[i]=0;
    }
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        m=max(m,a[i]);
    }

    for(i=0; i<n; i++)
    {
        temp=a[i];
        cnt=0;
        while (temp>0)
        {
            b[temp]+=cnt;
            c[temp]++;
            temp/=2;
            cnt++;
        }
        temp=a[i]*2;
        cnt=1;
        while (temp<=2*m)
        {
            b[temp]+=cnt;
            c[temp]++;
            cnt++;
            temp*=2;
        }
    }
    /*
    for(i=0; i<m; i++)
        cout<<i<<" "<<b[i].first<<" "<<b[i].second<<endl;
*/
ll ans=100000000000000;
for(i=0; i<4*m; i++)
if (c[i]==n) ans=min(ans,b[i]);
cout<<ans;

return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair <ll,ll> p;
vector <ll> a(100500);
vector <p> b(800500);
ll n,m=0;
int main()
{
    ll i,temp,cnt;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        m=max(m,a[i]);
    }
    for(i=0; i<n; i++)
    {
        temp=a[i];
        cnt=0;
        while (temp>0)
        {
            b[temp].first+=cnt;
            b[temp].second++;
            temp/=2;
            cnt++;
        }
        temp=a[i]*2;
        cnt=1;
        while (temp<=4*m)
        {
            b[temp].first+=cnt;
            b[temp].second++;
            cnt++;
            temp*=2;
        }
    }
    /*
    for(i=0; i<m; i++)
        cout<<i<<" "<<b[i].first<<" "<<b[i].second<<endl;
*/
ll ans=1000000000000;
for(i=0; i<4*m; i++)
if (b[i].second==n) ans=min(ans,b[i].first);
cout<<ans;

return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector <ll> a(100500);
typedef pair <ll,ll> myp;
vector <myp> b(200500);
set <ll> cont;
ll n,m=-1,ans=1000000000000000;
    void bfs(ll start)
    {
        //cout<<"new= "<<start<<endl;
        cont.clear();
        cont.insert(start);
        vector <myp> temp;
        temp.push_back(make_pair(start,0));
        ll yb=0, ye=0;
            while (yb<=ye)
            {
                ll cur=temp[yb].first;
                //cout<<"cur= "<<cur<<endl;

                b[cur].first+=temp[yb].second; // steps
                b[cur].second++; // numbers
                ll next=cur*2;
                ll prev=cur/2;
                if (prev>0 && cont.find(prev)==cont.end())
                {
                    cont.insert(prev);
                    temp.push_back(make_pair(prev,temp[yb].second+1));
                    ye++;
                }
                if (next<=(2*m) && cont.find(next)==cont.end())
                {
                    cont.insert(next);
                    temp.push_back(make_pair(next,temp[yb].second+1));
                    ye++;
                }
            yb++;
            }
    }
int main()
{
    ll i;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        m=max(m,a[i]);
    }

    for(i=0; i<n; i++)
        bfs(a[i]);
    for(i=0; i<400000; i++)
    if (b[i].second==n) ans=min(ans,b[i].first);
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
//ifstream fin("input.txt");
//#define cin fin
typedef pair <ll,ll> p;
vector <ll> a(100500);
vector <ll> b(400500),c(400500);
ll n,m=0;
    void full(ll a, ll cnt)
    {
       // cout<<"params="<<a<<" "<<cnt<<endl;
        b[a]+=cnt;
        c[a]++;
        if (a<=m) full(a*2,cnt+1);
    }
int main()
{

    ll i,temp,cnt;
    for(i=0; i<400500; i++)
    {
        b[i]=0;
        c[i]=0;
    }
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        m=max(m,a[i]);
    }

    for(i=0; i<n; i++)
    {
        ll t=a[i];
        full(t*2,1);
        cnt=0;
        while (t>0)
        {
          //  cout<<"t="<<t<<endl;
            b[t]+=cnt;
            c[t]++;
            cnt++;
            if (t%2==0)
            {
                t/=2;
            }
            else
            {
                t/=2;
                if (t>0) full(t*2,cnt+1);
            }
        }
    }
    /*
    for(i=0; i<m; i++)
        cout<<i<<" "<<b[i].first<<" "<<b[i].second<<endl;
*/
ll ans=100000000000000;
for(i=0; i<4*m; i++)
if (c[i]==n) {ans=min(ans,b[i]); /*cout<<"i="<<i<<endl;*/}
/*
for(i=0; i<20; i++)
    cout<<b[i]<<" "<<c[i]<<endl;
    */
cout<<ans;

return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
//ifstream fin("input.txt");
//#define cin fin
typedef pair <ll,ll> p;
vector <ll> a(100500);
vector <ll> b(400500),c(400500);
ll n,m=0;
int main()
{

    ll i,temp,cnt;
    for(i=0; i<400500; i++)
    {
        b[i]=0;
        c[i]=0;
    }
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        m=max(m,a[i]);
    }

    for(i=0; i<n; i++)
    {
        temp=a[i];
        cnt=0;
        while (temp>0)
        {
            b[temp]+=cnt;
            c[temp]++;
            temp/=2;
            cnt++;
        }
        temp=a[i]*2;
        cnt=1;
        while (temp<=2*m)
        {
            b[temp]+=cnt;
            c[temp]++;
            cnt++;
            temp*=2;
        }
    }
    /*
    for(i=0; i<m; i++)
        cout<<i<<" "<<b[i].first<<" "<<b[i].second<<endl;
*/
ll ans=100000000000000;
for(i=0; i<4*m; i++)
if (c[i]==n) ans=min(ans,b[i]);
cout<<ans;

return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector <ll> a(100500);
typedef pair <ll,ll> myp;
vector <myp> b(400500);
set <ll> cont;
ll n,m=-1,ans=1000000000000000;
    void bfs(ll start)
    {
        //cout<<"new= "<<start<<endl;
        cont.clear();
        cont.insert(start);
        vector <myp> temp;
        temp.push_back(make_pair(start,0));
        ll yb=0, ye=0;
            while (yb<=ye)
            {
                ll cur=temp[yb].first;
                //cout<<"cur= "<<cur<<endl;

                b[cur].first+=temp[yb].second; // steps
                b[cur].second++; // numbers
                ll next=cur*2;
                ll prev=cur/2;
                if (prev>0 && cont.find(prev)==cont.end())
                {
                    cont.insert(prev);
                    temp.push_back(make_pair(prev,temp[yb].second+1));
                    ye++;
                }
                if (next<=(4*m) && cont.find(next)==cont.end())
                {
                    cont.insert(next);
                    temp.push_back(make_pair(next,temp[yb].second+1));
                    ye++;
                }
            yb++;
            }
    }
int main()
{
    ll i;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        m=max(m,a[i]);
    }

    for(i=0; i<n; i++)
        bfs(a[i]);
    for(i=0; i<400000; i++)
    if (b[i].second==n) ans=min(ans,b[i].first);
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
//ifstream fin("input.txt");
//#define cin fin
typedef pair <ll,ll> p;
vector <ll> a(100500);
vector <ll> b(400500),c(400500);
ll n,m=0;
    void full(ll a, ll cnt)
    {
       // cout<<"params="<<a<<" "<<cnt<<endl;
        b[a]+=cnt;
        c[a]++;
        if (a<=m) full(a*2,cnt+1);
    }
int main()
{

    ll i,temp,cnt;
    for(i=0; i<400500; i++)
    {
        b[i]=0;
        c[i]=0;
    }
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        m=max(m,a[i]);
    }

    for(i=0; i<n; i++)
    {
        ll t=a[i];
        full(t*2,1);
        cnt=0;
        while (t>0)
        {
            b[t]+=cnt;
            c[t]++;
            cnt++;
            if (t%2==0)
            {
                t/=2;
            }
            else
            {
                t/=2;
                if (t>0) full(t,cnt);
            }
        }
    }
    /*
    for(i=0; i<m; i++)
        cout<<i<<" "<<b[i].first<<" "<<b[i].second<<endl;
*/
ll ans=100000000000000;
for(i=0; i<4*m; i++)

if (c[i]==n) {ans=min(ans,b[i]);}
cout<<ans;

return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair <ll,ll> p;
vector <ll> a(100500);
vector <p> b(200500);
ll n,m=0;
int main()
{
    ll i,temp,cnt;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        m=max(m,a[i]);
    }
    for(i=0; i<n; i++)
    {
        temp=a[i];
        cnt=0;
        while (temp>0)
        {
            b[temp].first+=cnt;
            b[temp].second++;
            temp/=2;
            cnt++;
        }
        temp=a[i]*2;
        cnt=1;
        while (temp<=m)
        {
            b[temp].first+=cnt;
            b[temp].second++;
            cnt++;
            temp*=2;
        }
    }
    /*
    for(i=0; i<m; i++)
        cout<<i<<" "<<b[i].first<<" "<<b[i].second<<endl;
*/
ll ans=1000000000000;
for(i=0; i<2*m; i++)
if (b[i].second==n) ans=min(ans,b[i].first);
cout<<ans;

return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector <ll> a(100500);
typedef pair <ll,ll> myp;
vector <myp> b(200500);
set <ll> cont;
ll n,m=-1,ans=1000000000000000;
    void bfs(ll start)
    {
        //cout<<"new= "<<start<<endl;
        cont.clear();
        cont.insert(start);
        vector <myp> temp;
        temp.push_back(make_pair(start,0));
        ll yb=0, ye=0;
            while (yb<=ye)
            {
                ll cur=temp[yb].first;
                //cout<<"cur= "<<cur<<endl;

                b[cur].first+=temp[yb].second; // steps
                b[cur].second++; // numbers
                ll next=cur*2;
                ll prev=cur/2;
                if (prev>0 && cont.find(prev)==cont.end())
                {
                    cont.insert(prev);
                    temp.push_back(make_pair(prev,temp[yb].second+1));
                    ye++;
                }
                if (next<=(2*m) && cont.find(next)==cont.end())
                {
                    cont.insert(next);
                    temp.push_back(make_pair(next,temp[yb].second+1));
                    ye++;
                }
            yb++;
            }
    }
int main()
{
    ll i;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        m=max(m,a[i]);
    }

    for(i=0; i<n; i++)
        bfs(a[i]);
    for(i=0; i<200000; i++)
    if (b[i].second==n) ans=min(ans,b[i].first);
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
//ifstream fin("input.txt");
//#define cin fin
typedef pair <ll,ll> p;
vector <ll> a(100500);
vector <ll> b(400500),c(400500);
ll n,m=0;
int main()
{

    ll i,temp,cnt;
    for(i=0; i<400500; i++)
    {
        b[i]=0;
        c[i]=0;
    }
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        m=max(m,a[i]);
    }

    for(i=0; i<n; i++)
    {
        temp=a[i];
        cnt=0;
        while (temp>0)
        {   if (temp%2==1)
                {
                    b[temp-1]=b[temp-1]+cnt+2;
                    c[temp-1]++;
                }
            b[temp]+=cnt;
            c[temp]++;
            temp/=2;
            cnt++;
        }
        temp=a[i]*2;
        cnt=1;
        while (temp<=2*m)
        {
            b[temp]+=cnt;
            c[temp]++;
            cnt++;
            temp*=2;
        }
    }
    /*
    for(i=0; i<m; i++)
        cout<<i<<" "<<b[i].first<<" "<<b[i].second<<endl;
*/
ll ans=100000000000000;
for(i=0; i<4*m; i++)
if (c[i]==n) ans=min(ans,b[i]);
cout<<ans;

return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
vector <ll> a(100500);
typedef pair <ll,ll> myp;
vector <myp> b(100500);
set <ll> cont;
ll n,m=-1,ans=1000000000000000;
    void bfs(ll start)
    {
        //cout<<"new= "<<start<<endl;
        cont.clear();
        cont.insert(start);
        vector <myp> temp;
        temp.push_back(make_pair(start,0));
        ll yb=0, ye=0;
            while (yb<=ye)
            {
                ll cur=temp[yb].first;
                //cout<<"cur= "<<cur<<endl;

                b[cur].first+=temp[yb].second; // steps
                b[cur].second++; // numbers
                ll next=cur*2;
                ll prev=cur/2;
                if (prev>0 && cont.find(prev)==cont.end())
                {
                    cont.insert(prev);
                    temp.push_back(make_pair(prev,temp[yb].second+1));
                    ye++;
                }
                if (next<=(m) && cont.find(next)==cont.end())
                {
                    cont.insert(next);
                    temp.push_back(make_pair(next,temp[yb].second+1));
                    ye++;
                }
            yb++;
            }
    }
int main()
{
    ll i;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
        m=max(m,a[i]);
    }

    for(i=0; i<n; i++)
        bfs(a[i]);
    for(i=0; i<100050; i++)
    if (b[i].second==n) ans=min(ans,b[i].first);
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= maxi) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }

  for (int i = 1; i <= maxi; i++) {
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      //if (freq[b] == n) {
        b <<= 1;
        d++;
        while (b <= maxi) {
          freq[b]++;
          dis[b] += d;
          b <<= 1;
          d++;
        }
      //}
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= maxi) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }

  for (int i = 0; i < n; i++) {
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      b <<= 1;
      d++;
      while (b <= maxi) {
        freq[b]++;
        dis[b] += d;
        b <<= 1;
        d++;
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, idx, ans = 1e9, a[100001], dp[100001];
map<int, int> dis[100001];
map<int, int> freq;
map<int, int> tot;

void f(int v, int d) {
  if (v <= 0) {
    return;
  }
  if (dis[idx].count(v)) {
    int &r = dis[idx][v];
    dis[idx][v] = min(r, d);
    return;
  }
  dis[idx][v] = d;
  f(v<<1, d+1);
  f(v>>1, d+1);
}

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    idx = i;
    if (!dp[a[i]]) {
      f(a[i], 0);
      dp[a[i]]++;
    }
  }
  for (int i = 0; i < n; i++) {
    for (auto p : dis[i]) {
      freq[p.first] += dp[a[i]];
      tot[p.first] += p.second*dp[a[i]];
    }
  }
  for (int i = 0; i < n; i++) {
    for (auto p: dis[i]) {
      if (freq[p.first] == n) {
        ans = min(ans, tot[p.first]);
      }
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= maxi) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }

  for (int i = 0; i < n; i++) {
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      //if (freq[b] == n) {
        b <<= 1;
        d++;
        while (b <= maxi) {
          freq[b]++;
          dis[b] += d;
          b <<= 1;
          d++;
        }
      //}
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, a, idx, ans = 1e9, maxi = 0;
map<int, int> dis[100001];
map<int, int> freq;
map<int, int> tot;

void f(int v, int d) {
  if (v <= 0 || v > maxi) {
    return;
  }
  if (dis[idx].count(v)) {
    int &r = dis[idx][v];
    dis[idx][v] = min(r, d);
    return;
  }
  dis[idx][v] = d;
  f(v<<1, d+1);
  f(v>>1, d+1);
}

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    maxi = max(maxi, a);
    idx = i;
    f(a, 0);
  }
  for (int i = 0; i < n; i++) {
    for (auto p : dis[i]) {
      freq[p.first]++;
      tot[p.first] += p.second;
    }
  }
  for (int i = 0; i < n; i++) {
    for (auto p: dis[i]) {
      if (freq[p.first] == n) {
        ans = min(ans, tot[p.first]);
      }
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b > 1 && b != ((b>>1)<<1)) {
        common[a[i]].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= maxi) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }

  for (int i = 0; i <= n; i++) {
    for (auto p : common[a[i]]) {
      int b = p.first, d = p.second;
      if (freq[b] == n) {
        b <<= 1;
        d++;
        while (b <= maxi) {
          freq[b]++;
          dis[b] += d;
          b <<= 1;
          d++;
        }
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, idx, ans = 1e9, a[100001], maxi = 0;
map<int, int> m;
map<int, int> freq;

int count(int v) {
  int tot = 0;
  for (int i = 0; i < n; i++) {
    int d = 0, b = a[i];
    while (v%b) {
      b >>= 1;
      d++;
    }
    while (v != b) {
      b <<= 1;
      d++;
    }
    tot += d;
  }
  return tot;
}

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i];
    set<int> s;
    while (b > 0) {
      freq[b]++;
      b >>= 1;
      s.insert(b);
    }
    for (auto b : s) {
      b <<= 1;
      while (b < maxi && !s.count(b)) {
        freq[b]++;
        b <<= 1;
      }
    }
  }
  for (auto p : freq) {
    if (p.second == n) {
      ans = min(ans, count(p.first));
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, vis[100001], dis[100001];
map<int, int> freq;

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    set<pair<int, int> > sd;
    memset(vis, 0, sizeof(vis));
    while (b > 0) {
      sd.insert(make_pair(b, d));
      vis[b] = 1;
      freq[b]++;
      dis[b] += d;
      b >>= 1;
      d++;
    }
    for (auto p : sd) {
      b = p.first<<1, d = p.second+1;
      while (b <= maxi && !vis[b]) {
        sd.insert(make_pair(b, d));
        vis[b] = 1;
        freq[b]++;
        dis[b] += d;
        b <<= 1;
        d++;
      }
    }
  }
  for (auto p : freq) {
    if (p.second == n) {
      ans = min(ans, dis[p.first]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
vector<pair<int, int> > common[100001];

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    while (b > 0) {
      freq[b]++;
      dis[b] += d;
      if (b != ((b>>1)<<1)) {
        common[i].push_back(make_pair(b>>1, d+1));
      }
      b >>= 1;
      d++;
    }
    b = a[i]<<1, d = 1;
    while (b <= maxi) {
      freq[b]++;
      dis[b] += d;
      b <<= 1;
      d++;
    }
  }

  for (int i = 1; i <= maxi; i++) {
    for (auto p : common[i]) {
      int b = p.first, d = p.second;
      if (freq[b] == n) {
        b <<= 1;
        d++;
        while (b <= maxi) {
          freq[b]++;
          dis[b] += d;
          b <<= 1;
          d++;
        }
      }
    }
  }

  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, ans = 1e9, a[100001], maxi = 0, dis[100001], freq[100001];
int sd[100001][2], nsd;

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    maxi = max(maxi, a[i]);
  }
  for (int i = 0; i < n; i++) {
    int b = a[i], d = 0;
    nsd = 0;
    int vis[100001] = {};
    while (b > 0) {
      sd[nsd][0] = b;
      sd[nsd++][1] = d;
      vis[b] = 1;
      freq[b]++;
      dis[b] += d;
      b >>= 1;
      d++;
    }
    for (int j = 0; j < nsd; j++) {
      b = sd[j][0]<<1, d = sd[j][1]+1;
      while (b <= maxi && !vis[b]) {
        sd[nsd][0] = b;
        sd[nsd++][1] = d;
        vis[b] = 1;
        freq[b]++;
        dis[b] += d;
        b <<= 1;
        d++;
      }
    }
  }
  for (int i = 1; i <= maxi; i++) {
    if (freq[i] == n) {
      ans = min(ans, dis[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int n, a, idx, ans = 1e9, maxi = 0;
map<int, int> dis[100001];
map<int, int> freq;
map<int, int> tot;

void f(int v, int d) {
  if (v <= 0 || v > 2*maxi) {
    return;
  }
  if (dis[idx].count(v)) {
    int &r = dis[idx][v];
    dis[idx][v] = min(r, d);
    return;
  }
  dis[idx][v] = d;
  f(v<<1, d+1);
  f(v>>1, d+1);
}

int main() { _
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    maxi = max(maxi, a);
    idx = i;
    f(a, 0);
  }
  for (int i = 0; i < n; i++) {
    for (auto p : dis[i]) {
      freq[p.first]++;
      tot[p.first] += p.second;
    }
  }
  for (int i = 0; i < n; i++) {
    for (auto p: dis[i]) {
      if (freq[p.first] == n) {
        ans = min(ans, tot[p.first]);
      }
    }
  }
  cout << ans << "\n";
  return 0;
}