import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.HashMap;
import java.util.PriorityQueue;
public class templ implements Runnable {
    class pair
    {
        int v,val;
        pair(int f,int s)
        {
            v=f;
            val=s;
        }
    }
    public static int M=1000000007;
    static int binarySearch(int arr[], int l, int r, int x)
    {
        if (r >= l)
        {
            int mid = l + (r - l)/2;
            if (arr[mid] == x)
                return mid;
            if (arr[mid] > x)
                return binarySearch(arr, l, mid-1, x);
            return binarySearch(arr, mid+1, r, x);
        }
        return -1;
    }
    void merge1(int arr[], int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        for (int i=0; i<n1; ++i)
            L[i] = arr[l + i];
        for (int j=0; j<n2; ++j)
            R[j] = arr[m + 1+ j];
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i]<=R[j])
            {
                arr[k] = L[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            j++;
            k++;
        }
    }
    void sort1(int arr[], int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort1(arr, l, m);
            sort1(arr , m+1, r);
            merge1(arr, l, m, r);
        }
    }
    void merge3(int arr[],int arr1[],int arr2[], int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        int L1[]=new int[n1];
        int R1[]=new int[n2];
        int L2[]=new int[n1];
        int R2[]=new int[n2];
        //long L3[]=new long[n1];
        //long R3[]=new long[n2];
        for (int i=0; i<n1; ++i)
        {
            L[i] = arr[l + i];
            L1[i]=arr1[l+i];
            L2[i]=arr2[l+i];
            //L3[i]=arr3[l+i];
        }
        for (int j=0; j<n2; ++j)
        {
            R[j] = arr[m + 1+ j];
            R1[j]=arr1[m+1+j];
            R2[j]=arr2[m+1+j];
            //R3[j]=arr3[m+1+j];
        }
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                arr1[k]=L1[i];
                arr2[k]=L2[i];
                //arr3[k]=L3[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                arr1[k]=R1[j];
                arr2[k]=R2[j];
                //arr3[k]=R3[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            arr1[k]=L1[i];
            arr2[k]=L2[i];
            //arr3[k]=L3[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            arr1[k]=R1[j];
            arr2[k]=R2[j];
            //arr3[k]=R3[j];
            j++;
            k++;
        }
    }
    void sort3(int arr[],int arr1[],int arr2[], int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort3(arr,arr1,arr2, l, m);
            sort3(arr ,arr1,arr2, m+1, r);
            merge3(arr,arr1,arr2,l, m, r);
        }
    }
    void merge2(int arr[],int arr1[],int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        int L1[]=new int[n1];
        int R1[]=new int[n2];
        for (int i=0; i<n1; ++i)
        {
            L[i] = arr[l + i];
            L1[i]=arr1[l+i];
        }
        for (int j=0; j<n2; ++j)
        {
            R[j] = arr[m + 1+ j];
            R1[j]=arr1[m+1+j];
        }
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                arr1[k]=L1[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                arr1[k]=R1[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            arr1[k]=L1[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            arr1[k]=R1[j];
            j++;
            k++;
        }
    }
    void sort2(int arr[],int arr1[],int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort2(arr,arr1, l, m);
            sort2(arr ,arr1, m+1, r);
            merge2(arr,arr1,l, m, r);
        }
    }
    void merge4(int arr[],int arr1[],int arr2[],int arr3[], int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        int L1[]=new int[n1];
        int R1[]=new int[n2];
        int L2[]=new int[n1];
        int R2[]=new int[n2];
        int L3[]=new int[n1];
        int R3[]=new int[n2];
        for (int i=0; i<n1; ++i)
        {
            L[i] = arr[l + i];
            L1[i]=arr1[l+i];
            L2[i]=arr2[l+i];
            L3[i]=arr3[l+i];
        }
        for (int j=0; j<n2; ++j)
        {
            R[j] = arr[m + 1+ j];
            R1[j]=arr1[m+1+j];
            R2[j]=arr2[m+1+j];
            R3[j]=arr3[m+1+j];
        }
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                arr1[k]=L1[i];
                arr2[k]=L2[i];
                arr3[k]=L3[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                arr1[k]=R1[j];
                arr2[k]=R2[j];
                arr3[k]=R3[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            arr1[k]=L1[i];
            arr2[k]=L2[i];
            arr3[k]=L3[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            arr1[k]=R1[j];
            arr2[k]=R2[j];
            arr3[k]=R3[j];
            j++;
            k++;
        }
    }
    void sort4(int arr[],int arr1[],int arr2[],int arr3[], int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort4(arr,arr1,arr2,arr3, l, m);
            sort4(arr ,arr1,arr2,arr3, m+1, r);
            merge4(arr,arr1,arr2,arr3,l, m, r);
        }
    }
    public int justsmall(int a[],int l,int r,int x)
    {
        int p=-1;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(a[mid]<=x)
            {
                p=mid;
                l=mid+1;
            }
            else
                r=mid-1;
        }
        return p;
    }
    public int justlarge(int a[],int l,int r,int x)
    {
        int p=0;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(a[mid]<x)
            {
                l=mid+1;
            }
            else
            {
                p=mid;
                r = mid - 1;
            }
        }
        return p;
    }
    long gcd(long x,long y)
    {
        if(x%y==0)
            return y;
        else
            return(gcd(y,x%y));
    }
    int fact(int n)
    {
        int ans=1;
        for(int i=1;i<=n;i++)
            ans*=i;
        return ans;
    }
    long bintoint(String s)
    {
        long a=0;
        int l=s.length();
        int k=0;
        for(int i=l-1;i>=0;i--)
        {
            char c=s.charAt(i);
            if(c=='1')
            {
                a=a+(long)Math.pow(2,k);
            }
            k++;
        }
        return a;
    }
    String inttobin(long x)
    {
        String s="";
        while(x!=0)
        {
            long k=x%2;
            if(k==1)
                s="1"+s;
            else
                s="0"+s;
            x=x/2;
        }
        return s;
    }
    public static void main(String args[])throws Exception
    {
        new Thread(null,new templ(),"templ",1<<26).start();
    }
    public void run() {
        try {
            InputReader in = new InputReader(System.in);
            PrintWriter out = new PrintWriter(System.out);
            int n=in.ni();
            int a[]=new int[n];
            for(int i=0;i<n;i++)
                a[i]=in.ni();
            int m=in.ni();
            int b[]=new int[m];
            for(int i=0;i<m;i++)
                b[i]=in.ni();
            int i=0,j=0;
            int x=0;
            long sum1=0,sum2=0;
            int l=0;
            while(i<n&&j<m)
            {
                sum1=a[i];
                sum2=b[j];
                i++;
                j++;
                while(sum1!=sum2)
                {
                    if(i==n&&j==m)
                        break;
                    if(sum1<sum2)
                    {
                        if(i==n)
                            break;
                        sum1=sum1+a[i];
                        i++;
                    }
                    if(sum2<sum1)
                    {
                        if(j==m)
                            break;
                        sum2=sum2+b[j];
                        j++;
                    }
                }
                if(sum1==sum2)
                {
                    l++;
                }
                if(i==n||j==m)
                    break;
            }
            if(i==n&&j==m&&sum1==sum2&&l!=0)
                out.println(l);
            else
                out.println("-1");
            out.close();
        }
        catch(Exception e){
            return;
        }
    }
    static class InputReader {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;

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

        public int read() {
            if (snumChars == -1)
                throw new InputMismatchException();
            if (curChar >= snumChars) {
                curChar = 0;
                try {
                    snumChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (snumChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int ni() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nl() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

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

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

    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.HashMap;
import java.util.PriorityQueue;
public class templ implements Runnable {
    class pair
    {
        int v,val;
        pair(int f,int s)
        {
            v=f;
            val=s;
        }
    }
    public static int M=1000000007;
    static int binarySearch(int arr[], int l, int r, int x)
    {
        if (r >= l)
        {
            int mid = l + (r - l)/2;
            if (arr[mid] == x)
                return mid;
            if (arr[mid] > x)
                return binarySearch(arr, l, mid-1, x);
            return binarySearch(arr, mid+1, r, x);
        }
        return -1;
    }
    void merge1(int arr[], int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        for (int i=0; i<n1; ++i)
            L[i] = arr[l + i];
        for (int j=0; j<n2; ++j)
            R[j] = arr[m + 1+ j];
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i]<=R[j])
            {
                arr[k] = L[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            j++;
            k++;
        }
    }
    void sort1(int arr[], int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort1(arr, l, m);
            sort1(arr , m+1, r);
            merge1(arr, l, m, r);
        }
    }
    void merge3(int arr[],int arr1[],int arr2[], int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        int L1[]=new int[n1];
        int R1[]=new int[n2];
        int L2[]=new int[n1];
        int R2[]=new int[n2];
        //long L3[]=new long[n1];
        //long R3[]=new long[n2];
        for (int i=0; i<n1; ++i)
        {
            L[i] = arr[l + i];
            L1[i]=arr1[l+i];
            L2[i]=arr2[l+i];
            //L3[i]=arr3[l+i];
        }
        for (int j=0; j<n2; ++j)
        {
            R[j] = arr[m + 1+ j];
            R1[j]=arr1[m+1+j];
            R2[j]=arr2[m+1+j];
            //R3[j]=arr3[m+1+j];
        }
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                arr1[k]=L1[i];
                arr2[k]=L2[i];
                //arr3[k]=L3[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                arr1[k]=R1[j];
                arr2[k]=R2[j];
                //arr3[k]=R3[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            arr1[k]=L1[i];
            arr2[k]=L2[i];
            //arr3[k]=L3[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            arr1[k]=R1[j];
            arr2[k]=R2[j];
            //arr3[k]=R3[j];
            j++;
            k++;
        }
    }
    void sort3(int arr[],int arr1[],int arr2[], int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort3(arr,arr1,arr2, l, m);
            sort3(arr ,arr1,arr2, m+1, r);
            merge3(arr,arr1,arr2,l, m, r);
        }
    }
    void merge2(int arr[],int arr1[],int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        int L1[]=new int[n1];
        int R1[]=new int[n2];
        for (int i=0; i<n1; ++i)
        {
            L[i] = arr[l + i];
            L1[i]=arr1[l+i];
        }
        for (int j=0; j<n2; ++j)
        {
            R[j] = arr[m + 1+ j];
            R1[j]=arr1[m+1+j];
        }
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                arr1[k]=L1[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                arr1[k]=R1[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            arr1[k]=L1[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            arr1[k]=R1[j];
            j++;
            k++;
        }
    }
    void sort2(int arr[],int arr1[],int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort2(arr,arr1, l, m);
            sort2(arr ,arr1, m+1, r);
            merge2(arr,arr1,l, m, r);
        }
    }
    void merge4(int arr[],int arr1[],int arr2[],int arr3[], int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        int L1[]=new int[n1];
        int R1[]=new int[n2];
        int L2[]=new int[n1];
        int R2[]=new int[n2];
        int L3[]=new int[n1];
        int R3[]=new int[n2];
        for (int i=0; i<n1; ++i)
        {
            L[i] = arr[l + i];
            L1[i]=arr1[l+i];
            L2[i]=arr2[l+i];
            L3[i]=arr3[l+i];
        }
        for (int j=0; j<n2; ++j)
        {
            R[j] = arr[m + 1+ j];
            R1[j]=arr1[m+1+j];
            R2[j]=arr2[m+1+j];
            R3[j]=arr3[m+1+j];
        }
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                arr1[k]=L1[i];
                arr2[k]=L2[i];
                arr3[k]=L3[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                arr1[k]=R1[j];
                arr2[k]=R2[j];
                arr3[k]=R3[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            arr1[k]=L1[i];
            arr2[k]=L2[i];
            arr3[k]=L3[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            arr1[k]=R1[j];
            arr2[k]=R2[j];
            arr3[k]=R3[j];
            j++;
            k++;
        }
    }
    void sort4(int arr[],int arr1[],int arr2[],int arr3[], int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort4(arr,arr1,arr2,arr3, l, m);
            sort4(arr ,arr1,arr2,arr3, m+1, r);
            merge4(arr,arr1,arr2,arr3,l, m, r);
        }
    }
    public int justsmall(int a[],int l,int r,int x)
    {
        int p=-1;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(a[mid]<=x)
            {
                p=mid;
                l=mid+1;
            }
            else
                r=mid-1;
        }
        return p;
    }
    public int justlarge(int a[],int l,int r,int x)
    {
        int p=0;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(a[mid]<x)
            {
                l=mid+1;
            }
            else
            {
                p=mid;
                r = mid - 1;
            }
        }
        return p;
    }
    long gcd(long x,long y)
    {
        if(x%y==0)
            return y;
        else
            return(gcd(y,x%y));
    }
    int fact(int n)
    {
        int ans=1;
        for(int i=1;i<=n;i++)
            ans*=i;
        return ans;
    }
    long bintoint(String s)
    {
        long a=0;
        int l=s.length();
        int k=0;
        for(int i=l-1;i>=0;i--)
        {
            char c=s.charAt(i);
            if(c=='1')
            {
                a=a+(long)Math.pow(2,k);
            }
            k++;
        }
        return a;
    }
    String inttobin(long x)
    {
        String s="";
        while(x!=0)
        {
            long k=x%2;
            if(k==1)
                s="1"+s;
            else
                s="0"+s;
            x=x/2;
        }
        return s;
    }
    public static void main(String args[])throws Exception
    {
        new Thread(null,new templ(),"templ",1<<26).start();
    }
    public void run() {
        try {
            InputReader in = new InputReader(System.in);
            PrintWriter out = new PrintWriter(System.out);
            int n=in.ni();
            int a[]=new int[n];
            for(int i=0;i<n;i++)
                a[i]=in.ni();
            int m=in.ni();
            int b[]=new int[m];
            for(int i=0;i<m;i++)
                b[i]=in.ni();
            int i=0,j=0;
            int x=0;
            long sum1=0,sum2=0;
            int l=0;
            while(i<n&&j<m)
            {
                sum1=a[i];
                sum2=b[j];
                i++;
                j++;
                while(sum1!=sum2)
                {
                    if(i==n||j==m)
                        break;
                    if(sum1<sum2)
                    {
                        sum1=sum1+a[i];
                        i++;
                    }
                    if(sum2<sum1)
                    {
                        sum2=sum2+b[j];
                        j++;
                    }
                }
                if(sum1==sum2)
                {
                    l++;
                }
                if(i==n||j==m)
                    break;
            }
            if(i==n&&j==m&&sum1==sum2&&l!=0)
                out.println(l);
            else
                out.println("-1");
            out.close();
        }
        catch(Exception e){
            return;
        }
    }
    static class InputReader {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;

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

        public int read() {
            if (snumChars == -1)
                throw new InputMismatchException();
            if (curChar >= snumChars) {
                curChar = 0;
                try {
                    snumChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (snumChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int ni() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nl() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

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

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

    }
}


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

public class Main {
    public static void main(String[] args) {
        FastReader in = new FastReader();
        int n = in.nextInt();
        long[] an = new long[n+1];
        for(int i=1 ;i<=n ;i++) an[i] = in.nextLong();
        int m = in.nextInt();
        long[] am = new long[m+1];
        for(int i=1 ;i<=m ;i++) am[i] = in.nextLong();

        int i=n,j=m;
        int ans=0;
        while(i!=0 && j!=0){
            if(an[i]<am[j]){
                an[i-1]+=an[i];
                i--;
                ans++;
            }else if(an[i]>am[j]){
                am[j-1]+=am[j];
                j--;
            }else{
                i--;
                j--;
            }
        }

        System.out.println((an[i]==0 && am[j]==0 ) ? (n-ans) : -1);

    }

    static class FastReader  {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

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

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

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

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

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


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

public class Main {
    public static void main(String[] args) {
        FastReader in = new FastReader();
        int n = in.nextInt();
        long[] an = new long[n+1];
        for(int i=1 ;i<=n ;i++) an[i] = in.nextLong();
        int m = in.nextInt();
        long[] am = new long[m+1];
        for(int i=1 ;i<=m ;i++) am[i] = in.nextLong();

        int i=n,j=m;
        int ans=0;
        while(i!=0 && j!=0){
            if(an[i]<am[j]){
                an[i-1]+=an[i];
                i--;
                ans++;
            }else if(an[i]>am[j]){
                am[j-1]+=am[j];
                j--;
            }else{
                i--;
                j--;
            }
        }

        System.out.println((an[0]==0 && am[0]==0) ? (n-ans) : -1);

    }

    static class FastReader  {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

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

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

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

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

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


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

public class Main {
    public static void main(String[] args) {
        FastReader in = new FastReader();
        int n = in.nextInt();
        long[] an = new long[n+1];
        for(int i=1 ;i<=n ;i++) an[i] = in.nextLong();
        int m = in.nextInt();
        long[] am = new long[m+1];
        for(int i=1 ;i<=m ;i++) am[i] = in.nextLong();

        int i=n,j=m;
        int ans=0;
        while(i!=0 && j!=0){
            if(an[i]<am[j]){
                an[i-1]+=an[i];
                i--;
                ans++;
            }else if(an[i]>am[j]){
                am[j-1]+=am[j];
                j--;
            }else{
                i--;
                j--;
            }
        }

        System.out.println((an[j]==0 && am[i]==0 ) ? (n-ans) : -1);

    }

    static class FastReader  {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

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

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

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

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

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            long a[] = new long[n];
            for (int i = 0; i < n; i++) {
                long t = in.nextLong();
                if (i == 0)
                    a[i] = t;
                else
                    a[i] = a[i - 1] + t;
            }
            int m = in.nextInt();
            long b[] = new long[m];
            for (int i = 0; i < m; i++) {
                long t = in.nextLong();
                if (i == 0)
                    b[i] = t;
                else
                    b[i] = b[i - 1] + t;
            }

            int c[][] = new int[n][m];
            c[0][0] = a[0] == b[0] ? 1 : 0;
            for (int i = 1; i < m; i++) {
                if (a[0] == b[i])
                    c[0][i] = 1;
                else
                    c[0][i] = c[0][i - 1];
            }
            for (int i = 1; i < n; i++) {
                if (a[i] == b[0])
                    c[i][0] = 1;
                else
                    c[i][0] = c[i - 1][0];
            }
            for (int i = 1; i < n; i++) {
                for (int j = 1; j < m; j++) {
                    if (a[i] == b[j])
                        c[i][j] = c[i - 1][j - 1] + 1;
                    else
                        c[i][j] = Math.max(c[i - 1][j], c[i][j - 1]);
                }
            }
            out.println(c[n - 1][m - 1] != 0 ? c[n - 1][m - 1] : -1);
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Scanner;

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

    static class TaskD {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            long a[] = new long[n];
            for (int i = 0; i < n; i++) {
                long t = in.nextLong();
                if (i == 0)
                    a[i] = t;
                else
                    a[i] = a[i - 1] + t;
            }
            int m = in.nextInt();
            long b[] = new long[m];
            for (int i = 0; i < m; i++) {
                long t = in.nextLong();
                if (i == 0)
                    b[i] = t;
                else
                    b[i] = b[i - 1] + t;
            }
            int i=0;
            int j=0;
            int ans=0;
            while(i<n && j<m){
                if(a[i]==b[j]){
                    ans++;
                    i++;
                    j++;
                }
                else if(a[i]<b[j]){
                    i++;
                }
                else{
                    j++;
                }
            }
            if(a[n-1]==b[m-1]){
                out.println(ans);
            }
            else{
                out.println(-1);
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

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

    static class TaskD {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            long sum1=0;
            HashMap<Long,Boolean> hm=new HashMap<>();
            for (int i = 0; i < n; i++) {
                long t = in.nextLong();
                sum1+=t;
                hm.put(sum1,true);
            }
            int m = in.nextInt();
            long sum=0;
            int ans=0;
            for (int i = 0; i < m; i++) {
                long t = in.nextLong();
                sum+=t;
                if (hm.containsKey(sum)){
                    ans++;
                }
            }
            if (sum==sum1){
                out.println(ans);
            }
            else{
                out.println(-1);
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

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

    static class TaskD {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            long a[] = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = in.nextLong();
            }
            int m = in.nextInt();
            long b[] = new long[m];
            for (int i = 0; i < m; i++) {
                b[i] = in.nextLong();
            }
            long ans = 0;
            long curra = a[0];
            long currb = b[0];
            int i = 1;
            int j = 1;
            while (i < n && j < m) {
                if (curra == currb) {
                    ans++;
                    curra = a[i];
                    currb = b[j];
                    i++;
                    j++;
                } else if (curra < currb) {
                    curra += a[i++];
                } else {
                    currb += a[j++];
                }
            }

            if (curra == currb) {
                out.println(ans+1);
            } else {
                out.println(-1);
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

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

    static class TaskD {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            long sum1=0;
            HashMap<Long,Boolean> hm=new HashMap<>();
            for (int i = 0; i < n; i++) {
                long t = in.nextLong();
                sum1+=t;
                hm.put(sum1,true);
            }
            int m = in.nextInt();
            long sum=0;
            int ans=0;
            for (int i = 0; i < m; i++) {
                long t = in.nextLong();
                sum+=t;
                if (hm.get(sum)!=null){
                    ans++;
                }
            }
            if (sum==sum1){
                out.println(ans);
            }
            else{
                out.println(-1);
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

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

    static class D {
        public void solve(int testNumber,InputReader in, OutputWriter out) {
            int n = in.nextInt();
            long a[] = new long[n];
            for (int i = 0; i < n; i++) {
                long t = in.nextLong();
                if (i == 0)
                    a[i] = t;
                else
                    a[i] = a[i - 1] + t;
            }
            int m = in.nextInt();
            long b[] = new long[m];
            for (int i = 0; i < m; i++) {
                long t = in.nextLong();
                if (i == 0)
                    b[i] = t;
                else
                    b[i] = b[i - 1] + t;
            }
            int i=0;
            int j=0;
            int ans=0;
            while(i<n && j<m){
                if(a[i]==b[j]){
                    ans++;
                    i++;
                    j++;
                }
                else if(a[i]<b[j]){
                    i++;
                }
                else{
                    j++;
                }
            }
            if(a[n-1]==b[m-1]){
                out.println(ans);
            }
            else{
                out.println(-1);
            }
        }

    }

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

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

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Scanner;

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

    static class TaskD {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            long a[] = new long[n];
            for (int i = 0; i < n; i++) {
                long t = in.nextLong();
                if (i == 0)
                    a[i] = t;
                else
                    a[i] = a[i - 1] + t;
            }
            int m = in.nextInt();
            long b[] = new long[m];
            for (int i = 0; i < m; i++) {
                long t = in.nextLong();
                if (i == 0)
                    b[i] = t;
                else
                    b[i] = b[i - 1] + t;
            }
            if(a[n-1]!=b[m-1]){
                out.println(-1);
            }
            else{
            int i=0;
            int j=0;
            int ans=0;
            while(i<n && j<m){
                if(a[i]==b[j]){
                    ans++;
                    i++;
                    j++;
                }
                else if(a[i]<b[j]){
                    i++;
                }
                else{
                    j++;
                }
            }
                out.println(ans);
            }
        }

    }
}


// package Practice2.CF1036;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class CF1036D {
    public static void main(String[] args) throws Exception{
        Reader s = new Reader(/*System.in*/);
        int n = s.nextInt();
//        int[] arr = new int[n];
        ArrayList<Integer> arr = new ArrayList<>();

//        int[] brr = new int[m];
        ArrayList<Integer> brr = new ArrayList<>();
        long sum = 0;
        for (int i = 0; i < n; i++) {
            arr.add(s.nextInt());
            sum += arr.get(i);
        }
        int m = s.nextInt();
        for (int i = 0; i < m; i++) {
            brr.add(s.nextInt());
            sum -= brr.get(i);
        }

        if (sum != 0) {
            System.out.println(-1);
            return;
        }

        int ans = 0;
        int i = 0;
        int j = 0;
        while (i < n && j < m) {
            long sum1 = arr.get(i++);
            long sum2 = brr.get(j++);
            while (sum1 != sum2) {
                if (sum1 < sum2) {
                    sum1 += arr.get(i++);
                } else {
                    sum2 += brr.get(j++);
                }
            }
            ans++;
        }
        System.out.println(ans);
//        int count = arr.in
    }

    private static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();

            do {
                ret = ret * 10 + c - '0';
            }
            while ((c = read()) >= '0' && c <= '9');

            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException {
            if (din == null)
                return;
            din.close();
        }
    }
}


// package Practice2.CF1036;

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

public class CF1036D {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
//        int[] arr = new int[n];
        ArrayList<Integer> arr = new ArrayList<>();

//        int[] brr = new int[m];
        ArrayList<Integer> brr = new ArrayList<>();
        long sum = 0;
        for (int i = 0; i < n; i++) {
            arr.add(s.nextInt());
            sum += arr.get(i);
        }
        int m = s.nextInt();
        for (int i = 0; i < m; i++) {
            brr.add(s.nextInt());
            sum -= brr.get(i);
        }

        if(sum != 0){
            System.out.println(-1);
            return;
        }

        int ans = 0;
        int i =0;
        int j = 0;
        while(i < n && j < m){
            long sum1 = arr.get(i++);
            long sum2 = brr.get(j++);
            while(sum1 != sum2){
                if(sum1 < sum2){
                    sum1 += arr.get(i++);
                }else{
                    sum2 += brr.get(j++);
                }
            }
            ans++;
        }
        System.out.println(ans);
//        int count = arr.in
    }
}


#include <cstdio>

int main(){
    int n, m;
    scanf("%d", &n);
    int a[n];
    for(int i=0; i < n; ++i){
        scanf("%d", a+i);
    }
    scanf("%d", &m);
    int b[m];
    for(int i=0; i < m; ++i){
        scanf("%d", b+i);
    }
    int i = 1, j = 1;
    long long suma = a[0];
    long long sumb = b[0];
    int ans = 0;
    while(i < n && j < m){
        if(suma < sumb){
            suma += a[i++];
        }
        else if(suma > sumb){
            sumb += b[j++];
        }
        else{
            ++ans;
            suma = a[i++];
            sumb = b[j++];
        }
    }
    while(i < n){
        suma += a[i++];
    }
    while(j < m){
        sumb += a[j++];
    }
    if(suma != sumb){
        puts("-1");
    }
    else{
        printf("%d\n", ans+1);
    }
    return 0;
}


#include <cstdio>

int main(){
    int n, m;
    scanf("%d", &n);
    int a[n];
    for(int i=0; i < n; ++i){
        scanf("%d", a+i);
    }
    scanf("%d", &m);
    int b[m];
    for(int i=0; i < m; ++i){
        scanf("%d", b+i);
    }
    int i = 1, j = 1;
    long long suma = a[0];
    long long sumb = b[0];
    int ans = 0;
    while(i < n && j < m){
        if(suma < sumb){
            suma += a[i++];
        }
        else if(suma > sumb){
            sumb += b[j++];
        }
        else{
            ++ans;
            suma = a[i++];
            sumb = b[j++];
        }
    }
    while(i < n){
        suma += a[i++];
    }
    while(j < m){
        sumb += b[j++];
    }
    if(suma != sumb){
        puts("-1");
    }
    else{
        printf("%d\n", ans+1);
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<deque>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int len=3e5+5;
const ll mod=10007;
ll s1[len],s2[len];
int main()
{
    int n,m;
    cin>>n;
    ll t;
    for(int i=1;i<=n;++i)
    {
    	scanf("%d",&t);
    	s1[i]=t+s1[i-1];
	}
	cin>>m;
	for(int i=1;i<=m;++i)
	{
		scanf("%d",&t);
		s2[i]=t+s2[i-1];
	}
	if(s1[n]!=s2[m])return puts("-1"),0;
	int x=1,y=1,ans=0;
	while(x<=n||y<=m)
	{
		if(s1[x]==s2[y])ans++,x++,y++;
		else if(s1[x]>s2[y])y++;
		else if(s1[x]<s2[y])x++;
	}
	cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<deque>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int len=3e5+5;
const ll mod=10007;
#define maxn 300000+10
int a[maxn],b[maxn];
int main()
{
	int n,m,ans;
	ans=0;
  		cin>>n;
	  	for(int i=0;i<n;i++)
	  	{
	  	scanf("%d",&a[i]);
	  	 }
	   	cin>>m;
	   for(int i=0;i<m;i++)
	   {
	   	scanf("%d",&b[i]);
	   }
	   int l,r;
	   ll sum1,sum2;
	   	sum1=a[0];  sum2=b[0];
	   	l=r=0;
	   while(l<n&&r<m)
	   {
	   	if(sum1==sum2)
	   	{
	   		ans++;
	   		sum1=a[++l];
	   		sum2=b[++r];
		}
		else if(sum1>sum2)
		{
			sum2+=b[++r];
		}
		else
		sum1+=a[++l];
	   }
	   if(l==n&&r==m)
	   printf("%d\n",ans);
	   else
	   printf("-1\n");
	   return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<deque>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int len=3e5+5;
const ll mod=10007;
#define maxn 300000+10
int a[maxn],b[maxn];
int main()
{
	int n,m,ans;
	ans=0;
  		cin>>n;
	  	for(int i=0;i<n;i++)
	  	{
	  	scanf("%d",&a[i]);
	  	 }
	   	cin>>m;
	   for(int i=0;i<m;i++)
	   {
	   	scanf("%d",&b[i]);
	   }
	   int l,r;
	   ll sum1,sum2;
	   	sum1=a[0];  sum2=b[0];
	   	l=r=0;
	   while(l<n&&r<m)
	   {
	   	if(sum1==sum2)
	   	{
	   		ans++;
	   		sum1=a[++l];
	   		sum2=b[++r];
		}
		else if(sum1>sum2)
		{
			sum2+=b[++r];
		}
		else
		sum1+=a[++l];
	   }
	   if(l==n&&r==m)
	   printf("%lld\n",ans);
	   else
	   printf("-1\n");
	   return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
long long a[300020], b[300020];
int main()
{
	int n, m, i, j;
	while (cin >> n)
	{
		for (i = 0; i < n; i++)
		{
			scanf("%lld", &a[i]);
		}
		cin >> m;
		for (j = 0; j < m; j++)
		{
			scanf("%lld", &b[j]);
		}
		long long sum1 = a[0], sum2 = 0;
		i = 1; j = 0;
		int ans = 0;
		while (i < n&&j < m)
		{
			if (sum1 < sum2)
			{
				sum1 += a[i];
				i++;
			}
			else if (sum1>sum2)
			{
				sum2 += b[j];
				j++;
			}
			else
			{
				sum1 = a[i];
				sum2 = 0;
				ans++;
				i++;
			}
		}
		for (; i < n; i++)
			sum1 += a[i];
		for (; j < m; j++)
			sum2 += b[j];
		if (sum1 == sum2)
		{
			cout << ans + 1 << endl;
		}
		else
			cout << "-1" << endl;
	}
	return 0;
}


#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<deque>
#include<chrono>
#include<random>
#include<bitset>
#include<assert.h>
#include<cstdio>
#include<string.h>
using namespace std;
ofstream fo("test.out");
ifstream fi("test.inp");
#define For(i,n) for (long long(i) = 0; (i)<(n); (i)++)
#define For1(i,n) for (long long(i) = 1; (i) <= (n); (i)++)
#define Forlr(i,l,r) for (long long(i) = (l); (i) <= (r); (i)++)
#define Forrl(i,r,l) for (long long(i) = (r); (i) >= (l); (i)--)
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll base = 269;
const int oo = 1 << 30;
const int N = 1e5 + 5;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll P(ll x, ll y) // x^y
{
	ll ret = 1;
	while (y > 0)
	{
		if (y & 1) ret *= x;
		y /= 2;
		x = x * x;
	}
	return ret;
}
ll GCD(ll a, ll b)
{
	return (b ? GCD(b, a%b) : a);
}

int gi() {
	int w = 0; bool q = 1; char c = getchar();
	while ((c<'0' || c>'9') && c != '-') c = getchar();
	if (c == '-') q = 0, c = getchar();
	while (c >= '0'&&c <= '9') w = w * 10 + c - '0', c = getchar();
	return q ? w : -w;
}
namespace io {
	const int L = (1 << 21) + 1;
	char ibuf[L], *iS, *iT, obuf[L], *oS = obuf, *oT = obuf + L - 1, c, st[55]; int f, tp;
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	inline char getc() {
		return gc();
	}
	inline void flush() {
		fwrite(obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); }
	template<class I> inline bool gi(I&x) {
		for (f = 1, c = gc(); c<'0' || c>'9'; c = gc()) if (c == '-') f = -1; else if (c == -1) return false;
		for (x = 0; c <= '9'&&c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return true;
	}
	template<class I> inline void print(I x) {
		if (!x) putc('0');
		if (x < 0) putc('-'), x = -x;
		while (x) st[++tp] = x % 10 + '0', x /= 10;
		while (tp) putc(st[tp--]);
	}
	inline bool gs(char*s, int&l) {
		for (c = gc(); c<'a' || c>'z'; c = gc()) if (c == -1) return false;
		for (l = 0; c <= 'z'&&c >= 'a'; c = gc()) s[l++] = c;
		s[l] = 0;
		return true;
	}
	inline void printld(double x, int digit) {
		if (x < 0) putc('-'), x = -x;
		x += 0.5 * pow(0.1, digit);
		print((ll)x), putc('.');
		for (x -= (ll)x; digit; --digit) x = x * 10, putc((int)x + '0'), x -= (int)x;
	}
	inline void ps(const char*s) { for (int i = 0, n = strlen(s); i < n; i++) putc(s[i]); }
	struct IOFLUSHER { ~IOFLUSHER() { flush(); } } _ioflusher_;
};
using io::getc;
using io::putc;
using io::gi;
using io::gs;
using io::ps;
using io::print;
using io::printld;
void inp_outp()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
/*
MIKUNO - 15/02/1999
*/
ll n, m, q, k, pos, rs, check;
ll a[3 * N], b[3 * N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n;
	For(i, n)
		cin >> a[i], check+=a[i];
	cin >> m;
	For(i, n)
		cin >> b[i], check-=b[i];
	if (check) return cout << -1, 0;
	ll i = 0, j = 0;
	while (i < n && j < n)
	{
		ll val1 = a[i], val2 = b[j];
		while (val1 < val2)
		{
			i++;
			val1 += a[i];
		}
		while (val2 < val1)
		{
			j++;
			val2 += b[j];
		}
		if (val1 == val2)
			rs++, i++, j++;
	}
	cout << rs;
}


#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<deque>
#include<chrono>
#include<random>
#include<bitset>
#include<assert.h>
#include<cstdio>
#include<string.h>
using namespace std;
ofstream fo("test.out");
ifstream fi("test.inp");
#define For(i,n) for (long long(i) = 0; (i)<(n); (i)++)
#define For1(i,n) for (long long(i) = 1; (i) <= (n); (i)++)
#define Forlr(i,l,r) for (long long(i) = (l); (i) <= (r); (i)++)
#define Forrl(i,r,l) for (long long(i) = (r); (i) >= (l); (i)--)
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll base = 269;
const int oo = 1 << 30;
const int N = 1e5 + 5;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll P(ll x, ll y) // x^y
{
	ll ret = 1;
	while (y > 0)
	{
		if (y & 1) ret *= x;
		y /= 2;
		x = x * x;
	}
	return ret;
}
ll GCD(ll a, ll b)
{
	return (b ? GCD(b, a%b) : a);
}

int gi() {
	int w = 0; bool q = 1; char c = getchar();
	while ((c<'0' || c>'9') && c != '-') c = getchar();
	if (c == '-') q = 0, c = getchar();
	while (c >= '0'&&c <= '9') w = w * 10 + c - '0', c = getchar();
	return q ? w : -w;
}
namespace io {
	const int L = (1 << 21) + 1;
	char ibuf[L], *iS, *iT, obuf[L], *oS = obuf, *oT = obuf + L - 1, c, st[55]; int f, tp;
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	inline char getc() {
		return gc();
	}
	inline void flush() {
		fwrite(obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); }
	template<class I> inline bool gi(I&x) {
		for (f = 1, c = gc(); c<'0' || c>'9'; c = gc()) if (c == '-') f = -1; else if (c == -1) return false;
		for (x = 0; c <= '9'&&c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return true;
	}
	template<class I> inline void print(I x) {
		if (!x) putc('0');
		if (x < 0) putc('-'), x = -x;
		while (x) st[++tp] = x % 10 + '0', x /= 10;
		while (tp) putc(st[tp--]);
	}
	inline bool gs(char*s, int&l) {
		for (c = gc(); c<'a' || c>'z'; c = gc()) if (c == -1) return false;
		for (l = 0; c <= 'z'&&c >= 'a'; c = gc()) s[l++] = c;
		s[l] = 0;
		return true;
	}
	inline void printld(double x, int digit) {
		if (x < 0) putc('-'), x = -x;
		x += 0.5 * pow(0.1, digit);
		print((ll)x), putc('.');
		for (x -= (ll)x; digit; --digit) x = x * 10, putc((int)x + '0'), x -= (int)x;
	}
	inline void ps(const char*s) { for (int i = 0, n = strlen(s); i < n; i++) putc(s[i]); }
	struct IOFLUSHER { ~IOFLUSHER() { flush(); } } _ioflusher_;
};
using io::getc;
using io::putc;
using io::gi;
using io::gs;
using io::ps;
using io::print;
using io::printld;
void inp_outp()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
/*
MIKUNO - 15/02/1999
*/
ll n, m, q, k, pos, rs, check;
ll a[3 * N], b[3 * N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	gi(n);
	For(i, n)
		gi(a[i]), check+=a[i];
	cin >> m;
	For(i, m)
		gi(b[i]), check-=b[i];
	if (check) return cout << -1, 0;
	ll i = 0, j = 0;
	while (i < n && j < m)
	{
		ll val1 = a[i], val2 = b[j];
		while (val1 < val2)
		{
			i++;
			val1 += a[i];
		}
		while (val2 < val1)
		{
			j++;
			val2 += b[j];
		}
		if (val1 == val2)
			rs++, i++, j++;
	}

	if (!rs) cout << -1;
	else cout << rs;
}


#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<deque>
#include<chrono>
#include<random>
#include<bitset>
#include<assert.h>
#include<cstdio>
#include<string.h>
using namespace std;
ofstream fo("test.out");
ifstream fi("test.inp");
#define For(i,n) for (long long(i) = 0; (i)<(n); (i)++)
#define For1(i,n) for (long long(i) = 1; (i) <= (n); (i)++)
#define Forlr(i,l,r) for (long long(i) = (l); (i) <= (r); (i)++)
#define Forrl(i,r,l) for (long long(i) = (r); (i) >= (l); (i)--)
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll base = 269;
const int oo = 1 << 30;
const int N = 1e5 + 5;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll P(ll x, ll y) // x^y
{
	ll ret = 1;
	while (y > 0)
	{
		if (y & 1) ret *= x;
		y /= 2;
		x = x * x;
	}
	return ret;
}
ll GCD(ll a, ll b)
{
	return (b ? GCD(b, a%b) : a);
}

int gi() {
	int w = 0; bool q = 1; char c = getchar();
	while ((c<'0' || c>'9') && c != '-') c = getchar();
	if (c == '-') q = 0, c = getchar();
	while (c >= '0'&&c <= '9') w = w * 10 + c - '0', c = getchar();
	return q ? w : -w;
}
namespace io {
	const int L = (1 << 21) + 1;
	char ibuf[L], *iS, *iT, obuf[L], *oS = obuf, *oT = obuf + L - 1, c, st[55]; int f, tp;
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	inline char getc() {
		return gc();
	}
	inline void flush() {
		fwrite(obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); }
	template<class I> inline bool gi(I&x) {
		for (f = 1, c = gc(); c<'0' || c>'9'; c = gc()) if (c == '-') f = -1; else if (c == -1) return false;
		for (x = 0; c <= '9'&&c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return true;
	}
	template<class I> inline void print(I x) {
		if (!x) putc('0');
		if (x < 0) putc('-'), x = -x;
		while (x) st[++tp] = x % 10 + '0', x /= 10;
		while (tp) putc(st[tp--]);
	}
	inline bool gs(char*s, int&l) {
		for (c = gc(); c<'a' || c>'z'; c = gc()) if (c == -1) return false;
		for (l = 0; c <= 'z'&&c >= 'a'; c = gc()) s[l++] = c;
		s[l] = 0;
		return true;
	}
	inline void printld(double x, int digit) {
		if (x < 0) putc('-'), x = -x;
		x += 0.5 * pow(0.1, digit);
		print((ll)x), putc('.');
		for (x -= (ll)x; digit; --digit) x = x * 10, putc((int)x + '0'), x -= (int)x;
	}
	inline void ps(const char*s) { for (int i = 0, n = strlen(s); i < n; i++) putc(s[i]); }
	struct IOFLUSHER { ~IOFLUSHER() { flush(); } } _ioflusher_;
};
using io::getc;
using io::putc;
using io::gi;
using io::gs;
using io::ps;
using io::print;
using io::printld;
void inp_outp()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
/*
MIKUNO - 15/02/1999
*/
ll n, m, q, k, pos, rs, check;
ll a[3 * N], b[3 * N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n;
	For(i, n)
		cin >> a[i], check+=a[i];
	cin >> m;
	For(i, m)
		cin >> b[i], check-=b[i];
	if (check) return cout << -1, 0;
	ll i = 0, j = 0;
	while (i < n && j < m)
	{
		ll val1 = a[i], val2 = b[j];
		while (val1 < val2)
		{
			i++;
			val1 += a[i];
		}
		while (val2 < val1)
		{
			j++;
			val2 += b[j];
		}
		if (val1 == val2)
			rs++, i++, j++;
	}
	if (!rs) cout << -1;
	else cout << rs;
}


#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<deque>
#include<chrono>
#include<random>
#include<bitset>
#include<assert.h>
#include<cstdio>
#include<string.h>
using namespace std;
ofstream fo("test.out");
ifstream fi("test.inp");
#define For(i,n) for (long long(i) = 0; (i)<(n); (i)++)
#define For1(i,n) for (long long(i) = 1; (i) <= (n); (i)++)
#define Forlr(i,l,r) for (long long(i) = (l); (i) <= (r); (i)++)
#define Forrl(i,r,l) for (long long(i) = (r); (i) >= (l); (i)--)
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll base = 269;
const int oo = 1 << 30;
const int N = 1e5 + 5;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll P(ll x, ll y) // x^y
{
	ll ret = 1;
	while (y > 0)
	{
		if (y & 1) ret *= x;
		y /= 2;
		x = x * x;
	}
	return ret;
}
ll GCD(ll a, ll b)
{
	return (b ? GCD(b, a%b) : a);
}

int gi() {
	int w = 0; bool q = 1; char c = getchar();
	while ((c<'0' || c>'9') && c != '-') c = getchar();
	if (c == '-') q = 0, c = getchar();
	while (c >= '0'&&c <= '9') w = w * 10 + c - '0', c = getchar();
	return q ? w : -w;
}
namespace io {
	const int L = (1 << 21) + 1;
	char ibuf[L], *iS, *iT, obuf[L], *oS = obuf, *oT = obuf + L - 1, c, st[55]; int f, tp;
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	inline char getc() {
		return gc();
	}
	inline void flush() {
		fwrite(obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); }
	template<class I> inline bool gi(I&x) {
		for (f = 1, c = gc(); c<'0' || c>'9'; c = gc()) if (c == '-') f = -1; else if (c == -1) return false;
		for (x = 0; c <= '9'&&c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return true;
	}
	template<class I> inline void print(I x) {
		if (!x) putc('0');
		if (x < 0) putc('-'), x = -x;
		while (x) st[++tp] = x % 10 + '0', x /= 10;
		while (tp) putc(st[tp--]);
	}
	inline bool gs(char*s, int&l) {
		for (c = gc(); c<'a' || c>'z'; c = gc()) if (c == -1) return false;
		for (l = 0; c <= 'z'&&c >= 'a'; c = gc()) s[l++] = c;
		s[l] = 0;
		return true;
	}
	inline void printld(double x, int digit) {
		if (x < 0) putc('-'), x = -x;
		x += 0.5 * pow(0.1, digit);
		print((ll)x), putc('.');
		for (x -= (ll)x; digit; --digit) x = x * 10, putc((int)x + '0'), x -= (int)x;
	}
	inline void ps(const char*s) { for (int i = 0, n = strlen(s); i < n; i++) putc(s[i]); }
	struct IOFLUSHER { ~IOFLUSHER() { flush(); } } _ioflusher_;
};
using io::getc;
using io::putc;
using io::gi;
using io::gs;
using io::ps;
using io::print;
using io::printld;
void inp_outp()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
/*
MIKUNO - 15/02/1999
*/
ll n, m, q, k, pos, rs, check;
ll a[3 * N], b[3 * N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	scanf("%d", &n);
	For(i, n)
		scanf("%d", &a+i), check += a[i];
	cin >> m;
	For(i, m)
		scanf("%d", &b + i), check -= b[i];
	if (check) return cout << -1, 0;
	ll i = 0, j = 0;
	while (i < n && j < m)
	{
		ll val1 = a[i], val2 = b[j];
		while (val1 < val2)
		{
			i++;
			val1 += a[i];
		}
		while (val2 < val1)
		{
			j++;
			val2 += b[j];
		}
		if (val1 == val2)
			rs++, i++, j++;
	}

	if (!rs) cout << -1;
	else cout << rs;
}


#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<deque>
#include<chrono>
#include<random>
#include<bitset>
#include<assert.h>
#include<cstdio>
#include<string.h>
using namespace std;
ofstream fo("test.out");
ifstream fi("test.inp");
#define For(i,n) for (long long(i) = 0; (i)<(n); (i)++)
#define For1(i,n) for (long long(i) = 1; (i) <= (n); (i)++)
#define Forlr(i,l,r) for (long long(i) = (l); (i) <= (r); (i)++)
#define Forrl(i,r,l) for (long long(i) = (r); (i) >= (l); (i)--)
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll base = 269;
const int oo = 1 << 30;
const int N = 1e5 + 5;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll P(ll x, ll y) // x^y
{
	ll ret = 1;
	while (y > 0)
	{
		if (y & 1) ret *= x;
		y /= 2;
		x = x * x;
	}
	return ret;
}
ll GCD(ll a, ll b)
{
	return (b ? GCD(b, a%b) : a);
}

int gi() {
	int w = 0; bool q = 1; char c = getchar();
	while ((c<'0' || c>'9') && c != '-') c = getchar();
	if (c == '-') q = 0, c = getchar();
	while (c >= '0'&&c <= '9') w = w * 10 + c - '0', c = getchar();
	return q ? w : -w;
}
namespace io {
	const int L = (1 << 21) + 1;
	char ibuf[L], *iS, *iT, obuf[L], *oS = obuf, *oT = obuf + L - 1, c, st[55]; int f, tp;
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	inline char getc() {
		return gc();
	}
	inline void flush() {
		fwrite(obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); }
	template<class I> inline bool gi(I&x) {
		for (f = 1, c = gc(); c<'0' || c>'9'; c = gc()) if (c == '-') f = -1; else if (c == -1) return false;
		for (x = 0; c <= '9'&&c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return true;
	}
	template<class I> inline void print(I x) {
		if (!x) putc('0');
		if (x < 0) putc('-'), x = -x;
		while (x) st[++tp] = x % 10 + '0', x /= 10;
		while (tp) putc(st[tp--]);
	}
	inline bool gs(char*s, int&l) {
		for (c = gc(); c<'a' || c>'z'; c = gc()) if (c == -1) return false;
		for (l = 0; c <= 'z'&&c >= 'a'; c = gc()) s[l++] = c;
		s[l] = 0;
		return true;
	}
	inline void printld(double x, int digit) {
		if (x < 0) putc('-'), x = -x;
		x += 0.5 * pow(0.1, digit);
		print((ll)x), putc('.');
		for (x -= (ll)x; digit; --digit) x = x * 10, putc((int)x + '0'), x -= (int)x;
	}
	inline void ps(const char*s) { for (int i = 0, n = strlen(s); i < n; i++) putc(s[i]); }
	struct IOFLUSHER { ~IOFLUSHER() { flush(); } } _ioflusher_;
};
using io::getc;
using io::putc;
using io::gi;
using io::gs;
using io::ps;
using io::print;
using io::printld;
void inp_outp()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
/*
MIKUNO - 15/02/1999
*/
ll n, m, q, k, pos, rs, check;
ll a[3 * N], b[3 * N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n;
	For(i, n)
		cin >> a[i], check+=a[i];
	cin >> m;
	For(i, m)
		cin >> b[i], check-=b[i];
	if (check) return cout << -1, 0;
	ll i = 0, j = 0;
	while (i < n && j < m)
	{
		ll val1 = a[i], val2 = b[j];
		while (val1 < val2)
		{
			i++;
			val1 += a[i];
		}
		while (val2 < val1)
		{
			j++;
			val2 += b[j];
		}
		if (val1 == val2)
			rs++, i++, j++;
	}
	cout << rs;
}


#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<deque>
#include<chrono>
#include<random>
#include<bitset>
#include<assert.h>
#include<cstdio>
#include<string.h>
using namespace std;
ofstream fo("test.out");
ifstream fi("test.inp");
#define For(i,n) for (long long(i) = 0; (i)<(n); (i)++)
#define For1(i,n) for (long long(i) = 1; (i) <= (n); (i)++)
#define Forlr(i,l,r) for (long long(i) = (l); (i) <= (r); (i)++)
#define Forrl(i,r,l) for (long long(i) = (r); (i) >= (l); (i)--)
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll base = 269;
const int oo = 1 << 30;
const int N = 1e5 + 5;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll P(ll x, ll y) // x^y
{
	ll ret = 1;
	while (y > 0)
	{
		if (y & 1) ret *= x;
		y /= 2;
		x = x * x;
	}
	return ret;
}
ll GCD(ll a, ll b)
{
	return (b ? GCD(b, a%b) : a);
}

int gi() {
	int w = 0; bool q = 1; char c = getchar();
	while ((c<'0' || c>'9') && c != '-') c = getchar();
	if (c == '-') q = 0, c = getchar();
	while (c >= '0'&&c <= '9') w = w * 10 + c - '0', c = getchar();
	return q ? w : -w;
}
namespace io {
	const int L = (1 << 21) + 1;
	char ibuf[L], *iS, *iT, obuf[L], *oS = obuf, *oT = obuf + L - 1, c, st[55]; int f, tp;
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	inline char getc() {
		return gc();
	}
	inline void flush() {
		fwrite(obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); }
	template<class I> inline bool gi(I&x) {
		for (f = 1, c = gc(); c<'0' || c>'9'; c = gc()) if (c == '-') f = -1; else if (c == -1) return false;
		for (x = 0; c <= '9'&&c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return true;
	}
	template<class I> inline void print(I x) {
		if (!x) putc('0');
		if (x < 0) putc('-'), x = -x;
		while (x) st[++tp] = x % 10 + '0', x /= 10;
		while (tp) putc(st[tp--]);
	}
	inline bool gs(char*s, int&l) {
		for (c = gc(); c<'a' || c>'z'; c = gc()) if (c == -1) return false;
		for (l = 0; c <= 'z'&&c >= 'a'; c = gc()) s[l++] = c;
		s[l] = 0;
		return true;
	}
	inline void printld(double x, int digit) {
		if (x < 0) putc('-'), x = -x;
		x += 0.5 * pow(0.1, digit);
		print((ll)x), putc('.');
		for (x -= (ll)x; digit; --digit) x = x * 10, putc((int)x + '0'), x -= (int)x;
	}
	inline void ps(const char*s) { for (int i = 0, n = strlen(s); i < n; i++) putc(s[i]); }
	struct IOFLUSHER { ~IOFLUSHER() { flush(); } } _ioflusher_;
};
using io::getc;
using io::putc;
using io::gi;
using io::gs;
using io::ps;
using io::print;
using io::printld;
void inp_outp()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
/*
MIKUNO - 15/02/1999
*/
ll n, m, q, k, pos, rs, check;
ll a[3 * N], b[3 * N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n;
	For(i, n)
		cin >> a[i], check+=a[i];
	cin >> m;
	For(i, n)
		cin >> b[i], check-=b[i];
	if (check) return cout << -1, 0;
	ll i = 0, j = 0;
	while (i < n && j < m)
	{
		ll val1 = a[i], val2 = b[j];
		while (val1 < val2)
		{
			i++;
			val1 += a[i];
		}
		while (val2 < val1)
		{
			j++;
			val2 += b[j];
		}
		if (val1 == val2)
			rs++, i++, j++;
	}
	cout << rs;
}


#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<deque>
#include<chrono>
#include<random>
#include<bitset>
#include<assert.h>
#include<cstdio>
#include<string.h>
using namespace std;
ofstream fo("test.out");
ifstream fi("test.inp");
#define For(i,n) for (long long(i) = 0; (i)<(n); (i)++)
#define For1(i,n) for (long long(i) = 1; (i) <= (n); (i)++)
#define Forlr(i,l,r) for (long long(i) = (l); (i) <= (r); (i)++)
#define Forrl(i,r,l) for (long long(i) = (r); (i) >= (l); (i)--)
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll base = 269;
const int oo = 1 << 30;
const int N = 1e5 + 5;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll P(ll x, ll y) // x^y
{
	ll ret = 1;
	while (y > 0)
	{
		if (y & 1) ret *= x;
		y /= 2;
		x = x * x;
	}
	return ret;
}
ll GCD(ll a, ll b)
{
	return (b ? GCD(b, a%b) : a);
}

int gi() {
	int w = 0; bool q = 1; char c = getchar();
	while ((c<'0' || c>'9') && c != '-') c = getchar();
	if (c == '-') q = 0, c = getchar();
	while (c >= '0'&&c <= '9') w = w * 10 + c - '0', c = getchar();
	return q ? w : -w;
}
namespace io {
	const int L = (1 << 21) + 1;
	char ibuf[L], *iS, *iT, obuf[L], *oS = obuf, *oT = obuf + L - 1, c, st[55]; int f, tp;
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	inline char getc() {
		return gc();
	}
	inline void flush() {
		fwrite(obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); }
	template<class I> inline bool gi(I&x) {
		for (f = 1, c = gc(); c<'0' || c>'9'; c = gc()) if (c == '-') f = -1; else if (c == -1) return false;
		for (x = 0; c <= '9'&&c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return true;
	}
	template<class I> inline void print(I x) {
		if (!x) putc('0');
		if (x < 0) putc('-'), x = -x;
		while (x) st[++tp] = x % 10 + '0', x /= 10;
		while (tp) putc(st[tp--]);
	}
	inline bool gs(char*s, int&l) {
		for (c = gc(); c<'a' || c>'z'; c = gc()) if (c == -1) return false;
		for (l = 0; c <= 'z'&&c >= 'a'; c = gc()) s[l++] = c;
		s[l] = 0;
		return true;
	}
	inline void printld(double x, int digit) {
		if (x < 0) putc('-'), x = -x;
		x += 0.5 * pow(0.1, digit);
		print((ll)x), putc('.');
		for (x -= (ll)x; digit; --digit) x = x * 10, putc((int)x + '0'), x -= (int)x;
	}
	inline void ps(const char*s) { for (int i = 0, n = strlen(s); i < n; i++) putc(s[i]); }
	struct IOFLUSHER { ~IOFLUSHER() { flush(); } } _ioflusher_;
};
using io::getc;
using io::putc;
using io::gi;
using io::gs;
using io::ps;
using io::print;
using io::printld;
void inp_outp()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
/*
MIKUNO - 15/02/1999
*/
ll n, m, q, k, pos, rs, check;
ll a[3 * N], b[3 * N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n;
	For(i, n)
		cin >> a[i], check+=a[i];
	cin >> m;
	For(i, m)
		cin >> b[i], check-=b[i];
	if (check) return cout << -1, 0;
	ll i = 0, j = 0;
	while (i < n && j < m)
	{
		ll val1 = a[i], val2 = b[j];
		while (val1 < val2)
		{
			i++;
			val1 += a[i];
		}
		while (val2 < val1)
		{
			j++;
			val2 += b[j];
		}
		if (val1 == val2)
			rs++, i++, j++;
	}
	i = n - 1, j = m - 1;
	ll ans = 0;
	while (i >=0 && j >=0)
	{
		ll val1 = a[i], val2 = b[j];
		while (val1 < val2)
		{
			i--;
			val1 += a[i];
		}
		while (val2 < val1)
		{
			j--;
			val2 += b[j];
		}
		if (val1 == val2)
			ans++, i--, j--;
	}
	if (!rs && ! ans) cout << -1;
	else cout << max(rs, ans);
}


#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<deque>
#include<chrono>
#include<random>
#include<bitset>
#include<assert.h>
#include<cstdio>
#include<string.h>
using namespace std;
ofstream fo("test.out");
ifstream fi("test.inp");
#define For(i,n) for (long long(i) = 0; (i)<(n); (i)++)
#define For1(i,n) for (long long(i) = 1; (i) <= (n); (i)++)
#define Forlr(i,l,r) for (long long(i) = (l); (i) <= (r); (i)++)
#define Forrl(i,r,l) for (long long(i) = (r); (i) >= (l); (i)--)
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll base = 269;
const int oo = 1 << 30;
const int N = 1e5 + 5;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll P(ll x, ll y) // x^y
{
	ll ret = 1;
	while (y > 0)
	{
		if (y & 1) ret *= x;
		y /= 2;
		x = x * x;
	}
	return ret;
}
ll GCD(ll a, ll b)
{
	return (b ? GCD(b, a%b) : a);
}

int gi() {
	int w = 0; bool q = 1; char c = getchar();
	while ((c<'0' || c>'9') && c != '-') c = getchar();
	if (c == '-') q = 0, c = getchar();
	while (c >= '0'&&c <= '9') w = w * 10 + c - '0', c = getchar();
	return q ? w : -w;
}
namespace io {
	const int L = (1 << 21) + 1;
	char ibuf[L], *iS, *iT, obuf[L], *oS = obuf, *oT = obuf + L - 1, c, st[55]; int f, tp;
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	inline char getc() {
		return gc();
	}
	inline void flush() {
		fwrite(obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); }
	template<class I> inline bool gi(I&x) {
		for (f = 1, c = gc(); c<'0' || c>'9'; c = gc()) if (c == '-') f = -1; else if (c == -1) return false;
		for (x = 0; c <= '9'&&c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return true;
	}
	template<class I> inline void print(I x) {
		if (!x) putc('0');
		if (x < 0) putc('-'), x = -x;
		while (x) st[++tp] = x % 10 + '0', x /= 10;
		while (tp) putc(st[tp--]);
	}
	inline bool gs(char*s, int&l) {
		for (c = gc(); c<'a' || c>'z'; c = gc()) if (c == -1) return false;
		for (l = 0; c <= 'z'&&c >= 'a'; c = gc()) s[l++] = c;
		s[l] = 0;
		return true;
	}
	inline void printld(double x, int digit) {
		if (x < 0) putc('-'), x = -x;
		x += 0.5 * pow(0.1, digit);
		print((ll)x), putc('.');
		for (x -= (ll)x; digit; --digit) x = x * 10, putc((int)x + '0'), x -= (int)x;
	}
	inline void ps(const char*s) { for (int i = 0, n = strlen(s); i < n; i++) putc(s[i]); }
	struct IOFLUSHER { ~IOFLUSHER() { flush(); } } _ioflusher_;
};
using io::getc;
using io::putc;
using io::gi;
using io::gs;
using io::ps;
using io::print;
using io::printld;
void inp_outp()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
/*
MIKUNO - 15/02/1999
*/
ll n, m, q, k, pos, rs, check, check1;
ll a[3 * N], b[3 * N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n;
	For(i, n)
		cin >> a[i], check += a[i];
	cin >> m;
	For(i, m)
		cin >> b[i], check -= b[i];
	if (check) return cout << -1, 0;
	ll i = 0, j = 0;
	ll val1 = a[0], val2 = b[0];
	while (i < n && j < m)
	{
		while (val1 < val2)
		{
			i++;
			val1 += a[i];
		}
		while (val2 < val1)
		{
			j++;
			val2 += b[j];
		}
		if (val1 == val2)
			rs++, i++, j++, val1 = a[i], val2 = b[j];
	}
	cout << rs;
}


#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<deque>
#include<chrono>
#include<random>
#include<bitset>
#include<assert.h>
#include<cstdio>
#include<string.h>
using namespace std;
ofstream fo("test.out");
ifstream fi("test.inp");
#define For(i,n) for (long long(i) = 0; (i)<(n); (i)++)
#define For1(i,n) for (long long(i) = 1; (i) <= (n); (i)++)
#define Forlr(i,l,r) for (long long(i) = (l); (i) <= (r); (i)++)
#define Forrl(i,r,l) for (long long(i) = (r); (i) >= (l); (i)--)
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll base = 269;
const int oo = 1 << 30;
const int N = 1e5 + 5;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll P(ll x, ll y) // x^y
{
	ll ret = 1;
	while (y > 0)
	{
		if (y & 1) ret *= x;
		y /= 2;
		x = x * x;
	}
	return ret;
}
ll GCD(ll a, ll b)
{
	return (b ? GCD(b, a%b) : a);
}

int gi() {
	int w = 0; bool q = 1; char c = getchar();
	while ((c<'0' || c>'9') && c != '-') c = getchar();
	if (c == '-') q = 0, c = getchar();
	while (c >= '0'&&c <= '9') w = w * 10 + c - '0', c = getchar();
	return q ? w : -w;
}
namespace io {
	const int L = (1 << 21) + 1;
	char ibuf[L], *iS, *iT, obuf[L], *oS = obuf, *oT = obuf + L - 1, c, st[55]; int f, tp;
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	inline char getc() {
		return gc();
	}
	inline void flush() {
		fwrite(obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); }
	template<class I> inline bool gi(I&x) {
		for (f = 1, c = gc(); c<'0' || c>'9'; c = gc()) if (c == '-') f = -1; else if (c == -1) return false;
		for (x = 0; c <= '9'&&c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return true;
	}
	template<class I> inline void print(I x) {
		if (!x) putc('0');
		if (x < 0) putc('-'), x = -x;
		while (x) st[++tp] = x % 10 + '0', x /= 10;
		while (tp) putc(st[tp--]);
	}
	inline bool gs(char*s, int&l) {
		for (c = gc(); c<'a' || c>'z'; c = gc()) if (c == -1) return false;
		for (l = 0; c <= 'z'&&c >= 'a'; c = gc()) s[l++] = c;
		s[l] = 0;
		return true;
	}
	inline void printld(double x, int digit) {
		if (x < 0) putc('-'), x = -x;
		x += 0.5 * pow(0.1, digit);
		print((ll)x), putc('.');
		for (x -= (ll)x; digit; --digit) x = x * 10, putc((int)x + '0'), x -= (int)x;
	}
	inline void ps(const char*s) { for (int i = 0, n = strlen(s); i < n; i++) putc(s[i]); }
	struct IOFLUSHER { ~IOFLUSHER() { flush(); } } _ioflusher_;
};
using io::getc;
using io::putc;
using io::gi;
using io::gs;
using io::ps;
using io::print;
using io::printld;
void inp_outp()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
/*
MIKUNO - 15/02/1999
*/
ll n, m, q, k, pos, rs, check;
ll a[3 * N], b[3 * N];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	scanf_s("%d", &n);
	For(i, n)
		scanf_s("%d", &a[i]), check += a[i];
	cin >> m;
	For(i, m)
		scanf_s("%d", &b[i]), check -= b[i];
	if (check) return cout << -1, 0;
	ll i = 0, j = 0;
	while (i < n && j < m)
	{
		ll val1 = a[i], val2 = b[j];
		while (val1 < val2)
		{
			i++;
			val1 += a[i];
		}
		while (val2 < val1)
		{
			j++;
			val2 += b[j];
		}
		if (val1 == val2)
			rs++, i++, j++;
	}

	if (!rs) cout << -1;
	else cout << rs;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
int main()
{
    boost
	ll n,i,m,j;
	cin>>n;
	ll a[n];
	for(i=0;i<n;i++)
	    cin>>a[i];
	ll suma=0,sumb=0;
	cin>>m;
	ll b[m];

	for(i=0;i<m;i++)
	    cin>>b[i];
	for(i=0;i<n;i++)
	suma+=a[i];
	for(i=0;i<m;i++)
	sumb+=b[i];
	//cout<<suma<<" "<<sumb;
	if(suma!=sumb)
	{
	    cout<<-1;return 0;
	}

	ll l=0,r=0,ans=0;
    suma=a[0];sumb=b[0];
	while(l<n and r<m)
	{
	    //cout<<suma<<" "<<sumb<<endl;
	    if(suma==sumb)
	    {
	        ans++;
	        l++;r++;
	        suma+=a[l];
	        sumb+=b[r];
	    }
	    else if(suma>sumb)
	    {
	       r++;
	        sumb+=b[r];

	    }
	    else if(suma<sumb)
	    {
	       l++;
	        suma+=a[l];

	    }

	}
	if(ans==0)
	cout<<-1;
	else
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
int main()
{
    boost
	ll n,m,i,j;
	cin>>n;
	ll a[n];

	for(i=0;i<n;i++)
	    cin>>a[i];

	cin>>m;
	ll b[m];

	for(i=0;i<m;i++)
	    cin>>b[i];

	ll pre1[n],pre2[m];
	pre1[0]=a[0];
	pre2[0]=b[0];
	for(i=1;i<n;i++)
	pre1[i]=a[i]+pre1[i-1];
	for(i=1;i<m;i++)
	pre2[i]=b[i]+pre2[i-1];
	unordered_map<ll,ll>mp;
	for(i=0;i<n;i++)
	mp[pre1[i]]++;
	for(i=0;i<m;i++)
	mp[pre2[i]]++;
	ll ans=0;
	for(i=0;i<n;i++)
	{
	    if(mp[pre1[i]]>1)
	    ans++;
	    //cout<<pre1[i]<<" "<<pre2[i]<<endl;
	}
	if(ans==0)
	cout<<-1;
	else
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	ll n,m,i,j;
	cin>>n;
	ll a[n];

	for(i=0;i<n;i++)
	    cin>>a[i];

	cin>>m;
	ll b[m];

	for(i=0;i<m;i++)
	    cin>>b[i];

	ll pre1[n],pre2[m];
	pre1[0]=a[0];
	pre2[0]=b[0];
	for(i=1;i<n;i++)
	pre1[i]=a[i]+pre1[i-1];
	for(i=1;i<m;i++)
	pre2[i]=b[i]+pre2[i-1];
	unordered_map<ll,ll>mp;
	for(i=0;i<n;i++)
	mp[pre1[i]]++;
	for(i=0;i<m;i++)
	mp[pre2[i]]++;
	ll ans=0;
	for(i=0;i<n;i++)
	{
	    if(mp[pre1[i]]>1)
	    ans++;
	    //cout<<pre1[i]<<" "<<pre2[i]<<endl;
	}
	if(ans==0)
	cout<<-1;
	else
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	ll n,m,i,j;
	cin>>n;
	ll a[n];

	for(i=0;i<n;i++)
	    cin>>a[i];

	cin>>m;
	ll b[m];

	for(i=0;i<m;i++)
	    cin>>b[i];

	unordered_map<ll,ll>mp;
	for(i=0;i<n;i++)
	mp[a[i]]++;
	for(i=0;i<m;i++)
	mp[b[i]]++;
	ll ans=0;
	for(i=0;i<n;i++)
	{
	    if(mp[a[i]]>1)
	    ans++;
	    //cout<<pre1[i]<<" "<<pre2[i]<<endl;
	}
	if(ans==0)
	cout<<-1;
	else
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
	ll n,i,m,j;
	cin>>n;
	ll a[n];
	for(i=0;i<n;i++)
	    cin>>a[i];
	ll suma=0,sumb=0;
	cin>>m;
	ll b[m];

	for(i=0;i<m;i++)
	    cin>>b[i];
	for(i=0;i<n;i++)
	suma+=a[i];
	for(i=0;i<m;i++)
	sumb+=b[i];
	//cout<<suma<<" "<<sumb;
	if(suma!=sumb)
	{
	    cout<<-1;return 0;
	}

	ll l=0,r=0,ans=0;
    suma=a[0];sumb=b[0];
	while(l<n and r<m)
	{
	    //cout<<suma<<" "<<sumb<<endl;
	    if(suma==sumb)
	    {
	        ans++;
	        l++;r++;
	        suma+=a[l];
	        sumb+=b[r];
	    }
	    else if(suma>sumb)
	    {
	       r++;
	        sumb+=b[r];

	    }
	    else if(suma<sumb)
	    {
	       l++;
	        suma+=a[l];

	    }

	}
	if(ans==0)
	cout<<-1;
	else
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
int main()
{
    boost
	ll n,m,i,j;
	cin>>n;
	ll a[n];

	for(i=0;i<n;i++)
	    cin>>a[i];

	cin>>m;
	ll b[m];

	for(i=0;i<m;i++)
	    cin>>b[i];

	ll pre1[n],pre2[m];

	pre1[0]=a[0];
	pre2[0]=b[0];

	for(i=1;i<n;i++)
	pre1[i]=a[i]+pre1[i-1];

	for(i=1;i<m;i++)
	pre2[i]=b[i]+pre2[i-1];

	unordered_map<ll,ll>mp,mp2;

	for(i=0;i<n;i++)
	mp[pre1[i]]++;

	for(i=0;i<m;i++)
	mp[pre2[i]]++;

	ll ans1=0,ans2=0,ans=0;
	for(i=0;i<n;i++)
	{
	    if(mp[pre1[i]]>1)
	    ans1++;
	    //cout<<pre1[i]<<" "<<pre2[i]<<endl;
	}
	ll suf[n],suf2[m];
	suf[n-1]=a[n-1];suf2[m-1]=b[m-1];

	for(i=n-2;i>=0;i--)
	    suf[i]=suf[i+1]+a[i];

	for(i=m-2;i>=0;i--)
	    suf2[i]=suf2[i+1]+b[i];

	for(i=0;i<n;i++)
	    mp2[suf[i]]++;

	for(i=0;i<m;i++)
	    mp2[suf2[i]]++;

	 for(i=0;i<n;i++)
	 {
	     if(mp2[suf[i]]>1)
	     ans2++;
	 }
	ans=max(ans1,ans2);
	if(ans==0)
	cout<<-1;
	else
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
int main()
{
    boost
	ll n,m,i,j;
	cin>>n;
	ll a[n];

	for(i=0;i<n;i++)
	    cin>>a[i];
	ll suma=0,sumb=0;

	cin>>m;
	ll b[m];

	for(i=0;i<m;i++)
	    cin>>b[i];

	for(i=0;i<n;i++)
	suma+=a[i];

	for(i=0;i<m;i++)
	sumb+=b[i];

	if(suma!=sumb)
	{
	    cout<<-1;
	    return 0;
	}

	ll pre1[n],pre2[m];

	pre1[0]=a[0];
	pre2[0]=b[0];

	for(i=1;i<n;i++)
	pre1[i]=a[i]+pre1[i-1];

	for(i=1;i<m;i++)
	pre2[i]=b[i]+pre2[i-1];

	unordered_map<ll,ll>mp,mp2;

	for(i=0;i<n;i++)
	mp[pre1[i]]++;

	for(i=0;i<m;i++)
	mp[pre2[i]]++;

	ll ans1=0,ans2=0,ans=0;
	for(i=0;i<n;i++)
	{
	    if(mp[pre1[i]]>1)
	    ans1++;
	    //cout<<pre1[i]<<" "<<pre2[i]<<endl;
	}
	ll suf[n],suf2[m];
	suf[n-1]=a[n-1];suf2[m-1]=b[m-1];

	for(i=n-2;i>=0;i--)
	    suf[i]=suf[i+1]+a[i];

	for(i=m-2;i>=0;i--)
	    suf2[i]=suf2[i+1]+b[i];

	for(i=0;i<n;i++)
	    mp2[suf[i]]++;

	for(i=0;i<m;i++)
	    mp2[suf2[i]]++;

	 for(i=0;i<n;i++)
	 {
	     if(mp2[suf[i]]>1)
	     ans2++;
	 }
	ans=max(ans1,ans2);
	if(ans==0)
	cout<<-1;
	else
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
int main()
{
    boost
	ll n,m,i,j;
	cin>>n;
	ll a[n];

	for(i=0;i<n;i++)
	    cin>>a[i];

	cin>>m;
	ll b[m];

	for(i=0;i<m;i++)
	    cin>>b[i];
	ll suma=0,sumb=0;
	for(i=0;i<n;i++)
	suma+=a[i];

	for(i=0;i<m;i++)
	sumb+=b[i];

	if(suma!=sumb)
	{
	    cout<<-1;
	    return 0;
	}
	ll pre1[n],pre2[m];
	pre1[0]=a[0];
	pre2[0]=b[0];
	for(i=1;i<n;i++)
	pre1[i]=a[i]+pre1[i-1];
	for(i=1;i<m;i++)
	pre2[i]=b[i]+pre2[i-1];
	unordered_map<ll,ll>mp;
	for(i=0;i<n;i++)
	mp[pre1[i]]++;
	for(i=0;i<m;i++)
	mp[pre2[i]]++;
	ll ans=0;
	for(i=0;i<n;i++)
	{
	    if(mp[pre1[i]]>1)
	    ans++;
	    //cout<<pre1[i]<<" "<<pre2[i]<<endl;
	}
	if(ans==0)
	cout<<-1;
	else
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
int main()
{

 int n;
 cin>>n;
 int a[n];
 ll as=0;
// ll cum1[n+1]={};
vector<ll> cum1;
 for(int i=0;i<n;i++){
    cin>>a[i];
   // cum1[i+1]=a[i];
    as+=a[i];
    cum1.pb(as);
 }
 int m;
 cin>>m;
 int b[m];
 ll bs=0;
// ll cum2[m+1]={};
 int ans=0;
 for(int i=0;i<m;i++){
    cin>>b[i];
    //cum2[i+1]=b[i];
    bs+=b[i];
     if(binary_search(cum1.begin(),cum1.end(),bs))
    ans++;
 }
    if(bs!=as)
        cout<<-1;
    else
          cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{

 int n;
 cin>>n;
 int a[n];
 ll as=0;
 int cum1[n+1]={};
 for(int i=0;i<n;i++){
    cin>>a[i];
    cum1[i+1]=a[i];
    as+=a[i];
 }
 for(int i=2;i<=n;i++)
    cum1[i]+=cum1[i-1];
 int m;
 cin>>m;
 int b[m];
 ll bs=0;
 int cum2[m+1]={};
 for(int i=0;i<m;i++){
    cin>>b[i];
    cum2[i+1]=b[i];
    bs+=b[i];
 }
 for(int i=2;i<=m;i++)
    cum2[i]+=cum2[i-1];
    if(bs!=as)
        cout<<-1;
    else{
            int ans=0;
         if(n<m){
            for(int i=1;i<=n;i++){
                if(binary_search(cum2+1,cum2+m+1,cum1[i]))
                    ans++;
            }
         }
         else{
            for(int i=1;i<=m;i++){
                if(binary_search(cum1+1,cum1+n+1,cum2[i]))
                    ans++;
            }
         }
          cout<<ans;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{

 int n;
 cin>>n;
 int a[n];
 ll as=0;
 ll cum1[n+1]={};
 for(int i=0;i<n;i++){
    cin>>a[i];
    cum1[i+1]=a[i];
    as+=a[i];
 }
 for(int i=2;i<=n;i++)
    cum1[i]+=cum1[i-1];
 int m;
 cin>>m;
 int b[m];
 ll bs=0;
// ll cum2[m+1]={};
 int ans=0;
 for(int i=0;i<m;i++){
    cin>>b[i];
    //cum2[i+1]=b[i];
    bs+=b[i];
     if(binary_search(cum1,cum1+n+1,bs))
    ans++;
 }
    if(bs!=as)
        cout<<-1;
    else
          cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
int main()
{
ios_base::sync_with_stdio(0);
    cin.tie(0);
 int n;
 cin>>n;
 ll as=0;
 ll x;
vector<ll> cum1;
 for(int i=0;i<n;i++){
    cin>>x;
    as+=x;
    cum1.pb(as);
 }
 int m;
 cin>>m;
 ll bs=0;
 int ans=0;
 ll y;
 for(int i=0;i<m;i++){
    cin>>y;
    bs+=y;
     if(binary_search(cum1.begin(),cum1.end(),bs))
    ans++;
 }
    if(bs!=as)
        cout<<-1;
    else
          cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
/*#define neig(u, e,v) for(int e=head[u],v;v=to[e],~e;e=nxt[e])
const int N=1e6+1,M=1e6,OO=0x3f3f3f3f;
int ne,head[N],nxt[M],to[M];
int n,m,x,y,vis[N];

void addEdge(int f,int t){
    nxt[ne]=head[f];
    to[ne]=t;
    head[f]=ne++;
}
void addBiEdge(int a,int b){
    addEdge(a,b);
    addEdge(b,a);
}

void init(){
      memset(head,-1,n*sizeof head[0]);
      ne=0;
}
bool cmp(pair<int,int> &a,pair<int,int>&b)
  return a.second<b.second;*/
int main()
{

 int n;
 cin>>n;
// int a[n];
 ll as=0;
 ll x;
// ll cum1[n+1]={};
vector<ll> cum1;
 for(int i=0;i<n;i++){
    cin>>x;
   // cum1[i+1]=a[i];
    as+=x;
    cum1.pb(as);
 }
 int m;
 cin>>m;
 //int b[m];
 ll bs=0;
// ll cum2[m+1]={};
 int ans=0;
 ll y;
 for(int i=0;i<m;i++){
    cin>>y;
    //cum2[i+1]=b[i];
    bs+=y;
     if(binary_search(cum1.begin(),cum1.end(),bs))
    ans++;
 }
    if(bs!=as)
        cout<<-1;
    else
          cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
/*#define neig(u, e,v) for(int e=head[u],v;v=to[e],~e;e=nxt[e])
const int N=1e6+1,M=1e6,OO=0x3f3f3f3f;
int ne,head[N],nxt[M],to[M];
int n,m,x,y,vis[N];

void addEdge(int f,int t){
    nxt[ne]=head[f];
    to[ne]=t;
    head[f]=ne++;
}
void addBiEdge(int a,int b){
    addEdge(a,b);
    addEdge(b,a);
}

void init(){
      memset(head,-1,n*sizeof head[0]);
      ne=0;
}
bool cmp(pair<int,int> &a,pair<int,int>&b)
  return a.second<b.second;*/
int main()
{
 int n;
 cin>>n;
 int a[n];
 ll as=0;
 int cum1[n+1]={};
 for(int i=0;i<n;i++){
    cin>>a[i];
    as+=a[i];
 }
 for(int i=1;i<=n;i++)
    cum1[i]+=cum1[i-1];
 int m;
 cin>>m;
 int b[m];
 ll bs=0;
 int cum2[m+1]={};
 for(int i=0;i<m;i++){
    cin>>b[i];
    bs+=b[i];
 }
 for(int i=1;i<=m;i++)
    cum2[i]+=cum2[i-1];
    if(bs!=as)
        cout<<-1;
    else{
            int ans=0;
         if(n<m){
            for(int i=1;i<=n;i++){
                if(binary_search(cum2+1,cum2+m+1,cum1[i]))
                    ans++;
            }
         }
         else{
            for(int i=1;i<=m;i++){
                if(binary_search(cum1+1,cum1+n+1,cum2[i]))
                    ans++;
            }
         }
          cout<<ans;
    }

    return 0;
}