import java.util.LinkedList;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		try (Scanner in = new Scanner(System.in)) {
			while (in.hasNext()) {
				LinkedList<Integer> p1 = new LinkedList<>();
				LinkedList<Integer> p2 = new LinkedList<>();
				try (Scanner line = new Scanner(in.nextLine())) {
					while (line.hasNext()) p1.add(line.nextInt());
				}
				try (Scanner line = new Scanner(in.nextLine())) {
					while (line.hasNext()) p2.add(line.nextInt());
				}
				System.out.println(play(p1, p2));
			}
		}
	}
	/**
	 * Play a full game of war on two decks
	 * @param a
	 * @param b
	 * @return
	 */
	public static int play(LinkedList<Integer> a, LinkedList<Integer> b) {
		while (true) {
			//System.out.printf("%s%n%s%n%n",a,b);
			if (a.size() == 0 && b.size() == 0) return 0;
			else if (a.size() == 0) return 2;
			else if (b.size() == 0) return 1;
			int ax = a.removeFirst();
			int bx = b.removeFirst();
			if (ax>bx) { // check normal battle conditions
				a.addLast(ax);
				a.addLast(bx);
			} else if (bx>ax) {
				b.addLast(bx);
				b.addLast(ax);
			} else {
				int warResult = war(a, b); // ax == bx
				if (warResult >= 0) return warResult;
				else if (warResult == -1) {
					a.add(ax); // Don't forget to re-add the tied cards!
					a.add(bx);
				} else if (warResult == -2) {
					b.add(bx);
					b.add(ax);
				} else throw new RuntimeException("wtf");
			}
		}
	}
	/**
	 * Execute a war (after a tied battle).
	 * @param a player 1's deck (having removed tying cards if this is a top-level war)
	 * @param b player 2's deck (having removed tying cards if this is a top-level war)
	 * @return non-negative result indicates game-over, negative result indicates only this war's result
	 */
	public static int war(LinkedList<Integer> a, LinkedList<Integer> b) {
		if (a.size() == 0 && b.size() == 0) return 0;
		else if (a.size() == 0) return 2;
		else if (b.size() == 0) return 1;
		LinkedList<Integer> aSoldier = new LinkedList<>();
		LinkedList<Integer> bSoldier = new LinkedList<>();
		int n = min(4,min(a.size(),b.size()))-1; //This variable used to be in the for loop declaration. Since a and b change sizes, this was causing bugs that ultimately led to cards being re-added in the wrong order. 
		for (int i=0; i<n; ++i) {
			aSoldier.addLast(a.removeFirst());
			bSoldier.addLast(b.removeFirst());
		}
		int ax = a.removeFirst();
		int bx = b.removeFirst();
		int result = (ax>bx)?(-1):((bx>ax)?(-2):(war(a,b)));
		if (result >= 0) return result;
		int wx,lx;
		LinkedList<Integer> w,wSoldier,lSoldier;
		if (result == -1) {
			wx = ax; lx = bx;
			w = a; 
			wSoldier = aSoldier;
			lSoldier = bSoldier;
		} else if (result == -2) {
			wx = bx; lx = ax;
			w = b;
			wSoldier = bSoldier;
			lSoldier = aSoldier;
		} else throw new RuntimeException("wtf - war");
		while (wSoldier.size() > 0) w.add(wSoldier.removeFirst());
		w.add(wx);
		while (lSoldier.size() > 0) w.add(lSoldier.removeFirst());
		w.add(lx);
		return result;
	}
	public static int min(int a, int b) {
		return ((a<b)?a:b);
	}
}