C. Soldier and Cards

problem_index: C

time_limit: 2 seconds

memory_limit: 256 megabytes

note:
First sample:
![image](images/cf_546_C/1.png)
Second sample:
![image](images/cf_546_C/2.png)

solutions: ["n = int(input())\ns1 = [int(x) for x in input().split()]\ns1.pop(0)\ns2 = [int(x) for x in input().split()]\ns2.pop(0)\nc = 0\nchk = [[False for x in range(11)] for x in range(11)]\nwhile True:\n\tif c > 1000:\n\t\tprint(-1)\n\t\treturn\n\tif not s1 or not s2:\n\t\tprint(c, 1 if not s2 else 2)\n\t\treturn\n\telse:\n\t\tchk[s1[0]][s2[0]] = True\n\t\tif s1[0] > s2[0]:\n\t\t\ts1.append(s2[0])\n\t\t\ts1.append(s1[0])\n\t\t\ts1.pop(0)\n\t\t\ts2.pop(0)\n\t\telse:\n\t\t\ts2.append(s1[0])\n\t\t\ts2.append(s2[0])\n\t\t\ts1.pop(0)\n\t\t\ts2.pop(0)\n\t\tc += 1\n", "n = int(input())\ntemp = list(map(int, input().split()))\nk1 = temp[0]\na = temp[1:].copy()\ntemp = list(map(int, input().split()))\nk2 = temp[0]\nb = temp[1:].copy()\ncount = 0\nl1 = len(a)\nl2 = len(b)\nwhile count < 1000:\n\taTemp = a.pop(0)\n\tbTemp = b.pop(0)\n\tif aTemp > bTemp:\n\t\ta.append(bTemp)\n\t\ta.append(aTemp)\n\t\tl1 += 1\n\t\tl2 -= 1\n\telse:\n\t\tb.append(aTemp)\n\t\tb.append(bTemp)\n\t\tl2 += 1\n\t\tl1 -= 1\n\tcount += 1\n\tif l1 == 0 or l2 == 0:\n\t\tbreak\nif l1 == 0 or l2 == 0:\n\tprint(count, '1' if l2 == 0 else '2')\nelse:\n\tprint(-1)\n", "t = int(input())\nl = list(map(int, input().split()))\nl1 = list(map(int, input().split()))\nk1 = l.pop(0)\nk2 = l1.pop(0)\ngame = 0\nh = 0\nlen10 = 1\nlen20 = 1\nwhile len(l) != 0 and len(l1) != 0:\n\ta = l.pop(0)\n\tb = l1.pop(0)\n\tif a > b:\n\t\tl.append(b)\n\t\tl.append(a)\n\telif b > a:\n\t\tl1.append(a)\n\t\tl1.append(b)\n\tgame += 1\n\tif game > 10000:\n\t\th = 1\n\t\tbreak\n\tif len(l) == 0:\n\t\tlen10 = 0\n\tif len(l1) == 0:\n\t\tlen20 = 0\nif h == 0:\n\tprint(game, end=' ')\n\tif len20:\n\t\tprint(2)\n\tif len10:\n\t\tprint(1)\nelse:\n\tprint(-1)\n", "from bisect import bisect_left\nimport os\nimport sys\nfrom io import BytesIO, IOBase\nfrom collections import Counter, defaultdict\nfrom collections import deque\nfrom functools import cmp_to_key\nimport math\nimport heapq\nimport re\n\ndef sin():\n\treturn input()\n\ndef ain():\n\treturn list(map(int, sin().split()))\n\ndef sain():\n\treturn input().split()\n\ndef iin():\n\treturn int(sin())\nMAX = float('inf')\nMIN = float('-inf')\nMOD = 1000000007\n\ndef sieve(n):\n\tprime = [True for i in range(n + 1)]\n\tp = 2\n\twhile p * p <= n:\n\t\tif prime[p] == True:\n\t\t\tfor i in range(p * p, n + 1, p):\n\t\t\t\tprime[i] = False\n\t\tp += 1\n\ts = set()\n\tfor p in range(2, n + 1):\n\t\tif prime[p]:\n\t\t\ts.add(p)\n\treturn s\n\ndef readTree(n, m):\n\tadj = [deque([]) for _ in range(n + 1)]\n\tfor _ in range(m):\n\t\t(u, v) = ain()\n\t\tadj[u].append(v)\n\t\tadj[v].append(u)\n\treturn adj\n\ndef main():\n\tn = iin()\n\tk1 = ain()\n\tk2 = ain()\n\td1 = deque(k1[1:])\n\td2 = deque(k2[1:])\n\tk1 = k1[0]\n\tk2 = k2[0]\n\ta1 = deque([d1.copy()])\n\ta2 = deque([d2.copy()])\n\tflag = -1\n\tans = 0\n\twhile 1:\n\t\tans += 1\n\t\tx1 = d1.popleft()\n\t\tx2 = d2.popleft()\n\t\tif x1 > x2:\n\t\t\td1.append(x2)\n\t\t\td1.append(x1)\n\t\telse:\n\t\t\td2.append(x1)\n\t\t\td2.append(x2)\n\t\tif len(d1) == 0:\n\t\t\tflag = 2\n\t\t\tbreak\n\t\tif len(d2) == 0:\n\t\t\tflag = 1\n\t\t\tbreak\n\t\tif d1 in a1 and d2 in a2:\n\t\t\tbreak\n\t\ta1.append(d1.copy())\n\t\ta2.append(d2.copy())\n\tif flag == -1:\n\t\tprint(flag)\n\telse:\n\t\tprint(ans, flag)\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\nif os.getcwd() == 'D:\\\\code':\n\tsys.stdin = open('input.txt', 'r')\n\tsys.stdout = open('output.txt', 'w')\nelse:\n\t(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\n\tinput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nmain()\n", "n = int(input())\nA = list(map(int, input().split()))\nA.pop(0)\nB = list(map(int, input().split()))\nB.pop(0)\ncount = 0\nwhile len(A) != 0 and len(B) != 0 and (count <= 10000):\n\tif A[0] < B[0]:\n\t\tB.append(A[0])\n\t\tB.append(B[0])\n\t\tB.pop(0)\n\t\tA.pop(0)\n\t\tcount = count + 1\n\telif A[0] > B[0]:\n\t\tA.append(B[0])\n\t\tA.append(A[0])\n\t\tA.pop(0)\n\t\tB.pop(0)\n\t\tcount = count + 1\nif count >= 10000:\n\tprint(-1)\nelif len(A) == 0:\n\tprint(count, end=' ')\n\tprint(2)\nelse:\n\tprint(count, end=' ')\n\tprint(1)\n", "from math import *\nfrom collections import deque\nfrom copy import deepcopy\nimport sys\n\ndef inp():\n\treturn sys.stdin.readline().rstrip('\\r\\n')\n\ndef multi():\n\treturn map(int, input().split())\n\ndef strmulti():\n\treturn map(str, inp().split())\n\ndef lis():\n\treturn list(map(int, inp().split()))\n\ndef lcm(a, b):\n\treturn a * b // gcd(a, b)\n\ndef ncr(n, r):\n\treturn factorial(n) // (factorial(r) * factorial(max(n - r, 1)))\n\ndef stringlis():\n\treturn list(map(str, inp().split()))\n\ndef out(var):\n\tsys.stdout.write(str(var))\n\ndef printlist(a):\n\tprint(' '.join((str(a[i]) for i in range(len(a)))))\n\ndef isPrime(n):\n\tif n <= 1:\n\t\treturn False\n\tif n <= 3:\n\t\treturn True\n\tif n % 2 == 0 or n % 3 == 0:\n\t\treturn False\n\ti = 5\n\twhile i * i <= n:\n\t\tif n % i == 0 or n % (i + 2) == 0:\n\t\t\treturn False\n\t\ti = i + 6\n\treturn True\nn = int(input())\na = lis()\nb = lis()\nk1 = a[0]\nk2 = b[0]\ninitiala = deepcopy(a[1:])\ninitialb = deepcopy(b[1:])\na = deque(a[1:])\nb = deque(b[1:])\nans = 0\nwhile True:\n\tif ans > 200:\n\t\tprint(-1)\n\t\texit(0)\n\tif len(a) == 0 or len(b) == 0:\n\t\tif len(a) == 0:\n\t\t\tprint(ans, 2)\n\t\telif len(b) == 0:\n\t\t\tprint(ans, 1)\n\t\texit(0)\n\tif a[0] > b[0]:\n\t\tnum1 = a.popleft()\n\t\tnum2 = b.popleft()\n\t\ta.append(num2)\n\t\ta.append(num1)\n\t\tans += 1\n\telse:\n\t\tnum1 = a.popleft()\n\t\tnum2 = b.popleft()\n\t\tb.append(num1)\n\t\tb.append(num2)\n\t\tans += 1\n", "I = lambda : list(map(int, input().split()))\nn = int(input())\nd1 = I()[1:]\nd2 = I()[1:]\nstates = set()\nwhile 1:\n\tif d1[0] > d2[0]:\n\t\td1 = d1[1:] + [d2[0], d1[0]]\n\t\td2 = d2[1:]\n\telse:\n\t\td2 = d2[1:] + [d1[0], d2[0]]\n\t\td1 = d1[1:]\n\tif len(d2) == 0:\n\t\tprint(len(states) + 1, 1)\n\t\tbreak\n\tif len(d1) == 0:\n\t\tprint(len(states) + 1, 2)\n\t\tbreak\n\tstate = ''.join(map(str, d1)) + '|' + ''.join(map(str, d2))\n\tif state in states:\n\t\tprint(-1)\n\t\tbreak\n\tstates.add(state)\n", "n = int(input())\nsol_1 = list(map(int, input().split()))\nl1 = sol_1[0]\ns1 = l1\nsol_1 = sol_1[1:]\nsol_2 = list(map(int, input().split()))\nl2 = sol_2[0]\ns2 = l2\nsol_2 = sol_2[1:]\ni = 0\nj = 0\nsol_11 = []\nsol_22 = []\np = 10000\nc = 0\nwhile s1 != 0 and s2 != 0 and (p > 0):\n\tif i == len(sol_1):\n\t\ti = 0\n\t\tsol_1 = sol_11[:]\n\t\tsol_11 = []\n\tif j == len(sol_2):\n\t\tj = 0\n\t\tsol_2 = sol_22[:]\n\t\tsol_22 = []\n\tif sol_1[i] > sol_2[j]:\n\t\tsol_11.append(sol_2[j])\n\t\tsol_11.append(sol_1[i])\n\t\ts1 += 1\n\t\ts2 -= 1\n\telse:\n\t\tsol_22.append(sol_1[i])\n\t\tsol_22.append(sol_2[j])\n\t\ts2 += 1\n\t\ts1 -= 1\n\ti += 1\n\tj += 1\n\tc += 1\n\tp -= 1\nif p == 0:\n\tprint(-1)\nelif s1 == 0:\n\tprint(c, '2')\nelse:\n\tprint(c, '1')\n", "a = int(input())\nx = list(map(int, input().split(' ')))[1:]\ny = list(map(int, input().split(' ')))[1:]\nnum = 0\nstates = []\nwhile len(x) > 0 and len(y) > 0:\n\ttx = x[0]\n\tty = y[0]\n\tif tx > ty:\n\t\tx = x[1:] + [ty] + [tx]\n\t\ty = y[1:]\n\telse:\n\t\ty = y[1:] + [tx] + [ty]\n\t\tx = x[1:]\n\tnum += 1\n\tstates.append([x, y])\n\tif num > 10 ** 5:\n\t\tif [x, y] in states:\n\t\t\tprint(-1)\n\t\t\tquit()\nif x == []:\n\tprint(num, 2)\nelse:\n\tprint(num, 1)\n", "from collections import deque\nn = int(input())\nl1 = deque(list(map(int, input().split()))[1:])\nl2 = deque(list(map(int, input().split()))[1:])\nres = 0\nwhile l1 and l2:\n\t(c1, c2) = (l1.popleft(), l2.popleft())\n\tif res > 1000:\n\t\tbreak\n\tif c1 > c2:\n\t\tl1.append(c2)\n\t\tl1.append(c1)\n\telse:\n\t\tl2.append(c1)\n\t\tl2.append(c2)\n\tres += 1\nif res > 1000:\n\tprint(-1)\nelif l1:\n\tprint(res, 1)\nelse:\n\tprint(res, 2)\n", "n = int(input())\na1 = [int(x) for x in input().split()][1:]\na2 = [int(x) for x in input().split()][1:]\npos = []\ni = 0\ncur = [a1[1:], a2[1:]]\nwhile not cur in pos and len(a1) > 0 and (len(a2) > 0):\n\tpos.append(cur)\n\tif a1[0] > a2[0]:\n\t\tt = a1[0]\n\t\ta1 = a1[1:]\n\t\ta1 += [a2[0], t]\n\t\ta2 = a2[1:]\n\telse:\n\t\tt = a2[0]\n\t\ta2 = a2[1:]\n\t\ta2 += [a1[0], t]\n\t\ta1 = a1[1:]\n\tcur = [a1[1:], a2[1:]]\n\ti += 1\nif len(a1) == 0:\n\tprint(i)\n\tprint(2)\nelif len(a2) == 0:\n\tprint(i)\n\tprint(1)\nelse:\n\tprint(-1)\n", "from collections import deque\n\ndef main():\n\t(n, d, ans1, ans2) = (int(input()), {}, 0, 0)\n\tdq1 = deque([int(i) for i in input().split()][1:])\n\tdq2 = deque([int(i) for i in input().split()][1:])\n\twhile True:\n\t\tif (tuple(dq1), tuple(dq2)) in d:\n\t\t\tbreak\n\t\telse:\n\t\t\td[tuple(dq1), tuple(dq2)] = 1\n\t\t\t(a, b) = (dq1.popleft(), dq2.popleft())\n\t\t\tans1 += 1\n\t\t\tif a > b:\n\t\t\t\tdq1.append(b)\n\t\t\t\tdq1.append(a)\n\t\t\telse:\n\t\t\t\tdq2.append(a)\n\t\t\t\tdq2.append(b)\n\t\t\tif len(dq1) == 0:\n\t\t\t\tans2 = 2\n\t\t\t\tbreak\n\t\t\telif len(dq2) == 0:\n\t\t\t\tans2 = 1\n\t\t\t\tbreak\n\tif ans2 == 0:\n\t\tprint('-1')\n\telse:\n\t\tprint(ans1, ans2)\nmain()\n", "N = int(input())\nl = list(map(int, input().split()))\nk1 = l[0]\nL = l[1:]\nl = list(map(int, input().split()))\nk2 = l[0]\nR = l[1:]\nse = set([])\nse.add(tuple(L))\nse.add(tuple(R))\nans = 0\nwhile True:\n\tif len(L) == 0 or len(R) == 0:\n\t\tbreak\n\ttop_L = L[0]\n\ttop_R = R[0]\n\tL = L[1:]\n\tR = R[1:]\n\tif top_L > top_R:\n\t\tL = L + [top_R, top_L]\n\telse:\n\t\tR = R + [top_L, top_R]\n\tif tuple(L) in se and tuple(R) in se:\n\t\tprint(-1)\n\t\texit()\n\tse.add(tuple(L))\n\tse.add(tuple(R))\n\tans += 1\nif len(L) == 0:\n\tprint('{} {}'.format(ans, 2))\nelse:\n\tprint('{} {}'.format(ans, 1))\n", "n = int(input())\nk1 = list(map(int, input().split()))[1:]\nk2 = list(map(int, input().split()))[1:]\nkh = 0\nwhile k1 and k2:\n\tif kh > 5000:\n\t\tprint(-1)\n\t\texit()\n\tkh += 1\n\t(a, b) = (k1.pop(0), k2.pop(0))\n\tif a > b:\n\t\tk1 += [b, a]\n\telse:\n\t\tk2 += [a, b]\nif k1:\n\tprint(kh, 1)\nelse:\n\tprint(kh, 2)\n", "from sys import stdin\nfrom collections import defaultdict\nimport sys\nfrom collections import deque\nimport time\nsys.setrecursionlimit(15000)\n\ndef reverse(arr):\n\tc = []\n\tfor i in range(len(arr) - 1, 0, -1):\n\t\tc.append(arr[i])\n\treturn c\nn = int(stdin.readline().strip())\narr1 = list(map(int, stdin.readline().split()))\narr2 = list(map(int, stdin.readline().split()))\narr1 = reverse(arr1)\nfirst = deque(arr1)\narr2 = reverse(arr2)\nsecond = deque(arr2)\ncount = 0\ninitial = time.time()\nwhile time.time() - initial < 1.48 and len(first) > 0 and (len(second) > 0):\n\tcount += 1\n\tone = first.pop()\n\ttwo = second.pop()\n\tif one >= two:\n\t\tfirst.appendleft(two)\n\t\tfirst.appendleft(one)\n\telse:\n\t\tsecond.appendleft(one)\n\t\tsecond.appendleft(two)\nif len(first) == 0 and len(second) > 0:\n\tprint(count, 2)\nelif len(first) > 0 and len(second) == 0:\n\tprint(count, 1)\nelse:\n\tprint(-1)\n", "n = input()\ninput_one = input()\ninput_two = input()\nplayer1 = [int(x) for x in input_one.split(' ')]\nplayer2 = [int(x) for x in input_two.split(' ')]\nplayer2.pop(0)\nplayer1.pop(0)\ncount = 0\nstate = []\nstate.append(player1[:])\nstate.append(player2[:])\nwhile True:\n\ta = player1.pop(0)\n\tb = player2.pop(0)\n\tif a > b:\n\t\tcount += 1\n\t\tplayer1.append(b)\n\t\tplayer1.append(a)\n\telse:\n\t\tcount += 1\n\t\tplayer2.append(a)\n\t\tplayer2.append(b)\n\tif not player1:\n\t\tprint(str(count) + ' ' + str(2))\n\t\tbreak\n\tif not player2:\n\t\tprint(str(count) + ' ' + str(1))\n\t\tbreak\n\tif player1 in state and player2 in state:\n\t\tprint('-1')\n\t\tbreak\n\telse:\n\t\tstate.append(player1[:])\n\t\tstate.append(player2[:])\n", "n = int(input())\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\nd = {}\nd[tuple(a), tuple(b)] = True\nres = 0\nwhile len(a) and len(b):\n\tres += 1\n\tif a[0] > b[0]:\n\t\ta.extend([b[0], a[0]])\n\telse:\n\t\tb.extend([a[0], b[0]])\n\ta.pop(0)\n\tb.pop(0)\n\tif (tuple(a), tuple(b)) in d:\n\t\tprint(-1)\n\t\texit()\n\td[tuple(a), tuple(b)] = True\nprint(res, 1 + (len(a) == 0))\n", "import sys\nfrom collections import *\nimport math\ninput = sys.stdin.readline\nn = int(input())\narr1 = [int(x) for x in input().split()]\narr2 = [int(x) for x in input().split()]\nh = Counter()\nk1 = arr1[0]\nq1 = deque(arr1[1:])\nk2 = arr2[0]\nq2 = deque(arr2[1:])\ncount = 0\nh[tuple(q1)] = 1\nh[tuple(q2)] = 1\nwhile q1 and q2:\n\tx = q1.popleft()\n\ty = q2.popleft()\n\tif x > y:\n\t\tq1.append(y)\n\t\tq1.append(x)\n\telse:\n\t\tq2.append(x)\n\t\tq2.append(y)\n\tcount += 1\n\tif h[tuple(q1)] == 1 and h[tuple(q2)] == 1:\n\t\tcount = -1\n\t\tbreak\n\th[tuple(q1)] = 1\n\th[tuple(q2)] = 1\nif count == -1:\n\tprint(count)\nelif q1:\n\tprint(count, 1)\nelse:\n\tprint(count, 2)\n", "n = int(input())\ntmp = [int(i) for i in input().split()]\nan = tmp[0]\na = tmp[1:]\ntmp = [int(i) for i in input().split()]\nbn = tmp[0]\nb = tmp[1:]\ncount = 0\nnever = 0\nwhile True:\n\tav = a.pop(0)\n\tbv = b.pop(0)\n\tif av > bv:\n\t\ta.append(bv)\n\t\ta.append(av)\n\telse:\n\t\tb.append(av)\n\t\tb.append(bv)\n\tcount += 1\n\tif len(a) == 0 or len(b) == 0:\n\t\tbreak\n\tif count > 1000000:\n\t\tnever = 1\n\t\tbreak\nif never == 0:\n\twinner = 1\n\tif len(a) == 0:\n\t\twinner = 2\n\tprint(str(count) + ' ' + str(winner))\nelse:\n\tprint(-1)\n", "n = int(input())\nk1 = list(map(int, input().split()))[1:]\nk2 = list(map(int, input().split()))[1:]\nk1copy = k1.copy()\nk2copy = k2.copy()\nresult = True\nfight = 0\nwinner = 0\ni = 0\nwhile True:\n\tfirst = k1[0]\n\tsecond = k2[0]\n\tk1.pop(0)\n\tk2.pop(0)\n\tfight += 1\n\ti += 1\n\tif first > second:\n\t\tk1.append(second)\n\t\tk1.append(first)\n\telif second > first:\n\t\tk2.append(first)\n\t\tk2.append(second)\n\telse:\n\t\tfight -= 1\n\t\tcontinue\n\tif len(k1) == 0:\n\t\twinner = 2\n\t\tbreak\n\telif len(k2) == 0:\n\t\twinner = 1\n\t\tbreak\n\tif k1 == k1copy and k2 == k2copy:\n\t\tresult = False\n\t\tbreak\n\tif i > 10000:\n\t\tresult = False\n\t\tbreak\nif result:\n\tprint(fight, winner)\nelse:\n\tprint(-1)\n", "ll = lambda : map(int, input().split())\nt = lambda : int(input())\nss = lambda : input()\nlx = lambda x: map(int, input().split(x))\nyy = lambda : print('YES')\nnn = lambda : print('NO')\nfrom math import log10, log2, ceil, factorial as fac, gcd, inf, sqrt, log\nfrom bisect import bisect_right as br, bisect_left as bl\nfrom collections import Counter\nmod = 10 ** 9 + 7\n\ndef f():\n\tn = t()\n\t(k1, *a) = list(ll())\n\t(k2, *b) = list(ll())\n\tq = []\n\tc = 0\n\tq.append([a.copy(), b.copy()])\n\twhile len(a) and len(b):\n\t\tc += 1\n\t\t(x, y) = (a.pop(0), b.pop(0))\n\t\tif x > y:\n\t\t\ta.append(y)\n\t\t\ta.append(x)\n\t\telse:\n\t\t\tb.append(x)\n\t\t\tb.append(y)\n\t\tif [a, b] not in q:\n\t\t\tq.append([a.copy(), b.copy()])\n\t\telse:\n\t\t\tprint(-1)\n\t\t\tbreak\n\telse:\n\t\tprint(c, [1, 2][len(b) > 0])\nf()\n", "from sys import stdin\nfrom collections import deque\nn = int(stdin.readline())\nk1nums = deque(map(int, stdin.readline().split()))\nk2nums = deque(map(int, stdin.readline().split()))\nk1nums.popleft()\nk2nums.popleft()\nflag = 1\nng = 0\nloopcount = 1000\nwhile k1nums and k2nums and loopcount:\n\tloopcount -= 1\n\t(top1, top2) = (k1nums.popleft(), k2nums.popleft())\n\tif top1 > top2:\n\t\tk1nums.append(top2)\n\t\tk1nums.append(top1)\n\telse:\n\t\tk2nums.append(top1)\n\t\tk2nums.append(top2)\n\tng += 1\nif loopcount:\n\tif not k1nums:\n\t\tprint(ng, 2)\n\telse:\n\t\tprint(ng, 1)\nelse:\n\tprint(-1)\n", "input()\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\nc = 0\nfor i in range(100000):\n\tif not a or not b:\n\t\tprint(c, end=' ')\n\t\tprint(2 if not a else 1)\n\t\tbreak\n\tx = a.pop(0)\n\ty = b.pop(0)\n\tif x > y:\n\t\ta.append(y)\n\t\ta.append(x)\n\telse:\n\t\tb.append(x)\n\t\tb.append(y)\n\tc += 1\nelse:\n\tprint(-1)\n", "n = int(input())\nl1 = list(map(int, input().split()))\nl2 = list(map(int, input().split()))\nt1 = l1[1:]\nt2 = l2[1:]\ncnt = 0\nwhile cnt < 10000:\n\tTopT1 = t1.pop(0)\n\tTopT2 = t2.pop(0)\n\tif TopT1 < TopT2:\n\t\tt2.append(TopT1)\n\t\tt2.append(TopT2)\n\telse:\n\t\tt1.append(TopT2)\n\t\tt1.append(TopT1)\n\tcnt += 1\n\tif len(t1) <= 0:\n\t\tprint(cnt, '2')\n\t\texit(0)\n\telif len(t2) <= 0:\n\t\tprint(cnt, '1')\n\t\texit(0)\n\tif t1 == l1[1:] and t2 == l2[1:]:\n\t\tprint(-1)\n\t\texit(0)\nprint(-1)\n", "n = int(input())\nfrom collections import deque\nk1 = list(map(int, input().split()))\nk1 = deque(k1[1:])\nk2 = list(map(int, input().split()))\nk2 = deque(k2[1:])\na = []\nb = []\ncount = 0\nwhile list(k1) not in a or list(k2) not in b:\n\ta.append(list(k1))\n\tb.append(list(k2))\n\tif k1[0] > k2[0]:\n\t\tk1.append(k2[0])\n\t\tk1.append(k1[0])\n\t\tk1.popleft()\n\t\tk2.popleft()\n\telse:\n\t\tk2.append(k1[0])\n\t\tk2.append(k2[0])\n\t\tk1.popleft()\n\t\tk2.popleft()\n\tcount += 1\n\tif len(k2) == 0:\n\t\tprint(count, 1)\n\t\texit(0)\n\telif len(k1) == 0:\n\t\tprint(count, 2)\n\t\texit(0)\nprint(-1)\n", "n = int(input())\na1 = list(map(int, input().split()))\np = a1[0]\na1.pop(0)\na2 = list(map(int, input().split()))\nq = a2[0]\na2.pop(0)\nc = 0\nwhile len(a1) > 0 and len(a2) > 0:\n\tif a1[0] > a2[0]:\n\t\ta1.append(a2[0])\n\t\ta1.append(a1[0])\n\t\ta1.pop(0)\n\t\ta2.pop(0)\n\t\tc += 1\n\telse:\n\t\ta2.append(a1[0])\n\t\ta2.append(a2[0])\n\t\ta1.pop(0)\n\t\ta2.pop(0)\n\t\tc += 1\n\tif c > 100000:\n\t\tprint(-1)\n\t\texit()\nif len(a1) > len(a2):\n\tz = 1\nelse:\n\tz = 2\nprint(c, z)\n", "n = int(input())\nl = list(map(int, input().split()))\nl.pop(0)\nl1 = list(map(int, input().split()))\nl1.pop(0)\nc = 0\nwhile l and l1:\n\tc += 1\n\ta = l.pop(0)\n\tb = l1.pop(0)\n\tif a > b:\n\t\tl.append(b)\n\t\tl.append(a)\n\telif a < b:\n\t\tl1.append(a)\n\t\tl1.append(b)\n\tif c > 1000:\n\t\tbreak\nif c > 1000:\n\tprint(-1)\nelif len(l) < len(l1):\n\tprint(c, 2)\nelse:\n\tprint(c, 1)\n", "n = int(input())\nk1 = input().strip().split()\nk2 = input().strip().split()\nk1 = k1[1:]\nk2 = k2[1:]\nmema = []\nmemb = []\nr = 0\nwhile k1 != [] and k2 != []:\n\tr += 1\n\tmema.append(k1[:])\n\tmemb.append(k2[:])\n\ta = k1[0]\n\tdel k1[0]\n\tb = k2[0]\n\tdel k2[0]\n\tif int(a) > int(b):\n\t\tk1.append(b)\n\t\tk1.append(a)\n\telif int(b) > int(a):\n\t\tk2.append(a)\n\t\tk2.append(b)\n\tif k1 == []:\n\t\tprint(r, 2)\n\telif k2 == []:\n\t\tprint(r, 1)\n\tif k1 in mema and k2 in memb:\n\t\tprint(-1)\n\t\tbreak\n", "n = int(input())\ntemparr = input()\ntemparr = temparr.split()\nplayer1 = []\nplayer2 = []\nfor (j, i) in enumerate(temparr):\n\tif j == 0:\n\t\tcontinue\n\tplayer1.append(int(i))\ntemparr = input()\ntemparr = temparr.split()\nfor (j, i) in enumerate(temparr):\n\tif j == 0:\n\t\tcontinue\n\tplayer2.append(int(i))\nif len(player1) == 0:\n\tprint('0 2')\nelif len(player2) == 0:\n\tprint('0 1')\nelse:\n\tflag = 0\n\tcount = 0\n\twhile True:\n\t\tif len(player1) == 0:\n\t\t\tprint(str(count) + ' 2')\n\t\t\tflag = 1\n\t\t\tbreak\n\t\tif len(player2) == 0:\n\t\t\tprint(str(count) + ' 1')\n\t\t\tflag = 1\n\t\t\tbreak\n\t\tif count == 200:\n\t\t\tbreak\n\t\tif player1[0] > player2[0]:\n\t\t\thighercard = player1[0]\n\t\t\tlowercard = player2[0]\n\t\t\tplayer1.pop(0)\n\t\t\tplayer2.pop(0)\n\t\t\tplayer1.append(lowercard)\n\t\t\tplayer1.append(highercard)\n\t\telse:\n\t\t\thighercard = player2[0]\n\t\t\tlowercard = player1[0]\n\t\t\tplayer1.pop(0)\n\t\t\tplayer2.pop(0)\n\t\t\tplayer2.append(lowercard)\n\t\t\tplayer2.append(highercard)\n\t\tcount += 1\n\tif flag == 0:\n\t\tprint(-1)\n", "n = int(input())\nak = input().split(' ')\nak.remove(ak[0])\nbk = input().split(' ')\nbk.remove(bk[0])\nc = 0\nd = 0\nfor i in range(len(bk)):\n\twhile len(ak) > 0 and len(bk) > 0 and (c < 1000):\n\t\tif int(ak[i]) > int(bk[i]):\n\t\t\ts = ak[i]\n\t\t\tak.append(bk[i])\n\t\t\tak.remove(ak[i])\n\t\t\tak.append(s)\n\t\t\tbk.remove(bk[i])\n\t\t\tc = c + 1\n\t\telse:\n\t\t\tt = bk[i]\n\t\t\tbk.append(ak[i])\n\t\t\tbk.remove(bk[i])\n\t\t\tbk.append(t)\n\t\t\tak.remove(ak[i])\n\t\t\tc = c + 1\nif c > 999:\n\tprint(-1)\nelif len(ak) > len(bk):\n\tprint(c, 1)\nelse:\n\tprint(c, 2)\n", "N = int(input())\nA = list(map(int, input().rstrip().split()))\nB = list(map(int, input().rstrip().split()))\ninitA = A[1:len(A)]\ninitB = B[1:len(B)]\ninitA.reverse()\ninitB.reverse()\ntempA = []\ntempB = []\ntempA.extend(initA)\ntempB.extend(initB)\ncount = 0\nwhile 1 != 0:\n\tif tempA[-1] > tempB[-1]:\n\t\ttempA.insert(0, tempB.pop())\n\t\ttempA.insert(0, tempA.pop())\n\telse:\n\t\ttempB.insert(0, tempA.pop())\n\t\ttempB.insert(0, tempB.pop())\n\tif count == 100:\n\t\ttemptB = []\n\t\ttemptB.extend(tempB)\n\t\ttemptA = []\n\t\ttemptA.extend(tempA)\n\tif tempB == initB and tempA == initA:\n\t\tprint(-1)\n\t\tbreak\n\telif count > 100 and tempA == temptA and (tempB == temptB):\n\t\tprint(-1)\n\t\tbreak\n\telif len(tempB) == 0:\n\t\tprint(count + 1, 1)\n\t\tbreak\n\telif len(tempA) == 0:\n\t\tprint(count + 1, 2)\n\t\tbreak\n\telse:\n\t\tcount += 1\n", "n = int(input())\nh1 = list(map(int, input().split()))\nh2 = list(map(int, input().split()))\nh1.pop(0)\nh2.pop(0)\ns = set()\nres = 0\nwhile len(h1) != 0 and len(h2) != 0:\n\ta = tuple([tuple(h1), tuple(h2)])\n\tif a not in s:\n\t\ts.add(a)\n\telse:\n\t\tprint(-1)\n\t\texit()\n\tc1 = h1.pop(0)\n\tc2 = h2.pop(0)\n\tif c1 > c2:\n\t\th1 += [c2, c1]\n\telse:\n\t\th2 += [c1, c2]\n\tres += 1\nif len(h1) == 0:\n\tprint(str(res) + ' 2')\nelse:\n\tprint(str(res) + ' 1')\n", "import os\nimport sys\nfrom math import *\nfrom collections import *\nfrom bisect import *\nfrom io import BytesIO, IOBase\n\ndef vsInput():\n\tsys.stdin = open('input.txt', 'r')\n\tsys.stdout = open('output.txt', 'w')\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\n\ndef value():\n\treturn tuple(map(int, input().split()))\n\ndef array():\n\treturn [int(i) for i in input().split()]\n\ndef Int():\n\treturn int(input())\n\ndef Str():\n\treturn input()\n\ndef arrayS():\n\treturn [i for i in input().split()]\nn = Int()\na = array()[1:]\nb = array()[1:]\nina = a[:]\ninb = b[:]\nc = 0\nwhile True:\n\tc += 1\n\tif a[0] > b[0]:\n\t\te = b.pop(0)\n\t\te1 = a.pop(0)\n\t\ta.append(e)\n\t\ta.append(e1)\n\telif a[0] < b[0]:\n\t\te = a.pop(0)\n\t\te1 = b.pop(0)\n\t\tb.append(e)\n\t\tb.append(e1)\n\tif len(a) == 0:\n\t\tprint(c, 2)\n\t\texit()\n\telif len(b) == 0:\n\t\tprint(c, 1)\n\t\texit()\n\tif c == 20 * n:\n\t\tprint(-1)\n\t\texit()\n", "n = int(input())\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\ni = 0\ng = 1000000\nwhile i < g and len(a) > 0 and (len(b) > 0):\n\tif a[0] < b[0]:\n\t\tb = b[1:] + a[:1] + b[:1]\n\t\ta = a[1:]\n\telse:\n\t\ta = a[1:] + b[:1] + a[:1]\n\t\tb = b[1:]\n\ti += 1\nif i == g:\n\tprint(-1)\nelif len(a) == 0:\n\tprint(i, 2)\nelse:\n\tprint(i, 1)\n", "def solve(st1, st2):\n\tL = []\n\ts1 = st1[:]\n\ts2 = st2[:]\n\tflag = -1\n\tfights = 0\n\twhile [s1, s2] not in L:\n\t\tx1 = s1[:]\n\t\tx2 = s2[:]\n\t\tL.append([x1, x2])\n\t\tif len(s1) == 0:\n\t\t\tflag = 2\n\t\t\tbreak\n\t\telif len(s2) == 0:\n\t\t\tflag = 1\n\t\t\tbreak\n\t\tif s1[0] > s2[0]:\n\t\t\tx = s2.pop(0)\n\t\t\ty = s1.pop(0)\n\t\t\ts1.append(x)\n\t\t\ts1.append(y)\n\t\telif s1[0] < s2[0]:\n\t\t\tx = s2.pop(0)\n\t\t\ty = s1.pop(0)\n\t\t\ts2.append(y)\n\t\t\ts2.append(x)\n\t\tfights += 1\n\tif flag == -1:\n\t\tprint(flag)\n\telse:\n\t\tprint(fights, flag)\nN = int(input())\nstack1 = list(map(int, input().split()))\nstack2 = list(map(int, input().split()))\nstack1.pop(0)\nstack2.pop(0)\nsolve(stack1, stack2)\n", "from sys import stdin, stdout\nfrom collections import deque\ninput = stdin.readline\n\ndef main():\n\tt = 1\n\n\tdef pos(d1, d2):\n\t\treturn int(''.join(map(str, list(d1) + list(d2))))\n\tfor i in range(t):\n\t\tn = int(input())\n\t\tai = list(map(int, input().split()))[1:]\n\t\tai2 = list(map(int, input().split()))[1:]\n\t\td1 = deque()\n\t\td2 = deque()\n\t\tfor i in ai:\n\t\t\td1.appendleft(i)\n\t\tfor i in ai2:\n\t\t\td2.appendleft(i)\n\t\tp = {}\n\n\t\tdef action():\n\t\t\t(temp, temp2) = (d1.pop(), d2.pop())\n\t\t\tif temp > temp2:\n\t\t\t\td1.appendleft(temp2)\n\t\t\t\td1.appendleft(temp)\n\t\t\telse:\n\t\t\t\td2.appendleft(temp)\n\t\t\t\td2.appendleft(temp2)\n\t\tans = 0\n\t\tans2 = 0\n\t\twhile True:\n\t\t\tans += 1\n\t\t\taction()\n\t\t\tif len(d1) == 0:\n\t\t\t\tans2 = 2\n\t\t\t\tbreak\n\t\t\tif len(d2) == 0:\n\t\t\t\tans2 = 1\n\t\t\t\tbreak\n\t\t\ttemp = pos(d1, d2)\n\t\t\tif temp in p:\n\t\t\t\tprint(-1)\n\t\t\t\treturn\n\t\t\tp[temp] = 1\n\t\tprint(ans, ans2)\nmain()\n", "import collections\ninput()\nfights = 0\nplayer1 = collections.deque(map(int, input().split()))\nplayer2 = collections.deque(map(int, input().split()))\nplayer1.popleft()\nplayer2.popleft()\nwhile len(player1) != 0 and len(player2) != 0:\n\tfights += 1\n\tif fights > 10000:\n\t\tprint(-1)\n\t\texit(0)\n\tcard1 = player1.popleft()\n\tcard2 = player2.popleft()\n\tif card1 > card2:\n\t\tplayer1.append(card2)\n\t\tplayer1.append(card1)\n\telse:\n\t\tplayer2.append(card1)\n\t\tplayer2.append(card2)\nif len(player1) == 0:\n\tprint(fights, 2)\nelse:\n\tprint(fights, 1)\n", "n = int(input())\ncards1 = list(map(int, input().split()))\ncards2 = list(map(int, input().split()))\nk1 = cards1[0]\nk2 = cards2[0]\ncards1 = cards1[1:]\ncards2 = cards2[1:]\nc = 0\nf = 0\nwhile True:\n\ta = cards1.pop(0)\n\tb = cards2.pop(0)\n\tif a < b:\n\t\tcards2.append(a)\n\t\tcards2.append(b)\n\t\tc += 1\n\telse:\n\t\tcards1.append(b)\n\t\tcards1.append(a)\n\t\tc += 1\n\tif c > 500:\n\t\tf = 1\n\t\tbreak\n\tif len(cards1) == 0 or len(cards2) == 0:\n\t\tbreak\nif f == 1:\n\tprint(-1)\nelse:\n\tif len(cards1) > 0:\n\t\twin = 1\n\telse:\n\t\twin = 2\n\tprint(c, win)\n", "import sys\ninput = sys.stdin.readline\nfrom math import ceil, floor, factorial\n\ndef swaparr(arr, a, b):\n\ttemp = arr[a]\n\tarr[a] = arr[b]\n\tarr[b] = temp\n\ndef gcd(a, b):\n\tif a == 0:\n\t\treturn b\n\treturn gcd(b % a, a)\n\ndef nCr(n, k):\n\tif k > n - k:\n\t\tk = n - k\n\tres = 1\n\tfor i in range(k):\n\t\tres = res * (n - i)\n\t\tres = res / (i + 1)\n\treturn int(res)\n\ndef upper_bound(a, x, lo=0):\n\thi = len(a)\n\twhile lo < hi:\n\t\tmid = (lo + hi) // 2\n\t\tif a[mid] < x:\n\t\t\tlo = mid + 1\n\t\telse:\n\t\t\thi = mid\n\treturn lo\n\ndef primefs(n):\n\tprimes = {}\n\twhile n % 2 == 0:\n\t\tprimes[2] = primes.get(2, 0) + 1\n\t\tn = n // 2\n\tfor i in range(3, int(n ** 0.5) + 2, 2):\n\t\twhile n % i == 0:\n\t\t\tprimes[i] = primes.get(i, 0) + 1\n\t\t\tn = n // i\n\tif n > 2:\n\t\tprimes[n] = primes.get(n, 0) + 1\n\treturn primes\n\ndef power(x, y, p):\n\tres = 1\n\tx = x % p\n\tif x == 0:\n\t\treturn 0\n\twhile y > 0:\n\t\tif y & 1 == 1:\n\t\t\tres = res * x % p\n\t\ty = y >> 1\n\t\tx = x * x % p\n\treturn res\n\ndef swap(a, b):\n\ttemp = a\n\ta = b\n\tb = temp\n\treturn (a, b)\n\ndef find(x, link):\n\tp = x\n\twhile p != link[p]:\n\t\tp = link[p]\n\twhile x != p:\n\t\tnex = link[x]\n\t\tlink[x] = p\n\t\tx = nex\n\treturn p\n\ndef union(x, y, link, size):\n\tx = find(x, link)\n\ty = find(y, link)\n\tif size[x] < size[y]:\n\t\t(x, y) = swap(x, y)\n\tif x != y:\n\t\tsize[x] += size[y]\n\t\tlink[y] = x\n\ndef sieve(n):\n\tprime = [True for i in range(n + 1)]\n\tp = 2\n\twhile p * p <= n:\n\t\tif prime[p] == True:\n\t\t\tfor i in range(p * p, n + 1, p):\n\t\t\t\tprime[i] = False\n\t\tp += 1\n\treturn prime\nMAXN = int(1000000.0 + 5)\n\ndef spf_sieve():\n\tspf[1] = 1\n\tfor i in range(2, MAXN):\n\t\tspf[i] = i\n\tfor i in range(4, MAXN, 2):\n\t\tspf[i] = 2\n\tfor i in range(3, ceil(MAXN ** 0.5), 2):\n\t\tif spf[i] == i:\n\t\t\tfor j in range(i * i, MAXN, i):\n\t\t\t\tif spf[j] == j:\n\t\t\t\t\tspf[j] = i\n\ndef factoriazation(x):\n\tret = {}\n\twhile x != 1:\n\t\tret[spf[x]] = ret.get(spf[x], 0) + 1\n\t\tx = x // spf[x]\n\treturn ret\n\ndef int_array():\n\treturn list(map(int, input().strip().split()))\n\ndef str_array():\n\treturn input().strip().split()\nMOD = int(1000000000.0) + 7\nCMOD = 998244353\nINF = float('inf')\nNINF = -float('inf')\nn = int(input())\na = int_array()\nb = int_array()\nk1 = a[0]\na = a[1:]\nk2 = b[0]\nb = b[1:]\ncount = 0\nf = 0\nwon = 2\nwhile 1:\n\tif not a or not b:\n\t\tif not a:\n\t\t\twon = 2\n\t\telse:\n\t\t\twon = 1\n\t\tf = 1\n\t\tbreak\n\tcount += 1\n\tx = a.pop(0)\n\ty = b.pop(0)\n\tif x > y:\n\t\ta.append(y)\n\t\ta.append(x)\n\telse:\n\t\tb.append(x)\n\t\tb.append(y)\n\tif count == 100000.0:\n\t\tbreak\nif f:\n\tprint(*(count, won))\nelse:\n\tprint(-1)\n", "x = int(input())\nA = list(map(int, input().split()))\nA = A[1:]\nD = A\nB = list(map(int, input().split()))\nB = B[1:]\nE = B\nC = []\ncount = 0\nnum = 1000\nwhile num > 0:\n\tif count == 999:\n\t\tstate = 10000\n\t\tbreak\n\tstate = -100\n\tif A == C:\n\t\tstate = 10\n\t\tbreak\n\tif B == C:\n\t\tstate = 100\n\t\tbreak\n\tif A[0] > B[0]:\n\t\tA.append(B[0])\n\t\tA.append(A[0])\n\t\tA = A[1:]\n\t\tB = B[1:]\n\t\tcount = count + 1\n\t\tstate = 0\n\t\tnum = num - 1\n\tif state != 0:\n\t\tif B[0] > A[0]:\n\t\t\tB.append(A[0])\n\t\t\tB.append(B[0])\n\t\t\tB = B[1:]\n\t\t\tA = A[1:]\n\t\t\tcount = count + 1\n\t\t\tnum = num - 1\nif state == 10:\n\tprint(count, 2)\nif state == 100:\n\tprint(count, 1)\nif state == 10000:\n\tprint(-1)\n", "import math\nimport time\nn = int(input())\nonecards = input().split()\nonecards.pop(0)\nfor i in range(len(onecards)):\n\tonecards[i] = int(onecards[i])\ntwocards = input().split()\ntwocards.pop(0)\nfor i in range(len(twocards)):\n\ttwocards[i] = int(twocards[i])\nstartone = []\nfor i in range(len(onecards)):\n\tstartone.append(onecards[i])\nonelength = len(startone)\nstarttwo = []\nfor i in range(len(twocards)):\n\tstarttwo.append(twocards[i])\ntwolength = len(starttwo)\nstart = time.time()\nplays = 0\nwhile True:\n\tcone = onecards.pop(0)\n\tctwo = twocards.pop(0)\n\tif cone > ctwo:\n\t\tonecards.append(ctwo)\n\t\tonecards.append(cone)\n\telse:\n\t\ttwocards.append(cone)\n\t\ttwocards.append(ctwo)\n\tplays = plays + 1\n\tif len(twocards) == 0:\n\t\tprint(str(plays) + ' 1')\n\t\tbreak\n\tif len(onecards) == 0:\n\t\tprint(str(plays) + ' 2')\n\t\tbreak\n\tif onecards == startone and twocards == starttwo or plays > 362880:\n\t\tprint(-1)\n\t\tbreak\nfinish = time.time()\n", "def fact(x):\n\tif x == 0:\n\t\treturn 1\n\telse:\n\t\treturn x * fact(x - 1)\n\ndef lower_bound(li, num):\n\tanswer = -1\n\tstart = 0\n\tend = len(li) - 1\n\twhile start <= end:\n\t\tmiddle = (end + start) // 2\n\t\tif li[middle] >= num:\n\t\t\tanswer = middle\n\t\t\tend = middle - 1\n\t\telse:\n\t\t\tstart = middle + 1\n\treturn answer\n\ndef upper_bound(li, num):\n\tanswer = -1\n\tstart = 0\n\tend = len(li) - 1\n\twhile start <= end:\n\t\tmiddle = (end + start) // 2\n\t\tif li[middle] <= num:\n\t\t\tanswer = middle\n\t\t\tstart = middle + 1\n\t\telse:\n\t\t\tend = middle - 1\n\treturn answer\n\ndef abs(x):\n\treturn x if x >= 0 else -x\n\ndef binary_search(li, val, lb, ub):\n\tans = 0\n\twhile lb <= ub:\n\t\tmid = (lb + ub) // 2\n\t\tif li[mid] > val:\n\t\t\tub = mid - 1\n\t\telif val > li[mid]:\n\t\t\tlb = mid + 1\n\t\telse:\n\t\t\tans = 1\n\t\t\tbreak\n\treturn ans\n\ndef sieve_of_eratosthenes(n):\n\tans = []\n\tarr = [1] * (n + 1)\n\t(arr[0], arr[1], i) = (0, 0, 2)\n\twhile i * i <= n:\n\t\tif arr[i] == 1:\n\t\t\tj = i + i\n\t\t\twhile j <= n:\n\t\t\t\tarr[j] = 0\n\t\t\t\tj += i\n\t\ti += 1\n\tfor k in range(n):\n\t\tif arr[k] == 1:\n\t\t\tans.append(k)\n\treturn ans\n\ndef nc2(x):\n\tif x == 1:\n\t\treturn 0\n\telse:\n\t\treturn x * (x - 1) // 2\n\ndef kadane(x):\n\tsum_so_far = 0\n\tcurrent_sum = 0\n\tfor i in x:\n\t\tcurrent_sum += i\n\t\tif current_sum < 0:\n\t\t\tcurrent_sum = 0\n\t\telse:\n\t\t\tsum_so_far = max(sum_so_far, current_sum)\n\treturn sum_so_far\n\ndef mex(li):\n\tcheck = [0] * 1001\n\tfor i in li:\n\t\tcheck[i] += 1\n\tfor i in range(len(check)):\n\t\tif check[i] == 0:\n\t\t\treturn i\n\ndef sumdigits(n):\n\tans = 0\n\twhile n != 0:\n\t\tans += n % 10\n\t\tn //= 10\n\treturn ans\nfrom math import *\nfrom collections import deque\nmax_steps = 100005\nfor _ in range(1):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tb = list(map(int, input().split()))\n\ts1 = a[0]\n\ts2 = b[0]\n\ta = a[1:]\n\tc = a[:]\n\td = b[1:]\n\tb = d[:]\n\tcnt = 0\n\tsteps = 0\n\twhile len(a) != 0 and len(b) != 0:\n\t\tsteps += 1\n\t\tif steps > max_steps:\n\t\t\tbreak\n\t\t(val1, val2) = (a[0], b[0])\n\t\tif val1 < val2:\n\t\t\ta = a[1:]\n\t\t\tb = b[1:]\n\t\t\tb.append(val1)\n\t\t\tb.append(val2)\n\t\t\tcnt += 1\n\t\telse:\n\t\t\ta = a[1:]\n\t\t\tb = b[1:]\n\t\t\ta.append(val2)\n\t\t\ta.append(val1)\n\t\t\tcnt += 1\n\t\tif a == c and b == d:\n\t\t\tbreak\n\tif len(a) == 0:\n\t\tprint(cnt, 2)\n\telif len(b) == 0:\n\t\tprint(cnt, 1)\n\telse:\n\t\tprint(-1)\n", "n = input()\na = list(map(int, input().split()[1:]))\nb = list(map(int, input().split()[1:]))\ncopy_b = b[:]\ncopy_a = a[:]\ni = 0\nwhile True:\n\ta_x = a.pop(0)\n\tb_x = b.pop(0)\n\tif a_x > b_x:\n\t\ta.append(b_x)\n\t\ta.append(a_x)\n\telse:\n\t\tb.append(a_x)\n\t\tb.append(b_x)\n\tif len(a) == 0:\n\t\tprint(i + 1, 2)\n\t\tbreak\n\tif len(b) == 0:\n\t\tprint(i + 1, 1)\n\t\tbreak\n\tif copy_a == a and copy_b == b and (i != 0) or i > 10000:\n\t\tprint(-1)\n\t\tbreak\n\ti += 1\n", "import logging\nimport sys\nfrom collections import deque\nlogger_cagada = None\nnivel_log = logging.ERROR\n\ndef queue_a_tupla(q):\n\ttupla = tuple(list(q))\n\tlogger_cagada.debug('la tupla de {} es {}'.format(q, tupla))\n\treturn tupla\n\ndef soldado_del_amor_barajea(pila1, pila2, comps_ya_vistas):\n\tif (queue_a_tupla(pila1), queue_a_tupla(pila2)) in comps_ya_vistas:\n\t\treturn False\n\tcomps_ya_vistas.add((queue_a_tupla(pila1), queue_a_tupla(pila2)))\n\telem1 = pila1.popleft()\n\telem2 = pila2.popleft()\n\tlogger_cagada.debug('comparando {} con {}'.format(elem1, elem2))\n\tif elem1 > elem2:\n\t\tpila1.append(elem2)\n\t\tpila1.append(elem1)\n\telse:\n\t\tpila2.append(elem1)\n\t\tpila2.append(elem2)\n\treturn True\n\ndef soldado_del_amor_core(nums1, nums2):\n\tq1 = deque(nums1)\n\tq2 = deque(nums2)\n\tnum_vueltas = 0\n\tcomps_ya_vistas = set()\n\tconti = True\n\twhile conti and q1 and q2:\n\t\tlogger_cagada.debug('la q1 {} la q2 {}'.format(q1, q2))\n\t\tconti = soldado_del_amor_barajea(q1, q2, comps_ya_vistas)\n\t\tnum_vueltas += 1\n\tif not conti:\n\t\treturn (None, None)\n\tif not q1:\n\t\treturn (num_vueltas, 2)\n\telse:\n\t\treturn (num_vueltas, 1)\n\ndef soldado_del_amor_main():\n\tlineas = list(sys.stdin)\n\tnums1 = [int(x) for x in lineas[1].strip().split(' ')]\n\tnums2 = [int(x) for x in lineas[2].strip().split(' ')]\n\t(n_v, g) = soldado_del_amor_core(nums1[1:], nums2[1:])\n\tif n_v:\n\t\tprint('{} {}'.format(n_v, g))\n\telse:\n\t\tprint('-1')\nFORMAT = '[%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s'\nlogging.basicConfig(level=nivel_log, format=FORMAT)\nlogger_cagada = logging.getLogger('asa')\nlogger_cagada.setLevel(nivel_log)\nsoldado_del_amor_main()\n", "from collections import deque\nn = int(input())\na = [int(x) for x in input().split()]\nb = [int(x) for x in input().split()]\na = a[1:]\nb = b[1:]\na = deque(a)\nb = deque(b)\nk = 0\nwinner = 0\nwhile 1:\n\tif len(a) == 0:\n\t\twinner = 2\n\t\tbreak\n\telif len(b) == 0:\n\t\twinner = 1\n\t\tbreak\n\tfirstcard = a.popleft()\n\tsecondcard = b.popleft()\n\tif firstcard < secondcard:\n\t\tb.append(firstcard)\n\t\tb.append(secondcard)\n\telse:\n\t\ta.append(secondcard)\n\t\ta.append(firstcard)\n\tk += 1\n\tif k >= 10 ** 7:\n\t\tbreak\nif winner == 0:\n\tprint(-1)\nelse:\n\tprint(k, winner)\n", "def solve(n, playerA, playerB):\n\tround = 0\n\tcompared = {}\n\tplayerA.pop(0)\n\tplayerB.pop(0)\n\twhile True:\n\t\tif len(playerA) == 0:\n\t\t\tprint(round, 2)\n\t\t\treturn\n\t\telif len(playerB) == 0:\n\t\t\tprint(round, 1)\n\t\t\treturn\n\t\telse:\n\t\t\tcurrentStruc = [playerA.copy(), playerB.copy()]\n\t\t\tcardA = playerA.pop(0)\n\t\t\tcardB = playerB.pop(0)\n\t\t\tif cardA not in compared:\n\t\t\t\tcompared[cardA] = [currentStruc]\n\t\t\telse:\n\t\t\t\tfor arr in compared[cardA]:\n\t\t\t\t\tif arr == currentStruc:\n\t\t\t\t\t\tprint(-1)\n\t\t\t\t\t\treturn\n\t\t\t\tcompared[cardA].append(currentStruc)\n\t\t\tif cardA > cardB:\n\t\t\t\tplayerA.append(cardB)\n\t\t\t\tplayerA.append(cardA)\n\t\t\telse:\n\t\t\t\tplayerB.append(cardA)\n\t\t\t\tplayerB.append(cardB)\n\t\t\tround += 1\nn = int(input())\na = list(map(int, input().split()))\nb = list(map(int, input().split()))\nsolve(n, a, b)\n", "from collections import deque\ncache = {}\nN = int(input())\nleft = deque([int(i) for i in input().split()][1:])\nright = deque([int(i) for i in input().split()][1:])\n\ndef next(state):\n\tcards = (state[0].popleft(), state[1].popleft())\n\tappendto = max(cards) == cards[1]\n\tstate[appendto].extend((cards[not appendto], cards[appendto]))\n\treturn state\nstate = (left, right)\nf = 0\nwhile len(state[0]) and len(state[1]):\n\ta = (tuple(state[0]), tuple(state[1]))\n\tif a in cache:\n\t\tbreak\n\tcache[a] = True\n\tstate = next(state)\n\tf += 1\nelse:\n\tprint(f, int(len(state[0]) == 0) + 1)\n\texit(0)\nprint(-1)\n", "from collections import *\nn = int(input())\nli1 = list(map(int, input().split(' ')))\nli2 = list(map(int, input().split(' ')))\nst1 = deque(li1[1:])\nst2 = deque(li2[1:])\nli1 = deque(li1[1:])\nli2 = deque(li2[1:])\nchecked = []\ntemp = (st1.copy(), st2.copy())\nchecked.append(temp)\np1 = st1.popleft()\np2 = st2.popleft()\nif p1 > p2:\n\tst1.append(p2)\n\tst1.append(p1)\nelse:\n\tst2.append(p1)\n\tst2.append(p2)\nres = 0\ncount = 1\nwhile True:\n\tif len(st2) == 0:\n\t\tres = 1\n\t\tbreak\n\tif len(st1) == 0:\n\t\tres = 2\n\t\tbreak\n\ttemp = (st1.copy(), st2.copy())\n\tif temp in checked:\n\t\tbreak\n\tchecked.append(temp)\n\tp1 = st1.popleft()\n\tp2 = st2.popleft()\n\tif p1 > p2:\n\t\tst1.append(p2)\n\t\tst1.append(p1)\n\telse:\n\t\tst2.append(p1)\n\t\tst2.append(p2)\n\tcount += 1\nif res == 0:\n\tprint(-1)\n\tquit()\nprint(count, res)\n", "from collections import *\nn = int(input())\n(a1, a2) = [deque(map(int, input().split())) for i in range(2)]\n(k1, k2, visit) = (a1.popleft(), a2.popleft(), defaultdict(int))\n(state, c) = ((tuple(a1), tuple(a2)), 0)\nwhile not visit[state] and k1 and k2:\n\t(n1, n2) = (a1.popleft(), a2.popleft())\n\tk1 -= 1\n\tk2 -= 1\n\tif n1 > n2:\n\t\ta1.extend([n2, n1])\n\t\tk1 += 2\n\telse:\n\t\ta2.extend([n1, n2])\n\t\tk2 += 2\n\tvisit[state] = 1\n\tstate = (tuple(a1), tuple(a2))\n\tc += 1\nif k1 == 0:\n\tprint(c, 2)\nelif k2 == 0:\n\tprint(c, 1)\nelse:\n\tprint(-1)\n", "from collections import deque\nt = int(input())\narr = [int(x) for x in input().split()]\narr1 = [int(x) for x in input().split()]\nq1 = deque()\nfor i in range(1, len(arr)):\n\tq1.append(arr[i])\nq2 = deque()\nfor i in range(1, len(arr1)):\n\tq2.append(arr1[i])\n(cnt, c) = (0, 0)\nwhile len(q1) > 0 and len(q2) > 0:\n\tx = q1.popleft()\n\ty = q2.popleft()\n\tcnt += 1\n\tif x > y:\n\t\tq1.append(y)\n\t\tq1.append(x)\n\telse:\n\t\tq2.append(x)\n\t\tq2.append(y)\n\tif arr == q2 and arr1 == q1:\n\t\tc = 1\n\t\tbreak\n\telif cnt > 1000000:\n\t\tc = 1\n\t\tbreak\nif len(q1) == 0:\n\tprint(cnt, 2)\nelif c == 1:\n\tprint(-1)\nelse:\n\tprint(cnt, 1)\n", "n = int(input())\nl = list(map(int, input().split()))[1:]\nk = list(map(int, input().split()))[1:]\n(a, b) = (l[:], k[:])\nans = 0\nwhile len(l) and len(k) and (ans != -1) and (ans < 1000000):\n\t(v, s) = (l[0], k[0])\n\t(l, k) = (l[1:], k[1:])\n\tif v > s:\n\t\tl += [s, v]\n\telse:\n\t\tk += [v, s]\n\tans += 1\n\tif ans != 1 and (a == l and b == k or (a == k and b == l)):\n\t\tans = -1\nif ans == -1 or (len(k) and len(l)):\n\tprint(-1)\nelif len(l) == 0:\n\tprint(ans, 2)\nelse:\n\tprint(ans, 1)\n", "from collections import deque\n\ndef serialize(d):\n\tl = ''\n\tfor e in d:\n\t\tl += str(e)\n\treturn l\n\ndef hash(d1, d2):\n\treturn serialize(d1) + ':' + serialize(d2)\n\ndef main():\n\tn = int(input())\n\tstack1 = deque(map(int, input().split()))\n\tstack2 = deque(map(int, input().split()))\n\t(stack1.popleft(), stack2.popleft())\n\tstates = set()\n\tstates.add(hash(stack1, stack2))\n\tmoves = 0\n\twhile len(stack1) > 0 and len(stack2) > 0:\n\t\t(card1, card2, moves) = (stack1.popleft(), stack2.popleft(), moves + 1)\n\t\tif card1 > card2:\n\t\t\tstack1.append(card2)\n\t\t\tstack1.append(card1)\n\t\telse:\n\t\t\tstack2.append(card1)\n\t\t\tstack2.append(card2)\n\t\tnewState = hash(stack1, stack2)\n\t\tif newState in states:\n\t\t\tbreak\n\t\tstates.add(newState)\n\t(s1, s2) = (len(stack1), len(stack2))\n\tif s1 > 0 and s2 == 0:\n\t\tprint(moves, 1)\n\telif s2 > 0 and s1 == 0:\n\t\tprint(moves, 2)\n\telse:\n\t\tprint(-1)\nmain()\n", "import sys\nimport collections\n\ndef get_code(first_cards, second_cards):\n\tc1 = 0\n\tfor (i, card) in enumerate(first_cards):\n\t\tc1 += (card - 1) * 10 ** i\n\tc2 = 0\n\tfor (i, card) in enumerate(second_cards):\n\t\tc2 += (card - 1) * 10 ** i\n\treturn (c1, c2)\n\ndef main():\n\tn = int(sys.stdin.readline())\n\tfirst_cards = collections.deque(reversed(list(map(int, sys.stdin.readline().split()))[1:]))\n\tsecond_cards = collections.deque(reversed(list(map(int, sys.stdin.readline().split()))[1:]))\n\texisting_states = set()\n\tnumber_of_fights = 0\n\twhile first_cards and second_cards:\n\t\tcode = (tuple(first_cards), tuple(second_cards))\n\t\tif code in existing_states:\n\t\t\tprint(-1)\n\t\t\treturn\n\t\texisting_states.add(code)\n\t\tif first_cards[-1] > second_cards[-1]:\n\t\t\tfirst_cards.appendleft(second_cards.pop())\n\t\t\tfirst_cards.appendleft(first_cards.pop())\n\t\telse:\n\t\t\tsecond_cards.appendleft(first_cards.pop())\n\t\t\tsecond_cards.appendleft(second_cards.pop())\n\t\tnumber_of_fights += 1\n\tprint('%d %d' % (number_of_fights, 1 if not second_cards else 2))\nmain()\n", "n = int(input())\nx = input().split()\ny = input().split()\nplayer_1 = []\nplayer_2 = []\nmemory = []\naux_1 = []\naux_2 = []\nfor card in range(int(x[0])):\n\tplayer_1.append(int(x[card + 1]))\n\taux_1.append(int(x[card + 1]))\nfor card in range(int(y[0])):\n\tplayer_2.append(int(y[card + 1]))\n\taux_2.append(int(y[card + 1]))\nmemory.append((aux_1, aux_2))\ncondition = False\ncounter = 0\nwhile len(player_1) > 0 and len(player_2) > 0 and (not condition):\n\t(c1, c2) = (player_1.pop(0), player_2.pop(0))\n\tif c1 > c2:\n\t\tplayer_1.append(c2)\n\t\tplayer_1.append(c1)\n\telse:\n\t\tplayer_2.append(c1)\n\t\tplayer_2.append(c2)\n\taux_1 = []\n\taux_2 = []\n\tif (player_1, player_2) in memory:\n\t\tcondition = True\n\t\tbreak\n\tfor card in player_1:\n\t\taux_1.append(card)\n\tfor card in player_2:\n\t\taux_2.append(card)\n\tmemory.append((aux_1, aux_2))\n\tcounter += 1\nif not condition:\n\tanswer = str(counter)\n\tif len(player_1) == 0:\n\t\tanswer += ' 2'\n\telse:\n\t\tanswer += ' 1'\nelse:\n\tanswer = '-1'\nprint(answer)\n", "n = int(input())\nl1 = list(map(int, input().split()))\nl1.remove(l1[0])\nl2 = list(map(int, input().split()))\nl2.remove(l2[0])\n(a, b, c, d, l3, l4) = (0, 0, 0, 0, [l1[:]], [l2[:]])\nwhile len(l1) != 0 and len(l2) != 0:\n\tif l1[0] > l2[0]:\n\t\t(a, d) = (l1[0], l2[0])\n\t\tl1.remove(l1[0])\n\t\tl2.remove(l2[0])\n\t\tl1.append(d)\n\t\tl1.append(a)\n\telse:\n\t\t(a, d) = (l2[0], l1[0])\n\t\tl2.remove(l2[0])\n\t\tl1.remove(l1[0])\n\t\tl2.append(d)\n\t\tl2.append(a)\n\tb += 1\n\tif l1 not in l3 or l2 not in l4:\n\t\tl3.append(l1[:])\n\t\tl4.append(l2[:])\n\telse:\n\t\tc = 1\n\t\tbreak\nif c == 0:\n\tif len(l1) == 0:\n\t\tprint(str(b) + ' 2')\n\telse:\n\t\tprint(str(b) + ' 1')\nelse:\n\tprint(-1)\n", "from collections import deque\nn = int(input())\na1 = deque(map(int, input().split()))\na1.popleft()\na2 = deque(map(int, input().split()))\na2.popleft()\ncnt = 0\nwhile len(a1) != 0 and len(a2) != 0 and (cnt <= 1000):\n\t(s1, s2) = (a1.popleft(), a2.popleft())\n\tif s1 == s2:\n\t\tcontinue\n\telif s1 > s2:\n\t\ta1.append(s2)\n\t\ta1.append(s1)\n\telif s2 > s1:\n\t\ta2.append(s1)\n\t\ta2.append(s2)\n\tcnt += 1\nif cnt >= 1000:\n\tprint('-1')\nelse:\n\tprint(cnt, end=' ')\n\tif len(a1) == 0:\n\t\tprint(2)\n\telse:\n\t\tprint(1)\n", "def all_equal(arr, a):\n\tfor i in range(len(a)):\n\t\tif check_equal(arr, a[i]):\n\t\t\treturn True\n\treturn False\n\ndef check_equal(a, b):\n\tif len(a) != len(b):\n\t\treturn False\n\tfor i in range(len(a)):\n\t\tif a[i] != b[i]:\n\t\t\treturn False\n\treturn True\nn = int(input())\nk1 = [int(i) for i in input().split()]\nk2 = [int(i) for i in input().split()]\na = k1[1:]\nk1 = k1[0]\nb = k2[1:]\nk2 = k2[0]\ntemp_a = a[1:]\ntemp_b = b[1:]\nif a[0] > b[0]:\n\ttemp_a.append(b[0])\n\ttemp_a.append(a[0])\nelse:\n\ttemp_b.append(a[0])\n\ttemp_b.append(b[0])\na = [a]\nb = [b]\ncount = 1\nwhile (not all_equal(temp_a, a) or not all_equal(temp_b, b)) and len(temp_a) and len(temp_b):\n\ta.append(temp_a)\n\tb.append(temp_b)\n\ta1 = temp_a[1:]\n\tb1 = temp_b[1:]\n\tif temp_a[0] > temp_b[0]:\n\t\ta1.append(temp_b[0])\n\t\ta1.append(temp_a[0])\n\telse:\n\t\tb1.append(temp_a[0])\n\t\tb1.append(temp_b[0])\n\ttemp_a = a1\n\ttemp_b = b1\n\tcount += 1\nif not len(temp_a):\n\tprint(count, 2)\nelif not len(temp_b):\n\tprint(count, 1)\nelse:\n\tprint(-1)\n", "from collections import deque\nn = int(input())\ndec1 = deque()\ndec2 = deque()\nk1 = input().split()\nfor i in range(1, len(k1)):\n\tdec1.append(int(k1[i]))\nk2 = input().split()\nfor i in range(1, len(k2)):\n\tdec2.append(int(k2[i]))\nisAnswer = False\nfor i in range(1, 1000):\n\tif len(dec1) == 0:\n\t\tprint(str(i - 1) + ' ' + '2')\n\t\tisAnswer = True\n\t\tbreak\n\telif len(dec2) == 0:\n\t\tprint(str(i - 1) + ' ' + '1')\n\t\tisAnswer = True\n\t\tbreak\n\tx = dec1.popleft()\n\ty = dec2.popleft()\n\tif x > y:\n\t\tdec1.append(y)\n\t\tdec1.append(x)\n\telse:\n\t\tdec2.append(x)\n\t\tdec2.append(y)\nif isAnswer == False:\n\tprint(-1)\n", "n = int(input())\nstack1 = list(map(int, input().split()))\nstack2 = list(map(int, input().split()))\nk1 = stack1.pop(0)\nk2 = stack2.pop(0)\nfinal = 0\nstate = [(stack1[:], stack2[:])]\nbool1 = True\nwhile True:\n\tif (stack1, stack2) in state[:-1]:\n\t\tprint(-1)\n\t\tbool1 = False\n\t\tbreak\n\tif stack1[0] > stack2[0]:\n\t\tstack1.append(stack2[0])\n\t\tstack2.pop(0)\n\t\tstack1.append(stack1[0])\n\t\tstack1.pop(0)\n\t\tstate.append((stack1[:], stack2[:]))\n\telif stack1[0] < stack2[0]:\n\t\tstack2.append(stack1[0])\n\t\tstack1.pop(0)\n\t\tstack2.append(stack2[0])\n\t\tstack2.pop(0)\n\t\tstate.append((stack1[:], stack2[:]))\n\tif len(stack1) == 0:\n\t\tfinal = 2\n\t\tbreak\n\telif len(stack2) == 0:\n\t\tfinal = 1\n\t\tbreak\nif bool1:\n\tprint(len(state) - 1)\n\tprint(final)\n", "from sys import stdin, stdout\ncin = stdin.readline\ncout = stdout.write\nn = int(cin())\na = list(map(int, cin().split()))[1:]\nb = list(map(int, cin().split()))[1:]\nva = []\nvb = []\ncount = 0\nwhile True:\n\tif not a or not b:\n\t\tcout(str(count) + ' ' + str((not a) * 2 + (not b) * 1))\n\t\tbreak\n\tif b in vb and a in va:\n\t\tcout('-1')\n\t\texit(0)\n\tva.append(a[:])\n\tvb.append(b[:])\n\tif a[0] > b[0]:\n\t\ta.append(b[0])\n\t\ta.append(a[0])\n\telse:\n\t\tb.append(a[0])\n\t\tb.append(b[0])\n\ta.pop(0)\n\tb.pop(0)\n\tcount += 1\n", "from collections import deque\ninput()\ncards1 = deque(list(map(int, input().split()))[1:])\ncards2 = deque(list(map(int, input().split()))[1:])\nfor i in range(1, 106):\n\t(card1, card2) = (cards1.popleft(), cards2.popleft())\n\tif card1 > card2:\n\t\tcards1.extend([card2, card1])\n\telse:\n\t\tcards2.extend([card1, card2])\n\tif not cards1:\n\t\tprint(i, 2)\n\t\tbreak\n\tif not cards2:\n\t\tprint(i, 1)\n\t\tbreak\nelse:\n\tprint(-1)\n", "import sys\nimport string\nimport math\nfrom collections import defaultdict\nfrom functools import lru_cache\nfrom collections import Counter\nfrom collections import deque\nfrom fractions import Fraction\n\ndef mi(s):\n\treturn map(int, s.strip().split())\n\ndef lmi(s):\n\treturn list(mi(s))\n\ndef tmi(s):\n\treturn tuple(mi(s))\n\ndef mf(f, s):\n\treturn map(f, s)\n\ndef lmf(f, s):\n\treturn list(mf(f, s))\n\ndef js(lst):\n\treturn ' '.join((str(d) for d in lst))\n\ndef jsns(lst):\n\treturn ''.join((str(d) for d in lst))\n\ndef line():\n\treturn sys.stdin.readline().strip()\n\ndef linesp():\n\treturn line().split()\n\ndef iline():\n\treturn int(line())\n\ndef mat(n):\n\tmatr = []\n\tfor _ in range(n):\n\t\tmatr.append(linesp())\n\treturn matr\n\ndef matns(n):\n\tmat = []\n\tfor _ in range(n):\n\t\tmat.append([c for c in line()])\n\treturn mat\n\ndef mati(n):\n\tmat = []\n\tfor _ in range(n):\n\t\tmat.append(lmi(line()))\n\treturn mat\n\ndef pmat(mat):\n\tfor row in mat:\n\t\tprint(js(row))\n\ndef main():\n\tline()\n\ts = deque(lmi(line())[1:])\n\tt = deque(lmi(line())[1:])\n\tvisited = set()\n\tfights = 0\n\twinner = None\n\twhile True:\n\t\tif s and t:\n\t\t\tfights += 1\n\t\t\tif s[0] > t[0]:\n\t\t\t\ta = s.popleft()\n\t\t\t\tb = t.popleft()\n\t\t\t\ts.append(b)\n\t\t\t\ts.append(a)\n\t\t\telse:\n\t\t\t\ta = s.popleft()\n\t\t\t\tb = t.popleft()\n\t\t\t\tt.append(a)\n\t\t\t\tt.append(b)\n\t\t\tstate = (str(s), str(t))\n\t\t\tif state in visited:\n\t\t\t\tprint(-1)\n\t\t\t\treturn\n\t\t\telse:\n\t\t\t\tvisited.add(state)\n\t\telif not s:\n\t\t\twinner = 2\n\t\t\tbreak\n\t\telif not t:\n\t\t\twinner = 1\n\t\t\tbreak\n\tprint(fights, winner)\nmain()\n", "n = int(input())\nl = list(map(int, input().split()))\nl1 = list(map(int, input().split()))\n\ndef fact(k):\n\tp = 1\n\tfor i in range(1, k + 1):\n\t\tp = p * i\n\treturn p\nans = 0\nans1 = -1\nz = fact(n + 1)\nwhile True:\n\tif len(l) == 1 or (len(l1) == 1 or z < 0):\n\t\tbreak\n\tz -= 1\n\tans += 1\n\tif l[1] > l1[1]:\n\t\tl.append(l1[1])\n\t\tl.append(l[1])\n\t\tl.remove(l[1])\n\t\tl1.remove(l1[1])\n\telse:\n\t\tl1.append(l[1])\n\t\tl1.append(l1[1])\n\t\tl.remove(l[1])\n\t\tl1.remove(l1[1])\n\tif len(l) == 1:\n\t\tans1 = 2\n\telif len(l1) == 1:\n\t\tans1 = 1\nif ans1 == -1:\n\tprint(-1)\nelse:\n\tprint(ans, end=' ')\n\tprint(ans1)\n", "from collections import deque\nn = int(input())\ninp = list(map(int, input().split(' ')))\nfirst = deque()\nfor el in inp[1:]:\n\tfirst.append(el)\ninp = list(map(int, input().split(' ')))\nsecond = deque()\nfor el in inp[1:]:\n\tsecond.append(el)\nend = False\nres = ''\nfor i in range(1, 250000):\n\ttmp1 = first.popleft()\n\ttmp2 = second.popleft()\n\tif tmp1 > tmp2:\n\t\tfirst.append(tmp2)\n\t\tfirst.append(tmp1)\n\telse:\n\t\tsecond.append(tmp1)\n\t\tsecond.append(tmp2)\n\tif len(first) == 0:\n\t\tend = True\n\t\tres = str(i) + ' 2'\n\t\tbreak\n\telif len(second) == 0:\n\t\tend = True\n\t\tres = str(i) + ' 1'\n\t\tbreak\nprint(-1 if not end else res)\n", "def solve(a, b):\n\tstate = set()\n\tcount = 0\n\twhile len(a) > 0 and len(b) > 0:\n\t\tk = str(a) + str(b)\n\t\tif k in state:\n\t\t\treturn -1\n\t\tstate.add(k)\n\t\t(x, y) = (a.pop(0), b.pop(0))\n\t\tif x > y:\n\t\t\ta.extend([y, x])\n\t\telse:\n\t\t\tb.extend([x, y])\n\t\tcount += 1\n\treturn (count, 1 if len(b) == 0 else 2)\nf = lambda : list(map(int, input().split()))\ninput()\nres = solve(f()[1:], f()[1:])\nprint(-1 if res == -1 else '{} {}'.format(res[0], res[1]))\n", "n = int(input())\ns = [[], []]\ndata = list(map(int, input().split()))\n(k1, s[0]) = (data[0], [data[i + 1] for i in range(len(data) - 1)])\ndata = list(map(int, input().split()))\n(k2, s[1]) = (data[0], [data[i + 1] for i in range(len(data) - 1)])\n(ans, iter) = (0, 0)\nwhile True:\n\tif len(s[0]) == 0:\n\t\tprint(iter, 2)\n\t\texit()\n\tif len(s[1]) == 0:\n\t\tprint(iter, 1)\n\t\texit()\n\tif iter > 100000:\n\t\tprint(-1)\n\t\texit()\n\t(a, b) = (s[0][0], s[1][0])\n\ts[0].pop(0)\n\ts[1].pop(0)\n\tif a < b:\n\t\ts[1].append(a)\n\t\ts[1].append(b)\n\telse:\n\t\ts[0].append(b)\n\t\ts[0].append(a)\n\titer += 1\n", "n = int(input())\nsoldier_1 = list(map(int, input().split()))\nsoldier_1 = soldier_1[1:]\nsoldier_2 = list(map(int, input().split()))\nsoldier_2 = soldier_2[1:]\nunfinished = True\ncounter = 0\nchecker = len(soldier_1) + len(soldier_2)\nwhile unfinished:\n\tif soldier_1[0] > soldier_2[0]:\n\t\tsoldier_1.append(soldier_2[0])\n\t\tsoldier_1.append(soldier_1[0])\n\t\tsoldier_2.remove(soldier_2[0])\n\t\tsoldier_1.remove(soldier_1[0])\n\t\tcounter += 1\n\telif soldier_1[0] < soldier_2[0]:\n\t\tsoldier_2.append(soldier_1[0])\n\t\tsoldier_2.append(soldier_2[0])\n\t\tsoldier_1.remove(soldier_1[0])\n\t\tsoldier_2.remove(soldier_2[0])\n\t\tcounter += 1\n\tif counter >= 200:\n\t\tprint(-1)\n\t\tbreak\n\tif len(soldier_1) == checker:\n\t\tprint(counter, 1)\n\t\tunfinished = False\n\telif len(soldier_2) == checker:\n\t\tprint(counter, 2)\n\t\tunfinished = False\n", "import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools\nsys.setrecursionlimit(10 ** 7)\ninf = 10 ** 20\neps = 1.0 / 10 ** 10\nmod = 10 ** 9 + 7\ndd = [(-1, 0), (0, 1), (1, 0), (0, -1)]\nddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]\n\ndef LI():\n\treturn [int(x) for x in sys.stdin.readline().split()]\n\ndef LI_():\n\treturn [int(x) - 1 for x in sys.stdin.readline().split()]\n\ndef LF():\n\treturn [float(x) for x in sys.stdin.readline().split()]\n\ndef LS():\n\treturn sys.stdin.readline().split()\n\ndef I():\n\treturn int(sys.stdin.readline())\n\ndef F():\n\treturn float(sys.stdin.readline())\n\ndef S():\n\treturn input()\n\ndef pf(s):\n\treturn print(s, flush=True)\n\ndef main():\n\tn = I()\n\ta = LI()[1:]\n\tb = LI()[1:]\n\tfor i in range(100000):\n\t\tif a[0] > b[0]:\n\t\t\ta = a[1:] + [b[0], a[0]]\n\t\t\tb = b[1:]\n\t\t\tif len(b) == 0:\n\t\t\t\treturn '{} {}'.format(i + 1, 1)\n\t\telse:\n\t\t\tb = b[1:] + [a[0], b[0]]\n\t\t\ta = a[1:]\n\t\t\tif len(a) == 0:\n\t\t\t\treturn '{} {}'.format(i + 1, 2)\n\treturn -1\nprint(main())\n", "n = int(input())\nl1 = list(map(int, input().split()))\nl1.pop(0)\nl2 = list(map(int, input().split()))\nl2.pop(0)\ncnt = 0\nwhile 1:\n\tif len(l1) == 0 or len(l2) == 0:\n\t\tbreak\n\tcnt -= -1\n\tif l2[0] > l1[0]:\n\t\tl2.append(l1[0])\n\t\tl2.append(l2[0])\n\telse:\n\t\tl1.append(l2[0])\n\t\tl1.append(l1[0])\n\tl1.pop(0)\n\tl2.pop(0)\n\tif cnt == 100000:\n\t\tbreak\nif cnt == 100000:\n\tprint(-1)\nelif len(l1) == 0:\n\tprint(cnt, 2)\nelse:\n\tprint(cnt, 1)\n", "n = int(input())\nlist_1 = input().split()\nlist_2 = input().split()\nn1 = int(list_1.pop(0))\nn2 = int(list_2.pop(0))\n(listt_1, listt_2) = ([], [])\nfor i in range(n1):\n\tlist_1[i] = int(list_1[i])\n\tlistt_1.append(list_1[i])\nfor i in range(n2):\n\tlist_2[i] = int(list_2[i])\n\tlistt_2.append(int(list_2[i]))\n(no, l1, l2) = (0, [], [])\nwhile True:\n\tl1.append([])\n\tl2.append([])\n\tfor i in range(len(listt_1)):\n\t\tl1[no].append(listt_1[i])\n\tfor i in range(len(listt_2)):\n\t\tl2[no].append(listt_2[i])\n\tif listt_1[0] > listt_2[0]:\n\t\tj = listt_2.pop(0)\n\t\ti = listt_1.pop(0)\n\t\tlistt_1.append(j)\n\t\tlistt_1.append(i)\n\telse:\n\t\tj = listt_1.pop(0)\n\t\ti = listt_2.pop(0)\n\t\tlistt_2.append(j)\n\t\tlistt_2.append(i)\n\tno += 1\n\tif len(listt_1) == 0:\n\t\tprint(no, '2')\n\t\tbreak\n\tif len(listt_2) == 0:\n\t\tprint(no, '1')\n\t\tbreak\n\tif listt_1 in l1 and listt_2 in l2:\n\t\tprint('-1')\n\t\tbreak\n", "n = int(input())\nfrom collections import deque\nfrom copy import copy\na = deque([*map(int, input().split())][1:])\nb = deque([*map(int, input().split())][1:])\nta = copy(a)\ntb = copy(b)\nrounds = 0\nwhile len(a) and len(b):\n\tif a[0] > b[0]:\n\t\ta.append(b[0])\n\t\tb.popleft()\n\t\ta.append(a[0])\n\t\ta.popleft()\n\telse:\n\t\tb.append(a[0])\n\t\ta.popleft()\n\t\tb.append(b[0])\n\t\tb.popleft()\n\trounds += 1\n\tif a == ta and b == tb or rounds > 1000000.0:\n\t\tprint(-1)\n\t\texit()\nprint(rounds, 1 if len(a) else 2)\n", "n = int(input())\nl = list(map(int, input().split()))\nl.reverse()\nl.pop(-1)\nk = list(map(int, input().split()))\nk.reverse()\nk.pop(-1)\nans = 0\nflag = 0\nq = 1000\nwhile q:\n\tq -= 1\n\tif len(l) == 0 or len(k) == 0:\n\t\tif len(l) == 0:\n\t\t\tflag = 2\n\t\tif len(k) == 0:\n\t\t\tflag = 1\n\t\tbreak\n\tans += 1\n\tx = l[-1]\n\tl.pop(-1)\n\ty = k[-1]\n\tk.pop(-1)\n\tif x > y:\n\t\tl.insert(0, y)\n\t\tl.insert(0, x)\n\telse:\n\t\tk.insert(0, x)\n\t\tk.insert(0, y)\nif q == 0:\n\tprint(-1)\nelse:\n\tprint(ans, flag)\n", "from sys import stdin\n\ndef get_ints():\n\treturn list(map(int, stdin.readline().strip().split()))\ninput()\na = get_ints()[1:]\nb = get_ints()[1:]\nfights = 0\ncan_win = True\nseen = {}\nwhile len(a) > 0 and len(b) > 0:\n\tif repr([a, b]) in seen:\n\t\tif seen[repr([a, b])] > 20:\n\t\t\tcan_win = False\n\t\t\tbreak\n\t\telse:\n\t\t\tseen[repr([a, b])] += 1\n\telse:\n\t\tseen[repr([a, b])] = 1\n\tif a[0] > b[0]:\n\t\tt1 = a[0]\n\t\tt2 = b[0]\n\t\ta = a[1:]\n\t\tb = b[1:]\n\t\ta.append(t2)\n\t\ta.append(t1)\n\telif a[0] < b[0]:\n\t\tt1 = a[0]\n\t\tt2 = b[0]\n\t\ta = a[1:]\n\t\tb = b[1:]\n\t\tb.append(t1)\n\t\tb.append(t2)\n\tfights += 1\nif not can_win:\n\tprint(-1)\n\texit()\nwinner = 1 if len(b) == 0 else 2\nprint(fights, winner)\n", "from collections import deque\nfrom copy import deepcopy\nn = int(input())\nA = deque(list(map(int, input().split()))[1:])\nB = deque(list(map(int, input().split()))[1:])\nstate = []\nstate.append((deepcopy(A), deepcopy(B)))\ncnt = 0\nwhile len(A) > 0 and len(B) > 0:\n\tif A[0] > B[0]:\n\t\tA.append(B.popleft())\n\t\tA.append(A.popleft())\n\telse:\n\t\tB.append(A.popleft())\n\t\tB.append(B.popleft())\n\tif (A, B) in state:\n\t\tprint(-1)\n\t\tbreak\n\tstate.append((deepcopy(A), deepcopy(B)))\n\tcnt += 1\nelse:\n\tprint(cnt, 1 if len(B) == 0 else 2)\n", "configs = set()\ntotal = 0\n\ndef trans(d):\n\treturn '#'.join(map(str, d))\n\ndef is_cycle(d1, d2):\n\tt1 = trans(d1)\n\tt2 = trans(d2)\n\treturn t1 + '@' + t2 in configs or t2 + '@' + t1 in configs\n\ndef add_config(d1, d2):\n\tconfigs.add(trans(d1) + '@' + trans(d2))\n\tconfigs.add(trans(d2) + '@' + trans(d1))\n\ndef step(d1, d2):\n\tadd_config(d1, d2)\n\tglobal total\n\ttotal += 1\n\tk1 = d1.pop(0)\n\tk2 = d2.pop(0)\n\tif k1 > k2:\n\t\td1.append(k2)\n\t\td1.append(k1)\n\telse:\n\t\td2.append(k1)\n\t\td2.append(k2)\n\tif len(d1) == 0:\n\t\tprint(total, 2)\n\t\texit(0)\n\telif len(d2) == 0:\n\t\tprint(total, 1)\n\t\texit(0)\n\telif is_cycle(d1, d2):\n\t\tprint(-1)\n\t\texit(0)\nn = int(input())\nd1 = list(map(int, input().split()))[1:]\nd2 = list(map(int, input().split()))[1:]\nwhile True:\n\tstep(d1, d2)\n", "n = int(input())\nper1 = input().split()\nper2 = input().split()\nA = per1[1:]\nB = per2[1:]\n\ndef func(A, B):\n\tif int(A[0]) > int(B[0]):\n\t\tA.append(B.pop(0))\n\t\tA.append(A.pop(0))\n\telse:\n\t\tB.append(A.pop(0))\n\t\tB.append(B.pop(0))\nanswer = 0\ns = 1\nvse = set()\nlenny = 0\nwhile A and B:\n\tvse.add(''.join(A) + ',' + ''.join(B))\n\tfunc(A, B)\n\tif len(vse) == answer:\n\t\ts = 0\n\t\tbreak\n\tanswer += 1\nprint(str(answer) + ' ' + str(2 if len(A) == 0 else 1) if s == 1 else -1)\n", "import collections\nn = int(input())\nk1ss = list(map(int, input().split()))\nk2ss = list(map(int, input().split()))\nk1s = collections.deque(k1ss[1:], 2 * n)\nk2s = collections.deque(k2ss[1:], 2 * n)\n\ndef solve(n, k1s, k2s):\n\tfs = 0\n\tvisited = set()\n\tvisited.add(tuple(k1s) + (0,) + tuple(k2s))\n\twhile k1s and k2s:\n\t\tkey = fight(k1s, k2s)\n\t\tif key in visited:\n\t\t\treturn (-1,)\n\t\tvisited.add(key)\n\t\tfs += 1\n\tif k1s:\n\t\treturn (fs, 1)\n\telse:\n\t\treturn (fs, 2)\n\ndef fight(k1s, k2s):\n\tk1 = k1s.popleft()\n\tk2 = k2s.popleft()\n\tif k1 > k2:\n\t\tk1s.append(k2)\n\t\tk1s.append(k1)\n\telse:\n\t\tk2s.append(k1)\n\t\tk2s.append(k2)\n\treturn tuple(k1s) + (0,) + tuple(k2s)\nprint(*solve(n, k1s, k2s))\n", "from bisect import *\nfrom math import *\nfrom collections import *\nfrom sys import stdin, stdout\ninput = stdin.readline\nI = lambda : int(input())\nM = lambda : map(int, input().split())\nLI = lambda : list(map(int, input().split()))\nfor _ in range(1):\n\tI()\n\ta = LI()\n\tb = LI()\n\ta = deque(a[1:])\n\tb = deque(b[1:])\n\tc = deque() + a\n\td = deque() + b\n\tflag = 1\n\tPgames = 0\n\td1 = [list(c)]\n\td2 = [list(d)]\n\twhile len(c) and len(d) and flag:\n\t\tt1 = c[0]\n\t\tt2 = d[0]\n\t\tif c[0] > d[0]:\n\t\t\tc.append(t2)\n\t\t\tc.append(t1)\n\t\telse:\n\t\t\td.append(t1)\n\t\t\td.append(t2)\n\t\tc.popleft()\n\t\td.popleft()\n\t\tPgames += 1\n\t\tif list(c) in d1 and list(d) in d2:\n\t\t\tflag = 0\n\t\td1 += [list(c)]\n\t\td2 += [list(d)]\n\tif not flag:\n\t\tprint(-1)\n\telif len(c) == 0:\n\t\tprint(Pgames, 2)\n\telse:\n\t\tprint(Pgames, 1)\n", "import sys\nn = int(input())\nd1 = list(map(int, input().split(' ')))\nd2 = list(map(int, input().split(' ')))\nk1 = d1[0]\nk2 = d2[0]\nd1.remove(d1[0])\nd2.remove(d2[0])\nmoves = 0\nwhile True:\n\tif moves > 500000:\n\t\tprint(-1)\n\t\treturn\n\ta = d1[0]\n\td1.remove(d1[0])\n\tb = d2[0]\n\td2.remove(d2[0])\n\tif a > b:\n\t\tmoves += 1\n\t\td1.append(b)\n\t\td1.append(a)\n\t\tif len(d2) == 0:\n\t\t\tprint(str(moves) + ' 1')\n\t\t\treturn\n\tif b > a:\n\t\tmoves += 1\n\t\td2.append(a)\n\t\td2.append(b)\n\t\tif len(d1) == 0:\n\t\t\tprint(str(moves) + ' 2')\n\t\t\treturn\n", "d = int(input())\nd1 = list(map(int, input().split()))[1:]\nd2 = list(map(int, input().split()))[1:]\nf = 0\nwhile d1 and d2:\n\tif f > 5000:\n\t\tprint(-1)\n\t\texit()\n\tif d1[0] > d2[0]:\n\t\td1.append(d2[0])\n\t\td1.append(d1[0])\n\t\td1.pop(0)\n\t\td2.pop(0)\n\telse:\n\t\td2.append(d1[0])\n\t\td2.append(d2[0])\n\t\td1.pop(0)\n\t\td2.pop(0)\n\tf += 1\nif d1:\n\tprint(f, 1)\nelse:\n\tprint(f, 2)\n", "ii = lambda : int(input())\nkk = lambda : map(int, input().split())\nll = lambda : list(kk())\nvis = set()\nn = ii()\nls = ll()[1:]\nls2 = ll()[1:]\nc = 0\nwhile ls and ls2:\n\tc += 1\n\ta = ls[0]\n\tb = ls2[0]\n\tls = ls[1:]\n\tls2 = ls2[1:]\n\tif a > b:\n\t\tls.extend([b, a])\n\telse:\n\t\tls2.extend([a, b])\n\ttup = (tuple(ls), tuple(ls2))\n\tif tup in vis:\n\t\tprint(-1)\n\t\texit()\n\tvis.add(tup)\nprint(c, 1 if ls else 2)\nexit()\n", "n = int(input())\na = [int(x) for x in input().split()[1:]]\nb = [int(x) for x in input().split()[1:]]\ns = set()\nt = 0\nwhile len(a) * len(b):\n\tt += 1\n\tx = a.pop(0)\n\ty = b.pop(0)\n\tif x > y:\n\t\ta.append(y)\n\t\ta.append(x)\n\telse:\n\t\tb.append(x)\n\t\tb.append(y)\n\tk = (tuple(a), tuple(b))\n\tif k in s:\n\t\tprint(-1)\n\t\texit()\n\ts.add(k)\nprint(t, 1 if len(a) else 2)\n", "n = int(input())\na = input().split()\nk1 = int(a[0])\nfor i in range(k1 + 1):\n\ta[i] = int(a[i])\na.pop(0)\nb = input().split()\nk2 = int(b[0])\nfor i in range(k2 + 1):\n\tb[i] = int(b[i])\nb.pop(0)\ncnt = 0\nwhile cnt <= 10000:\n\tif len(b) == 0:\n\t\tprint(cnt, 1)\n\t\texit()\n\tif len(a) == 0:\n\t\tprint(cnt, 2)\n\t\texit()\n\tif a[0] < b[0]:\n\t\tb.insert(len(b), a[0])\n\t\tb.insert(len(b), b[0])\n\t\tb.pop(0)\n\t\ta.pop(0)\n\telse:\n\t\ta.insert(len(a), b[0])\n\t\ta.insert(len(a), a[0])\n\t\tb.pop(0)\n\t\ta.pop(0)\n\tcnt += 1\nprint(-1)\n", "import sys, os, io\nfrom collections import deque\ninput = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline\nn = int(input())\ninp = [int(i) for i in input().split()]\nk1 = inp[0]\na = inp[1:]\na = deque(a)\ninp = [int(i) for i in input().split()]\nk2 = inp[0]\nb = inp[1:]\nb = deque(b)\ncnt = 0\nwhile len(a) and len(b):\n\tcnt += 1\n\tif cnt == 10 ** 6:\n\t\tbreak\n\tif a[0] > b[0]:\n\t\tf = a.popleft()\n\t\ts = b.popleft()\n\t\ta.append(s)\n\t\ta.append(f)\n\telse:\n\t\tf = a.popleft()\n\t\ts = b.popleft()\n\t\tb.append(f)\n\t\tb.append(s)\nif cnt == 10 ** 6:\n\tprint(-1)\nelif len(a) == 0:\n\tprint(cnt, 2)\nelse:\n\tprint(cnt, 1)\n", "n = int(input())\narr = list(map(int, input().split()))\n(n1, arr) = (arr[0], arr[1:])\nbrr = list(map(int, input().split()))\n(n2, brr) = (brr[0], brr[1:])\nmapper = {}\ncnt = 0\ncan = True\nwhile arr != [] and brr != []:\n\t(s, t) = (tuple(arr), tuple(brr))\n\tif s in mapper and mapper[s] == t:\n\t\tcan = False\n\t\tbreak\n\telse:\n\t\tmapper[s] = t\n\tx = arr[0]\n\tarr.pop(0)\n\ty = brr[0]\n\tbrr.pop(0)\n\tif y > x:\n\t\tbrr.append(x)\n\t\tbrr.append(y)\n\telse:\n\t\tarr.append(y)\n\t\tarr.append(x)\n\tcnt += 1\nif can == False:\n\tprint(-1)\nelif len(arr) == 0:\n\tprint(cnt, 2)\nelse:\n\tprint(cnt, 1)\n", "import math\nn = int(input())\np1 = list(map(int, input().split()))\np2 = list(map(int, input().split()))\np1.pop(0)\np2.pop(0)\ntotTurns = math.factorial(n + 1)\nprevCard = 0\nfor i in range(totTurns):\n\t(a, b) = (p1.pop(0), p2.pop(0))\n\tif a > b:\n\t\tp1.extend([b, a])\n\telse:\n\t\tp2.extend([a, b])\n\tif not p1 or not p2:\n\t\tprint(i + 1, 1 if p1 else 2)\n\t\tbreak\nelse:\n\tprint(-1)\n", "n = int(input())\nas_ = list(map(int, input().split(' ')))\nbs_ = list(map(int, input().split(' ')))\nas_ = as_[1:]\nbs_ = bs_[1:]\n\ndef play_round():\n\tglobal as_, bs_\n\ta = as_[0]\n\tas_ = as_[1:]\n\tb = bs_[0]\n\tbs_ = bs_[1:]\n\tif a > b:\n\t\tas_.append(b)\n\t\tas_.append(a)\n\telse:\n\t\tbs_.append(a)\n\t\tbs_.append(b)\nhas_played = set()\nn = 0\nwhile True:\n\tif len(as_) == 0 or len(bs_) == 0:\n\t\tbreak\n\ts = ','.join(map(str, as_)) + ':' + ','.join(map(str, bs_))\n\tif s in has_played:\n\t\tbreak\n\thas_played.add(s)\n\tplay_round()\n\tn += 1\nif len(as_) == 0:\n\tprint(n, 2)\nelif len(bs_) == 0:\n\tprint(n, 1)\nelse:\n\tprint(-1)\n", "n = int(input())\nstring = input()\ns1 = list(map(int, string.split()))\nstring = input()\ns2 = list(map(int, string.split()))\ndel s1[0], s2[0]\ncards1 = []\ncards2 = []\na = 0\nwhile True:\n\tif len(s1) == 0:\n\t\tprint(str(a) + ' 2')\n\t\tbreak\n\telif len(s2) == 0:\n\t\tprint(str(a) + ' 1')\n\t\tbreak\n\tx = s1[0]\n\ty = s2[0]\n\tdel s1[0], s2[0]\n\tif x > y:\n\t\ts1.append(y)\n\t\ts1.append(x)\n\telse:\n\t\ts2.append(x)\n\t\ts2.append(y)\n\tcondition = False\n\tfor x in range(a):\n\t\tif cards1[x] == s1 and cards2[x] == s2:\n\t\t\tprint(-1)\n\t\t\tcondition = True\n\t\t\tbreak\n\tif condition:\n\t\tbreak\n\tcards1.append(s1[:])\n\tcards2.append(s2[:])\n\ta += 1\n", "num_cards = int(input())\nperson1 = tuple(list(map(int, input().split()))[1:])\nperson2 = tuple(list(map(int, input().split()))[1:])\nhas_happened = set()\nfights = 0\nwhile (person1, person2) not in has_happened and person1 != () and (person2 != ()) and ((person2, person1) not in has_happened):\n\thas_happened.add((person1, person2))\n\tc1 = person1[0]\n\tc2 = person2[0]\n\tif c1 > c2:\n\t\tperson1 = person1[1:] + (c2, c1)\n\t\tperson2 = person2[1:]\n\telse:\n\t\tperson2 = person2[1:] + (c1, c2)\n\t\tperson1 = person1[1:]\n\tfights += 1\nif (person1, person2) in has_happened or (person2, person1) in has_happened:\n\tprint(-1)\nelif person1 == ():\n\tprint(fights, 2)\nelse:\n\tprint(fights, 1)\n", "n = int(input())\nx1 = list(map(int, input().strip().split()))\nx2 = list(map(int, input().strip().split()))\nn1 = x1[0]\nn2 = x2[0]\nx1 = x1[1:]\nx2 = x2[1:]\ncounter = 0\nwon = False\nwhile True:\n\tif len(x1) == 0:\n\t\tprint(str(counter) + ' ' + str(2))\n\t\tbreak\n\tif len(x2) == 0:\n\t\tprint(str(counter) + ' ' + str(1))\n\t\tbreak\n\tif counter >= 100000:\n\t\tprint(-1)\n\t\tbreak\n\tif x1[0] > x2[0]:\n\t\tx1.append(x2[0])\n\t\tx1.append(x1[0])\n\t\tx1 = x1[1:]\n\t\tx2 = x2[1:]\n\telse:\n\t\tx2.append(x1[0])\n\t\tx2.append(x2[0])\n\t\tx2 = x2[1:]\n\t\tx1 = x1[1:]\n\tcounter += 1\n", "from collections import deque\nfrom sys import stdin\ntot = int(stdin.readline())\narr1 = deque(list(map(int, stdin.readline().split()))[1:][::-1])\narr2 = deque(list(map(int, stdin.readline().split()))[1:][::-1])\nseen = list()\ncnt = 0\nwhile True:\n\tif ''.join(list(map(str, arr1))) + ''.join(list(map(str, arr2))) in seen:\n\t\tprint(-1)\n\t\tbreak\n\tseen.append(''.join(list(map(str, arr1))) + ''.join(list(map(str, arr2))))\n\tel_arr1 = arr1.pop()\n\tel_arr2 = arr2.pop()\n\tif el_arr1 > el_arr2:\n\t\tarr1.appendleft(el_arr2)\n\t\tarr1.appendleft(el_arr1)\n\telse:\n\t\tarr2.appendleft(el_arr1)\n\t\tarr2.appendleft(el_arr2)\n\tcnt += 1\n\tif len(arr1) == 0:\n\t\tprint(cnt, '2')\n\t\tbreak\n\tif len(arr2) == 0:\n\t\tprint(cnt, '1')\n\t\tbreak\n", "n = int(input())\na = list(map(int, input().split()))\nb = list(map(int, input().split()))\na = a[1:]\nb = b[1:]\ncount = 0\nflag = False\nwhile len(a) != 0 and len(b) != 0:\n\tx = a.pop(0)\n\ty = b.pop(0)\n\tif x < y:\n\t\tb.append(x)\n\t\tb.append(y)\n\telif x > y:\n\t\ta.append(y)\n\t\ta.append(x)\n\tcount += 1\n\tif count == 50000:\n\t\tflag = True\n\t\tbreak\nif flag:\n\tprint(-1)\nelif len(a) > len(b):\n\tprint(count, 1)\nelif len(b) > len(a):\n\tprint(count, 2)\n", "input()\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\ncount = 0\nwhile a and b:\n\tc = a.pop(0)\n\td = b.pop(0)\n\tif c > d:\n\t\ta += [d, c]\n\telse:\n\t\tb += [c, d]\n\tcount += 1\n\tif count > 1000:\n\t\tprint(-1)\n\t\tbreak\nelse:\n\tprint(count, [1, 2][len(a) == 0])\n", "from collections import Counter\nimport string\nimport math\nimport sys\n\ndef array_int():\n\treturn [int(i) for i in sys.stdin.readline().split()]\n\ndef vary(arrber_of_variables):\n\tif arrber_of_variables == 1:\n\t\treturn int(sys.stdin.readline())\n\tif arrber_of_variables >= 2:\n\t\treturn map(int, sys.stdin.readline().split())\n\ndef makedict(var):\n\treturn dict(Counter(var))\n\ndef printDivisors(n):\n\tdivisors = []\n\ti = 1\n\twhile i <= math.sqrt(n):\n\t\tif n % i == 0:\n\t\t\tif n // i == i:\n\t\t\t\tdivisors.append(i)\n\t\t\telse:\n\t\t\t\tdivisors.extend((i, n // i))\n\t\ti = i + 1\n\treturn divisors\nn = vary(1)\nnum1 = array_int()\nfn = num1[0]\nfcn = num1[1:]\nnum1 = array_int()\nsn = num1[0]\nscn = num1[1:]\ncount = 0\ni = 10 ** 5\nwhile i:\n\tif len(fcn) == 0:\n\t\tprint(count, 2)\n\t\tbreak\n\tif len(scn) == 0:\n\t\tprint(count, 1)\n\t\tbreak\n\tif fcn[0] > scn[0]:\n\t\tfcn.extend((scn[0], fcn[0]))\n\t\tfcn.pop(0)\n\t\tscn.pop(0)\n\t\tcount += 1\n\telif scn[0] > fcn[0]:\n\t\tscn.extend((fcn[0], scn[0]))\n\t\tscn.pop(0)\n\t\tfcn.pop(0)\n\t\tcount += 1\n\ti -= 1\nelse:\n\tprint(-1)\n", "def to_list(s):\n\treturn list(map(lambda x: int(x), s.split(' ')))\n\ndef make_step(a, b):\n\t(a_copy, b_copy) = (a.copy(), b.copy())\n\tif a_copy[0] > b_copy[0]:\n\t\ta_copy.extend([b_copy[0], a[0]])\n\telse:\n\t\tb_copy.extend([a_copy[0], b_copy[0]])\n\t(a_copy, b_copy) = (a_copy[1:], b_copy[1:])\n\treturn (a_copy, b_copy)\n\ndef solve(a, b):\n\tk = 0\n\tarrays = [(a, b)]\n\twhile True:\n\t\t(a, b) = make_step(a, b)\n\t\tk += 1\n\t\tif (a == []) | (b == []):\n\t\t\tbreak\n\t\tif (a, b) in arrays:\n\t\t\treturn -1\n\t\tarrays.append((a, b))\n\tif a == []:\n\t\tnumber = 2\n\telse:\n\t\tnumber = 1\n\treturn str(k) + ' ' + str(number)\nn = int(input())\na = to_list(input())[1:]\nb = to_list(input())[1:]\nprint(solve(a, b))\n", "n = int(input())\n(a1, a2) = ([], [])\nins = list(map(int, input().split()))\nfor i in range(1, ins[0] + 1):\n\ta1.append(ins[i])\nins = list(map(int, input().split()))\nfor i in range(1, ins[0] + 1):\n\ta2.append(ins[i])\nkol = 0\nwhile 89 == 89:\n\tkol += 1\n\tif kol > 10000:\n\t\tbreak\n\tif a1[0] > a2[0]:\n\t\ta1.append(a2[0])\n\t\ta1.append(a1[0])\n\t\ta1.pop(0)\n\t\ta2.pop(0)\n\telse:\n\t\ta2.append(a1[0])\n\t\ta2.append(a2[0])\n\t\ta1.pop(0)\n\t\ta2.pop(0)\n\tif not a1 or not a2:\n\t\tbreak\nif not a1:\n\tprint(kol, 2)\nelif not a2:\n\tprint(kol, 1)\nelse:\n\tprint(-1)\n", "from collections import deque\nn = int(input())\ninp = input()\nk1 = int(inp[0])\np1 = [int(x) for x in inp.split()[1:]]\ninp = input()\nk2 = int(inp[0])\np2 = [int(x) for x in inp.split()[1:]]\nplayer1 = deque(p1)\nplayer2 = deque(p2)\nwinnable = False\ncounter = 0\nwhile counter < 1000:\n\ttry:\n\t\tcard1 = player1.popleft()\n\t\tcard2 = player2.popleft()\n\texcept IndexError:\n\t\twinnable = True\n\t\tbreak\n\tif card1 > card2:\n\t\tplayer1.append(card2)\n\t\tplayer1.append(card1)\n\telse:\n\t\tplayer2.append(card1)\n\t\tplayer2.append(card2)\n\tcounter += 1\nif not winnable:\n\tprint(-1)\nelse:\n\ttry:\n\t\tplayer1.pop()\n\texcept IndexError:\n\t\tprint(f'{counter} 2')\n\ttry:\n\t\tplayer2.pop()\n\texcept IndexError:\n\t\tprint(f'{counter} 1')\n", "import math\nfrom decimal import *\nimport random\nn = int(input())\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\ncnt = 0\nfnd = False\nans = 0\nwhile len(a) > 0 and len(b) > 0 and (cnt < int(100000.0)):\n\tif a[0] > b[0]:\n\t\ta += [b[0]] + [a[0]]\n\t\ta = a[1:]\n\t\tb = b[1:]\n\telse:\n\t\tb += [a[0]] + [b[0]]\n\t\ta = a[1:]\n\t\tb = b[1:]\n\tcnt += 1\n\tif len(a) == 0:\n\t\tans = 2\n\t\tfnd = True\n\t\tbreak\n\tif len(b) == 0:\n\t\tans = 1\n\t\tfnd = True\n\t\tbreak\nif fnd:\n\tprint(cnt, ans)\nelse:\n\tprint(-1)\n", "from itertools import chain\n\ndef main():\n\tinput()\n\taa = list(map(int, input().split()))[1:]\n\tbb = list(map(int, input().split()))[1:]\n\t(visited, cnt) = (set(), 0)\n\twhile aa and bb:\n\t\tcnt += 1\n\t\t(a, b) = (aa.pop(0), bb.pop(0))\n\t\tif a > b:\n\t\t\taa.append(b)\n\t\t\taa.append(a)\n\t\telse:\n\t\t\tbb.append(a)\n\t\t\tbb.append(b)\n\t\tk = tuple(chain(aa, (0,), bb))\n\t\tif k in visited:\n\t\t\tprint(-1)\n\t\t\treturn\n\t\telse:\n\t\t\tvisited.add(k)\n\tprint(cnt, 1 + (not aa))\nmain()\n", "import io, os\ninput = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline\n\ndef array_equal(a, b):\n\tif len(a) == len(b):\n\t\tfor i in range(len(a)):\n\t\t\tif a[i] == b[i]:\n\t\t\t\tcontinue\n\t\t\telse:\n\t\t\t\treturn False\n\t\treturn True\n\telse:\n\t\treturn False\nn = int(input())\na = list(map(int, input().split()))\nb = list(map(int, input().split()))\nk1 = a[0]\nk2 = b[0]\na.pop(0)\nb.pop(0)\na_hat = a.copy()\nb_hat = b.copy()\nc = 0\nwhile True:\n\tif a[0] > b[0]:\n\t\ta.append(b.pop(0))\n\t\ta.append(a.pop(0))\n\t\tc += 1\n\telse:\n\t\tb.append(a.pop(0))\n\t\tb.append(b.pop(0))\n\t\tc += 1\n\tif len(a) == 0:\n\t\tprint(str(c) + ' 2')\n\t\tbreak\n\tif len(b) == 0:\n\t\tprint(str(c) + ' 1')\n\t\tbreak\n\tif array_equal(a, a_hat) and array_equal(b, b_hat) or c > 1000:\n\t\tprint(-1)\n\t\tbreak\n", "import sys\nimport math\nimport queue\n\ndef worker():\n\twhile True:\n\t\titem = q.get()\n\t\tdo_work(item)\n\t\tq.task_done()\nN = int(input())\na1 = input().split()\nn1 = int(a1[0])\nq1 = queue.Queue()\nfor x in a1[1:]:\n\tq1.put(int(x))\na1 = input().split()\nn2 = int(a1[0])\nq2 = queue.Queue()\nfor x in a1[1:]:\n\tq2.put(int(x))\ncou = 0\nwhile cou <= 200:\n\tcou += 1\n\ta = q1.get()\n\tb = q2.get()\n\tif a > b:\n\t\tq1.put(b)\n\t\tq1.put(a)\n\telse:\n\t\tq2.put(a)\n\t\tq2.put(b)\n\tif q1.empty():\n\t\tprint(cou, '2')\n\t\tbreak\n\tif q2.empty():\n\t\tprint(cou, '1')\n\t\tbreak\nelse:\n\tprint('-1')\n", "n = int(input())\nk1 = list(map(int, input().split()))[1:]\nk2 = list(map(int, input().split()))[1:]\nstate = dict()\ncounter = 0\nwhile k1 and k2:\n\tcounter += 1\n\tif counter >= 10000:\n\t\tbreak\n\t(x, y) = (k1.pop(0), k2.pop(0))\n\tif x > y:\n\t\tk1 += [y, x]\n\telse:\n\t\tk2 += [x, y]\nif counter == 10000:\n\tprint(-1)\nelse:\n\tprint(counter, 1 if k1 else 2)\n", "import sys, os, io\n\ndef rs():\n\treturn sys.stdin.readline().rstrip()\n\ndef ri():\n\treturn int(sys.stdin.readline())\n\ndef ria():\n\treturn list(map(int, sys.stdin.readline().split()))\n\ndef ws(s):\n\tsys.stdout.write(s + '\\n')\n\ndef wi(n):\n\tsys.stdout.write(str(n) + '\\n')\n\ndef wia(a):\n\tsys.stdout.write(' '.join([str(x) for x in a]) + '\\n')\nimport math, datetime, functools, itertools, operator, bisect, fractions, statistics\nfrom collections import deque, defaultdict, OrderedDict, Counter\nfrom fractions import Fraction\nfrom decimal import Decimal\nfrom sys import stdout\nfrom heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest\nsys.setrecursionlimit(111111)\nINF = 99999999999999999999999999999999\n\ndef main():\n\tmod = 1000000007\n\tstarttime = datetime.datetime.now()\n\tif os.path.exists('input.txt'):\n\t\tsys.stdin = open('input.txt', 'r')\n\t\tsys.stdout = open('output.txt', 'w')\n\ttc = 1\n\tfor _ in range(tc):\n\t\tn = ri()\n\t\tpl1 = ria()\n\t\tpl2 = ria()\n\t\t(n1, p1) = (pl1[0], deque(pl1[1:][::-1]))\n\t\t(n2, p2) = (pl2[0], deque(pl2[1:][::-1]))\n\t\td = {}\n\t\twhowon = 0\n\t\tfights = 0\n\t\td[tuple(p1), tuple(p2)] = 1\n\t\twhile len(p1) and len(p2):\n\t\t\tif p1[-1] > p2[-1]:\n\t\t\t\tp1.appendleft(p2.pop())\n\t\t\t\tp1.appendleft(p1.pop())\n\t\t\telse:\n\t\t\t\tp2.appendleft(p1.pop())\n\t\t\t\tp2.appendleft(p2.pop())\n\t\t\tif (tuple(p1), tuple(p2)) not in d:\n\t\t\t\td[tuple(p1), tuple(p2)] = 1\n\t\t\telse:\n\t\t\t\twhowon = -1\n\t\t\t\tbreak\n\t\t\tfights += 1\n\t\tif whowon == -1:\n\t\t\tprint(-1)\n\t\telif len(p1):\n\t\t\tprint(fights, 1)\n\t\telse:\n\t\t\tprint(fights, 2)\n\tendtime = datetime.datetime.now()\n\ttime = (endtime - starttime).total_seconds() * 1000\n\tif os.path.exists('input.txt'):\n\t\tprint('Time:', time, 'ms')\n\nclass FastReader(io.IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, fd, chunk_size=1024 * 8):\n\t\tself._fd = fd\n\t\tself._chunk_size = chunk_size\n\t\tself.buffer = io.BytesIO()\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self, size=-1):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\nclass FastWriter(io.IOBase):\n\n\tdef __init__(self, fd):\n\t\tself._fd = fd\n\t\tself.buffer = io.BytesIO()\n\t\tself.write = self.buffer.write\n\n\tdef flush(self):\n\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass FastStdin(io.IOBase):\n\n\tdef __init__(self, fd=0):\n\t\tself.buffer = FastReader(fd)\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n\nclass FastStdout(io.IOBase):\n\n\tdef __init__(self, fd=1):\n\t\tself.buffer = FastWriter(fd)\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.flush = self.buffer.flush\nsys.stdin = FastStdin()\nsys.stdout = FastStdout()\nmain()\n", "from collections import deque\nn = int(input())\nt = [int(i) for i in input().split()]\nc1 = deque(t[1:])\nt = [int(i) for i in input().split()]\nc2 = deque(t[1:])\nf = 0\nwhile len(c1) > 0 and len(c2) > 0 and (f < 10 ** 6):\n\tf += 1\n\td1 = c1.popleft()\n\td2 = c2.popleft()\n\tif d1 > d2:\n\t\tc1.extend([d2, d1])\n\telse:\n\t\tc2.extend([d1, d2])\nif f == 10 ** 6:\n\tprint(-1)\nelif len(c1) != 0:\n\tprint(f, 1)\nelse:\n\tprint(f, 2)\n", "from sys import stdin, stdout\ninput = stdin.readline\n\ndef write(n, sep='\\n'):\n\tstdout.write(str(n))\n\tstdout.write(sep)\n\ndef gil():\n\treturn list(map(int, input().split()))\nn = int(input())\n(a, *ka) = gil()\n(b, *kb) = gil()\ns = []\nc = 0\nwhile len(ka) > 0 and len(kb) > 0:\n\tc += 1\n\tx = (ka.copy(), kb.copy())\n\tif x in s:\n\t\tprint(-1)\n\t\tbreak\n\ts.append(x)\n\ta = ka.pop(0)\n\tb = kb.pop(0)\n\tif a > b:\n\t\tka.append(b)\n\t\tka.append(a)\n\telse:\n\t\tkb.append(a)\n\t\tkb.append(b)\nelse:\n\tif len(ka) == 0:\n\t\ty = 2\n\telse:\n\t\ty = 1\n\tprint(c, y)\n", "from collections import deque\nn = input()\na = deque(list(map(int, input().split()))[1:])\nb = deque(list(map(int, input().split()))[1:])\nit = 0\nwhile a and b:\n\tx = a.popleft()\n\ty = b.popleft()\n\tif x > y:\n\t\ta.append(y)\n\t\ta.append(x)\n\telse:\n\t\tb.append(x)\n\t\tb.append(y)\n\tit += 1\n\tif it >= 999999:\n\t\tbreak\nif not a:\n\tprint(it, 2)\nelif not b:\n\tprint(it, 1)\nelse:\n\tprint(-1)\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\nmod = 10 ** 9 + 7\nfrom collections import deque\n\ndef main():\n\tn = int(input())\n\tone = deque(map(int, input().split()))\n\ttwo = deque(map(int, input().split()))\n\tone.popleft()\n\ttwo.popleft()\n\tc = one.copy()\n\td = two.copy()\n\tans = 0\n\twin = 0\n\twhile True:\n\t\tif ans > 100000:\n\t\t\twin = 0\n\t\t\tbreak\n\t\tans += 1\n\t\tif one[0] > two[0]:\n\t\t\ttemp = two.popleft()\n\t\t\ttemp2 = one.popleft()\n\t\t\tone.append(temp)\n\t\t\tone.append(temp2)\n\t\telse:\n\t\t\ttemp = one.popleft()\n\t\t\ttemp2 = two.popleft()\n\t\t\ttwo.append(temp)\n\t\t\ttwo.append(temp2)\n\t\tif len(one) == 0:\n\t\t\twin = 2\n\t\t\tbreak\n\t\telif len(two) == 0:\n\t\t\twin = 1\n\t\t\tbreak\n\t\telif one[0] == c[0] and two[0] == d[0]:\n\t\t\tif one == c and two == d:\n\t\t\t\twin = 0\n\t\t\t\tbreak\n\tif win:\n\t\tprint(ans, win)\n\telse:\n\t\tprint(-1)\n\ndef nouse0():\n\ta = 420\n\tb = 420\n\tprint(f'i am nitish{(a + b) // 2}')\n\ndef nouse1():\n\ta = 420\n\tb = 420\n\tprint(f'i am nitish{(a + b) // 2}')\n\ndef nouse2():\n\ta = 420\n\tb = 420\n\tprint(f'i am nitish{(a + b) // 2}')\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\n\ndef nouse3():\n\ta = 420\n\tb = 420\n\tprint(f'i am nitish{(a + b) // 2}')\n\ndef nouse4():\n\ta = 420\n\tb = 420\n\tprint(f'i am nitish{(a + b) // 2}')\n\ndef nouse5():\n\ta = 420\n\tb = 420\n\tprint(f'i am nitish{(a + b) // 2}')\nmain()\n", "import sys\nfrom datetime import datetime\nn = int(input())\nk1 = input().split()\nk2 = input().split()\nk1 = [int(x) for x in k1]\nk2 = [int(x) for x in k2]\nk1.pop(0)\nk2.pop(0)\niteration = 0\nwhile len(k1) != 0 and len(k2) != 0:\n\tkarta_1 = k1[0]\n\tkarta_2 = k2[0]\n\tif karta_2 > karta_1:\n\t\tk1.pop(0)\n\t\tk2.pop(0)\n\t\tk2.append(karta_1)\n\t\tk2.append(karta_2)\n\tif karta_2 < karta_1:\n\t\tk2.pop(0)\n\t\tk1.pop(0)\n\t\tk1.append(karta_2)\n\t\tk1.append(karta_1)\n\titeration = iteration + 1\n\tif iteration > 150:\n\t\tbreak\nif iteration > 150:\n\tprint(-1)\n\tsys.exit()\nif len(k2) != 0:\n\tprint(iteration, 2)\nif len(k1) != 0:\n\tprint(iteration, 1)\n", "import math\nimport random\nfrom queue import Queue\n\ndef main(arr1, arr2):\n\tq1 = Queue(maxsize=0)\n\tq2 = Queue(maxsize=0)\n\tseen = set([])\n\tans = 0\n\twhile len(arr1) != 0 and len(arr2) != 0:\n\t\te = (tuple(arr1), tuple(arr2))\n\t\tif e not in seen:\n\t\t\tseen.add(e)\n\t\telse:\n\t\t\tprint(-1)\n\t\t\treturn\n\t\tval1 = arr1.pop(0)\n\t\tval2 = arr2.pop(0)\n\t\tif val1 > val2:\n\t\t\tarr1.append(val2)\n\t\t\tarr1.append(val1)\n\t\telse:\n\t\t\tarr2.append(val1)\n\t\t\tarr2.append(val2)\n\t\tans += 1\n\tif len(arr1) == 0:\n\t\tprint(ans, 2)\n\telse:\n\t\tprint(ans, 1)\n\treturn\nn = int(input())\narr1 = list(map(int, input().split()))[1:]\narr2 = list(map(int, input().split()))[1:]\nmain(arr1, arr2)\n", "from collections import deque\nn = int(input())\nlist1 = list(input().split())\nlist2 = list(input().split())\nk1 = int(list1[0])\nk2 = int(list2[0])\nq1 = deque()\nq2 = deque()\nfor i in range(1, k1 + 1):\n\tq1.append(list1[i])\nfor i in range(1, k2 + 1):\n\tq2.append(list2[i])\nfights = 0\nseen = set()\nseen.add((''.join(q1), ''.join(q2)))\nnever_end = False\nwhile len(q1) and len(q2):\n\tfights += 1\n\ta = q1.popleft()\n\tb = q2.popleft()\n\tif (''.join(q1), ''.join(q2)) in seen:\n\t\tnever_end = True\n\t\tbreak\n\telse:\n\t\tseen.add((''.join(q1), ''.join(q2)))\n\tif int(a) > int(b):\n\t\tq1.append(b)\n\t\tq1.append(a)\n\telse:\n\t\tq2.append(a)\n\t\tq2.append(b)\nif never_end:\n\tprint(-1)\nelif len(q1):\n\tprint(fights, 1)\nelse:\n\tprint(fights, 2)\n", "class Queue(object):\n\n\tdef __init__(self):\n\t\tself.items = []\n\n\tdef enque(self, new):\n\t\tself.items.append(new)\n\n\tdef deque(self):\n\t\treturn self.items.pop(0)\n\n\tdef peek(self):\n\t\treturn self.items[0]\n\n\tdef isempty(self):\n\t\treturn self.items == []\n\n\tdef size(self):\n\t\treturn len(self.iems)\n\n\tdef __eq__(self, other):\n\t\treturn self.items == other.items\nn = int(input())\nL1 = [int(s) for s in input().split()]\nL2 = [int(s) for s in input().split()]\n(Q1, Q2) = (Queue(), Queue())\nfor i in range(1, len(L1)):\n\tQ1.enque(L1[i])\nfor i in range(1, len(L2)):\n\tQ2.enque(L2[i])\nc = 0\nwhile not Q1.isempty() and (not Q2.isempty()) and (c <= 2 ** n):\n\t(r1, r2) = (Q1.deque(), Q2.deque())\n\tif r1 > r2:\n\t\tQ1.enque(r2)\n\t\tQ1.enque(r1)\n\telse:\n\t\tQ2.enque(r1)\n\t\tQ2.enque(r2)\n\tc += 1\nif Q1.isempty():\n\tprint(' '.join([str(c), '2']))\nelif Q2.isempty():\n\tprint(' '.join([str(c), '1']))\nelse:\n\tprint(-1)\n", "n = int(input())\na = tuple(map(int, input().split()))[1:]\nb = tuple(map(int, input().split()))[1:]\nc = 0\ns = set()\nres = 0\nwhile (a, b) not in s:\n\tif not a:\n\t\tres = 2\n\t\tbreak\n\tif not b:\n\t\tres = 1\n\t\tbreak\n\ts.add((a, b))\n\t(x, y) = (a[0], b[0])\n\t(a, b) = (a[1:], b[1:])\n\tc += 1\n\tif x < y:\n\t\tb = b + (x, y)\n\telse:\n\t\ta = a + (y, x)\nif res == 0:\n\tprint(-1)\nelse:\n\tprint(c, res)\n", "for t in range(1):\n\tn = int(input())\n\trow1 = list(map(int, input().split()))\n\trow2 = list(map(int, input().split()))\n\trow1 = row1[1:]\n\trow2 = row2[1:]\n\tbroken = 0\n\tfor i in range(1, 10 ** 4):\n\t\tele1 = row1.pop(0)\n\t\tele2 = row2.pop(0)\n\t\tif ele1 > ele2:\n\t\t\trow1.append(ele2)\n\t\t\trow1.append(ele1)\n\t\telse:\n\t\t\trow2.append(ele1)\n\t\t\trow2.append(ele2)\n\t\tif len(row1) == 0:\n\t\t\tprint(i, 2)\n\t\t\tbroken += 1\n\t\t\tbreak\n\t\telif len(row2) == 0:\n\t\t\tprint(i, 1)\n\t\t\tbroken += 1\n\t\t\tbreak\n\t\telse:\n\t\t\tcontinue\n\tif broken == 0:\n\t\tprint(-1)\n", "N = int(input())\nfrom collections import deque\nA = deque()\nB = deque()\nfor x in range(2):\n\ta = input().split()\n\tif x == 0:\n\t\tfor y in range(1, len(a)):\n\t\t\tA.append(int(a[y]))\n\telse:\n\t\tfor y in range(1, len(a)):\n\t\t\tB.append(int(a[y]))\nbr = 0\np = 0\nwhile len(A) != 0 and len(B) != 0 and (br < 300000):\n\tc = A.popleft()\n\td = B.popleft()\n\tif c > d:\n\t\tA.append(d)\n\t\tA.append(c)\n\t\tp = 1\n\telse:\n\t\tB.append(c)\n\t\tB.append(d)\n\t\tp = 2\n\tbr += 1\nif br >= 300000:\n\tprint(-1)\nelse:\n\tprint(br, p)\n", "n = int(input())\np1 = input().strip().split()\np1 = [int(i) for i in p1[1:]]\np2 = input().strip().split()\np2 = [int(i) for i in p2[1:]]\ncomb = set()\ncont = 0\nwhile True:\n\tif tuple(p1 + [0] + p2) in comb:\n\t\tprint(-1)\n\t\tbreak\n\tif len(p1) == 0:\n\t\tprint(cont, 2)\n\t\tbreak\n\tif len(p2) == 0:\n\t\tprint(cont, 1)\n\t\tbreak\n\tcont += 1\n\tcomb.add(tuple(p1 + [0] + p2))\n\tp1_card = p1.pop(0)\n\tp2_card = p2.pop(0)\n\tif p2_card < p1_card:\n\t\tp1 += [p2_card, p1_card]\n\telse:\n\t\tp2 += [p1_card, p2_card]\n", "from math import factorial\nn = int(input())\ns1 = [int(i) for i in input().split()][1:]\ns2 = [int(i) for i in input().split()][1:]\n(i, finished, N) = (1, False, 120)\nwhile s1 and s2:\n\t(x, y) = (s1.pop(0), s2.pop(0))\n\tif x > y:\n\t\ts1 += [y, x]\n\telse:\n\t\ts2 += [x, y]\n\tif i == N:\n\t\tprint(-1)\n\t\tbreak\n\ti = i + 1\nif not s1:\n\tprint(i - 1, 2)\nelif not s2:\n\tprint(i - 1, 1)\n", "from collections import Counter, defaultdict, deque\nfrom math import factorial as fact\nt = int(input())\n\ndef sim(a1, a2):\n\tif a1[0] > a2[0]:\n\t\ta = a2.pop(0)\n\t\tb = a1.pop(0)\n\t\ta1.extend([a, b])\n\telse:\n\t\ta = a1.pop(0)\n\t\tb = a2.pop(0)\n\t\ta2.extend([a, b])\nvisited = set()\nc = 0\ns1 = [int(x) for x in input().split()]\ns2 = [int(x) for x in input().split()]\ns1.pop(0)\ns2.pop(0)\nwhile True:\n\tif len(s1) == 0 or len(s2) == 0:\n\t\tbreak\n\th = hash(str([s1, s2]))\n\tif h in visited:\n\t\tprint(-1)\n\t\texit()\n\tvisited.add(h)\n\tsim(s1, s2)\n\tc += 1\nif len(s1) == 0:\n\tprint(c, 2)\nelse:\n\tprint(c, 1)\n", "n = int(input())\na = list(map(int, input().split()))\nk1 = a[0]\ns1 = a[1:]\na = list(map(int, input().split()))\nk2 = a[0]\ns2 = a[1:]\nflag = 0\nfights = 0\ncount = 0\nwhile len(s1) != 0 and len(s2) != 0:\n\tcount += 1\n\tif count > 1000000:\n\t\tflag = 1\n\t\tbreak\n\tt = s1.pop(0)\n\tp = s2.pop(0)\n\tif t > p:\n\t\ts1.append(p)\n\t\ts1.append(t)\n\telse:\n\t\ts2.append(t)\n\t\ts2.append(p)\n\tfights += 1\nif flag == 1:\n\tprint('-1')\nelse:\n\tif len(s1) == 0:\n\t\twon = '2'\n\telse:\n\t\twon = '1'\n\tprint(fights, won, sep=' ')\n", "n = int(input())\n\ndef inp(s):\n\tj = 0\n\ta = []\n\tfor i in range(len(s)):\n\t\tif s[i] == ' ':\n\t\t\ta.append(int(s[j:i]))\n\t\t\tj = i + 1\n\t\tif i == len(s) - 1:\n\t\t\ta.append(int(s[j:]))\n\treturn a\ns1 = input()\ns2 = input()\na = inp(s1)\nb = inp(s2)\na = a[1:]\nb = b[1:]\np = [a]\nq = [b]\ni = 0\nwhile len(a) != 0 and len(b) != 0:\n\tif a[0] > b[0]:\n\t\tx = a[0]\n\t\ty = b[0]\n\t\ta = a[1:]\n\t\tb = b[1:]\n\t\ta.append(y)\n\t\ta.append(x)\n\telse:\n\t\tx = a[0]\n\t\ty = b[0]\n\t\ta = a[1:]\n\t\tb = b[1:]\n\t\tb.append(x)\n\t\tb.append(y)\n\ti = i + 1\n\tif a in p and b in q:\n\t\ti = -1\n\t\tbreak\n\tp.append(a)\n\tq.append(b)\nprint(str(i) + ' ', end='')\nif len(a) == 0:\n\tprint(2)\nelif len(b) == 0:\n\tprint(1)\n", "from sys import stdin, stdout\nfrom math import gcd, ceil, sqrt\nii1 = lambda : int(stdin.readline().strip())\nis1 = lambda : stdin.readline().strip()\niia = lambda : list(map(int, stdin.readline().strip().split()))\nisa = lambda : stdin.readline().strip().split()\nmod = 1000000007\nn = ii1()\n(_, *a) = iia()\n(_, *b) = iia()\nif len(b) == 0:\n\tprint(0, 1)\nelse:\n\t(old_a, old_b) = (set(), set())\n\tres = 0\n\twhile True:\n\t\told_a.add(tuple(a))\n\t\told_b.add(tuple(b))\n\t\tcur_a = a.pop(0)\n\t\tcur_b = b.pop(0)\n\t\tres += 1\n\t\tif cur_a > cur_b:\n\t\t\ta.extend([cur_b, cur_a])\n\t\telse:\n\t\t\tb.extend([cur_a, cur_b])\n\t\tif not len(a):\n\t\t\tprint(res, 2)\n\t\t\tbreak\n\t\tif not len(b):\n\t\t\tprint(res, 1)\n\t\t\tbreak\n\t\tif tuple(a) in old_a and tuple(b) in old_b:\n\t\t\tprint(-1)\n\t\t\tbreak\n", "n = input()\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\nr = 0\nwhile a and b:\n\tr += 1\n\tif r > 1000:\n\t\tprint(-1)\n\t\texit(0)\n\t(x, y) = (a.pop(0), b.pop(0))\n\tif x > y:\n\t\ta += [y, x]\n\telse:\n\t\tb += [x, y]\nif a:\n\tprint(r, 1)\nelse:\n\tprint(r, 2)\n", "n = int(input())\ns1 = list(map(int, input().split()))[1:]\ns2 = list(map(int, input().split()))[1:]\nc = 0\nwhile s1 and s2 and (c < 10 ** 5):\n\tif s1[0] > s2[0]:\n\t\ts1.append(s2[0])\n\t\ts1.append(s1[0])\n\telse:\n\t\ts2.append(s1[0])\n\t\ts2.append(s2[0])\n\ts1.pop(0)\n\ts2.pop(0)\n\tc += 1\nif not s1:\n\tprint(c, 2)\nelif not s2:\n\tprint(c, 1)\nelse:\n\tprint(-1)\n", "import queue\nn = int(input())\nx = [int(j) for j in input().split()]\ny = [int(j) for j in input().split()]\nk1 = x[0]\nk2 = y[0]\na = x[1:]\nb = y[1:]\ns = ()\nc = 0\nq1 = queue.Queue()\nq2 = queue.Queue()\nfor i in range(k1):\n\tq1.put(a[i])\nfor i in range(k2):\n\tq2.put(b[i])\nwhile q1.empty() != True and q2.empty() != True:\n\tc1 = q1.get()\n\tc2 = q2.get()\n\tif c1 > c2:\n\t\tq1.put(c2)\n\t\tq1.put(c1)\n\telse:\n\t\tq2.put(c1)\n\t\tq2.put(c2)\n\tc += 1\n\tif c > 400:\n\t\tprint(-1)\n\t\texit(0)\n\t\tbreak\nif q1.empty():\n\tk = 2\nelse:\n\tk = 1\nprint(c, k)\n", "def helper(s1, s2, visited, move):\n\t(n, m) = (len(s1), len(s2))\n\tif n == 0:\n\t\tprint(move, '2')\n\t\treturn\n\tif m == 0:\n\t\tprint(move, '1')\n\t\treturn\n\tif tuple([tuple(s1), tuple(s2)]) in visited:\n\t\tprint('-1')\n\t\treturn\n\telse:\n\t\tvisited.add(tuple([tuple(s1), tuple(s2)]))\n\tif s1[0] > s2[0]:\n\t\ta = s2.pop(0)\n\t\ts1 += [a, s1[0]]\n\t\ts1.pop(0)\n\telse:\n\t\ta = s1.pop(0)\n\t\ts2 += [a, s2[0]]\n\t\ts2.pop(0)\n\thelper(s1, s2, visited, move + 1)\nn = [int(x) for x in input().split()]\na1 = [int(x) for x in input().split()]\na2 = [int(x) for x in input().split()]\nvisited = set()\nhelper(a1[1:], a2[1:], visited, 0)\n", "cards = int(input())\nstart_first = input().split()[1:]\nstart_second = input().split()[1:]\nhist = []\nf = [c for c in start_first]\ns = [c for c in start_second]\nrounds = 0\nwhile f and s:\n\t(c_f, c_s) = (f.pop(0), s.pop(0))\n\tif int(c_f) > int(c_s):\n\t\tf.append(c_s)\n\t\tf.append(c_f)\n\telse:\n\t\ts.append(c_f)\n\t\ts.append(c_s)\n\trounds += 1\n\tif (f, s) in hist:\n\t\tprint(-1)\n\t\tbreak\n\thist.append(([x for x in f], [x for x in s]))\nelse:\n\tprint(rounds, (1, 2)[f == []])\n", "from sys import exit\nn = int(input())\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\nused = set()\ncnt = 0\nwhile len(a) != 0 and len(b) != 0:\n\tif (tuple(a), tuple(b)) not in used:\n\t\tused.add((tuple(a), tuple(b)))\n\telse:\n\t\tprint(-1)\n\t\texit(0)\n\t(x, y) = (a.pop(0), b.pop(0))\n\tif x > y:\n\t\ta.append(y)\n\t\ta.append(x)\n\telse:\n\t\tb.append(x)\n\t\tb.append(y)\n\tcnt += 1\nprint(cnt, '1' if len(b) == 0 else '2')\n", "def getHash(a, b):\n\tif len(a) == 0 or len(b) == 0:\n\t\treturn 0\n\th = len(a)\n\tfor i in range(len(a)):\n\t\th = h * 10 + a[i]\n\tfor i in range(len(b)):\n\t\th = h * 10 + b[i]\n\treturn h\n\ndef update(a, b):\n\tif a[0] > b[0]:\n\t\ta.append(b[0])\n\t\ta.append(a[0])\n\t\ta.pop(0)\n\t\tb.pop(0)\n\telif a[0] < b[0]:\n\t\tb.append(a[0])\n\t\tb.append(b[0])\n\t\ta.pop(0)\n\t\tb.pop(0)\n\ndef main():\n\tn = int(input())\n\ta = list(map(int, input().split(' ')))\n\ta = a[1:len(a)]\n\tb = list(map(int, input().split(' ')))\n\tb = b[1:len(b)]\n\tcnt = 1\n\tfor i in range(n, 1, -1):\n\t\tcnt *= i\n\tstate = set()\n\tfor i in range(cnt):\n\t\tupdate(a, b)\n\t\th = int(getHash(a, b))\n\t\tif h == 0:\n\t\t\tprint(i + 1, (len(a) == 0) + 1)\n\t\t\tbreak\n\t\tif h in state:\n\t\t\tprint(-1)\n\t\t\tbreak\n\t\telse:\n\t\t\tstate.add(h)\nmain()\n", "n = int(input())\np1 = list(map(int, input().split()))\np2 = list(map(int, input().split()))\np1_cards = p1.pop(0)\np2_cards = p2.pop(0)\nvisited1 = []\nvisited2 = []\nwinner = -1\nvisited1.append(list(p1))\nvisited2.append(list(p2))\nfights = 0\nwhile True:\n\tif len(p1) == 0:\n\t\twinner = 2\n\t\tprint(str(fights) + '  ' + str(winner))\n\t\tbreak\n\tif len(p2) == 0:\n\t\twinner = 1\n\t\tprint(str(fights) + '  ' + str(winner))\n\t\tbreak\n\tif p1[0] >= p2[0]:\n\t\tp1.append(p2.pop(0))\n\t\tp1.append(p1.pop(0))\n\telse:\n\t\tp2.append(p1.pop(0))\n\t\tp2.append(p2.pop(0))\n\tif p1 in visited1 and p2 in visited2 or (p2 in visited1 and p1 in visited2):\n\t\tprint(winner)\n\t\tbreak\n\tvisited1.append(list(p1))\n\tvisited2.append(list(p2))\n\tfights += 1\n", "N = input()\nlist1 = [int(x) for x in input().split()]\nlist2 = [int(x) for x in input().split()]\nlist1.pop(0)\nlist2.pop(0)\ns1 = tuple(map(int, list1))\ns2 = tuple(map(int, list2))\nalready_seen = []\nnum_fights = 0\nwhile len(list1) > 0 and len(list2) > 0:\n\tnum_fights += 1\n\tif (s1, s2) in already_seen:\n\t\tbreak\n\telse:\n\t\talready_seen.append((s1, s2))\n\tif list1[0] < list2[0]:\n\t\tlist2.append(list1[0])\n\t\tlist2.append(list2[0])\n\t\tlist2.pop(0)\n\t\tlist1.pop(0)\n\telse:\n\t\tlist1.append(list2[0])\n\t\tlist1.append(list1[0])\n\t\tlist2.pop(0)\n\t\tlist1.pop(0)\n\ts1 = tuple(map(int, list1))\n\ts2 = tuple(map(int, list2))\nif len(list1) == 0:\n\tprint(str(num_fights) + ' 2')\nelif len(list2) == 0:\n\tprint(str(num_fights) + ' 1')\nelse:\n\tprint(-1)\n", "n = int(input())\na = [int(x) for x in input().split()]\na = a[1:]\nb = [int(x) for x in input().split()]\nb = b[1:]\nk = 0\nwhile len(a) > 0 and len(b) > 0 and (k <= 10 ** 3):\n\tif a[0] > b[0] and (not (a[0] == 0 and b[0] == 9) and (not (a[0] == 9 and b[0] == 0))):\n\t\ta.append(b[0])\n\t\ta.append(a[0])\n\t\ta.pop(0)\n\t\tb.pop(0)\n\t\tk += 1\n\telse:\n\t\tb.append(a[0])\n\t\tb.append(b[0])\n\t\ta.pop(0)\n\t\tb.pop(0)\n\t\tk += 1\nif len(a) != 0 and len(b) != 0:\n\tprint(-1)\nelif len(a) > 0:\n\tprint(k, 1)\nelse:\n\tprint(k, 2)\n", "n = int(input())\nstring1 = input()\nstring2 = input()\nlist1 = string1.split(' ')\nlist2 = string2.split(' ')\ndel list1[0]\ndel list2[0]\npile1 = []\npile2 = []\nfor i in range(len(list1)):\n\tpile1.append(int(list1[i]))\nfor i in range(len(list2)):\n\tpile2.append(int(list2[i]))\nstarting1 = pile1.copy()\nstarting2 = pile2.copy()\ncount = 0\nwhile True:\n\tcount += 1\n\tx = pile1[0]\n\ty = pile2[0]\n\tdel pile1[0]\n\tdel pile2[0]\n\tif x > y:\n\t\tpile1.append(y)\n\t\tpile1.append(x)\n\tif y > x:\n\t\tpile2.append(x)\n\t\tpile2.append(y)\n\tif len(pile1) == 0:\n\t\tprint(str(count) + ' 2')\n\t\tbreak\n\tif len(pile2) == 0:\n\t\tprint(str(count) + ' 1')\n\t\tbreak\n\tif pile1 == starting1 and pile2 == starting2:\n\t\tprint('-1')\n\t\tbreak\n\tif count > 326592:\n\t\tprint('-1')\n\t\tbreak\n", "n = int(input().strip())\na = list(map(int, input().strip().split()[1:]))\nb = list(map(int, input().strip().split()[1:]))\nsteps = 0\nwhile len(a) and len(b):\n\tx = a.pop(0)\n\ty = b.pop(0)\n\tif x > y:\n\t\ta += [y, x]\n\telse:\n\t\tb += [x, y]\n\tsteps += 1\n\tif steps > 110:\n\t\tprint(-1)\n\t\tquit()\nif a == []:\n\tprint(steps, 2)\nelse:\n\tprint(steps, 1)\n", "n = int(input())\na = [int(i) for i in input().split()]\na.remove(a[0])\nb = [int(i) for i in input().split()]\nb.remove(b[0])\nk = 0\nwhile len(a) > 0 and len(b) > 0:\n\tif a[0] > b[0]:\n\t\ta.append(b[0])\n\t\ta.append(a[0])\n\t\ta.remove(a[0])\n\t\tb.remove(b[0])\n\telse:\n\t\tb.append(a[0])\n\t\tb.append(b[0])\n\t\tb.remove(b[0])\n\t\ta.remove(a[0])\n\tk += 1\n\tif k > 700000:\n\t\tprint(-1)\n\t\texit(0)\nif len(a) > 0:\n\tprint(k, 1)\nelse:\n\tprint(k, 2)\n", "from __future__ import division, print_function\nimport os, sys\nfrom io import BytesIO, IOBase\nif sys.version_info[0] < 3:\n\tfrom __builtin__ import xrange as range\n\tfrom future_builtins import ascii, filter, hex, map, oct, zip\n\ndef ii():\n\treturn int(input())\n\ndef si():\n\treturn input()\n\ndef mi():\n\treturn map(int, input().strip().split(' '))\n\ndef msi():\n\treturn map(str, input().strip().split(' '))\n\ndef li():\n\treturn list(mi())\n\ndef dmain():\n\tsys.setrecursionlimit(1000000)\n\tthreading.stack_size(1024000)\n\tthread = threading.Thread(target=main)\n\tthread.start()\nimport sys\ninput = sys.stdin.readline\nscanner = lambda : int(input())\nstring = lambda : input().rstrip()\nget_list = lambda : list(read())\nread = lambda : map(int, input().split())\nget_float = lambda : map(float, input().split())\n\ndef ceil(x):\n\tif x != int(x):\n\t\tx = int(x) + 1\n\treturn x\n\ndef factorial(x, m):\n\tval = 1\n\twhile x > 0:\n\t\tval = val * x % m\n\t\tx -= 1\n\treturn val\n\ndef fact(x):\n\tval = 1\n\twhile x > 0:\n\t\tval *= x\n\t\tx -= 1\n\treturn val\n\ndef swaparr(arr, a, b):\n\ttemp = arr[a]\n\tarr[a] = arr[b]\n\tarr[b] = temp\n\ndef gcd(a, b):\n\tif b == 0:\n\t\treturn a\n\treturn gcd(b, a % b)\n\ndef lcm(a, b):\n\treturn a * b // math.gcd(a, b)\n\ndef is_integer(n):\n\treturn math.ceil(n) == math.floor(n)\n\ndef nCr(n, k):\n\tif k > n:\n\t\treturn 0\n\tif k > n - k:\n\t\tk = n - k\n\tres = 1\n\tfor i in range(k):\n\t\tres = res * (n - i)\n\t\tres = res / (i + 1)\n\treturn int(res)\n\ndef primefs(n):\n\tprimes = {}\n\twhile n % 2 == 0 and n > 0:\n\t\tprimes[2] = primes.get(2, 0) + 1\n\t\tn = n // 2\n\tfor i in range(3, int(n ** 0.5) + 2, 2):\n\t\twhile n % i == 0 and n > 0:\n\t\t\tprimes[i] = primes.get(i, 0) + 1\n\t\t\tn = n // i\n\tif n > 2:\n\t\tprimes[n] = primes.get(n, 0) + 1\n\treturn primes\n\ndef power(x, y, p):\n\tres = 1\n\tx = x % p\n\tif x == 0:\n\t\treturn 0\n\twhile y > 0:\n\t\tif y & 1 == 1:\n\t\t\tres = res * x % p\n\t\ty = y >> 1\n\t\tx = x * x % p\n\treturn res\n\ndef swap(a, b):\n\ttemp = a\n\ta = b\n\tb = temp\n\treturn (a, b)\n\ndef find(x, link):\n\tp = x\n\twhile p != link[p]:\n\t\tp = link[p]\n\twhile x != p:\n\t\tnex = link[x]\n\t\tlink[x] = p\n\t\tx = nex\n\treturn p\n\ndef union(x, y, link, size):\n\tx = find(x, link)\n\ty = find(y, link)\n\tif size[x] < size[y]:\n\t\t(x, y) = swap(x, y)\n\tif x != y:\n\t\tsize[x] += size[y]\n\t\tlink[y] = x\n\ndef sieve(n):\n\tprime = [True for i in range(n + 1)]\n\t(prime[0], prime[1]) = (False, False)\n\tp = 2\n\twhile p * p <= n:\n\t\tif prime[p] == True:\n\t\t\tfor i in range(p * p, n + 1, p):\n\t\t\t\tprime[i] = False\n\t\tp += 1\n\treturn prime\n\ndef phi(n):\n\tresult = n\n\tp = 2\n\twhile p * p <= n:\n\t\tif n % p == 0:\n\t\t\twhile n % p == 0:\n\t\t\t\tn = n // p\n\t\t\tresult = result * (1.0 - 1.0 / float(p))\n\t\tp = p + 1\n\tif n > 1:\n\t\tresult = result * (1.0 - 1.0 / float(n))\n\treturn int(result)\n\ndef is_prime(n):\n\tif n == 0:\n\t\treturn False\n\tif n == 1:\n\t\treturn True\n\tfor i in range(2, int(n ** (1 / 2)) + 1):\n\t\tif not n % i:\n\t\t\treturn False\n\treturn True\n\ndef next_prime(n, primes):\n\twhile primes[n] != True:\n\t\tn += 1\n\treturn n\nMAXN = int(100000.0 + 5)\n\ndef spf_sieve():\n\tspf[1] = 1\n\tfor i in range(2, MAXN):\n\t\tspf[i] = i\n\tfor i in range(4, MAXN, 2):\n\t\tspf[i] = 2\n\tfor i in range(3, ceil(MAXN ** 0.5), 2):\n\t\tif spf[i] == i:\n\t\t\tfor j in range(i * i, MAXN, i):\n\t\t\t\tif spf[j] == j:\n\t\t\t\t\tspf[j] = i\nspf = [0 for i in range(MAXN)]\n\ndef factoriazation(x):\n\tres = []\n\tfor i in range(2, int(x ** 0.5) + 1):\n\t\twhile x % i == 0:\n\t\t\tres.append(i)\n\t\t\tx //= i\n\tif x != 1:\n\t\tres.append(x)\n\treturn res\n\ndef factors(n):\n\tres = []\n\tfor i in range(1, int(n ** 0.5) + 1):\n\t\tif n % i == 0:\n\t\t\tres.append(i)\n\t\t\tres.append(n // i)\n\treturn list(set(res))\n\ndef int_array():\n\treturn list(map(int, input().strip().split()))\n\ndef float_array():\n\treturn list(map(float, input().strip().split()))\n\ndef str_array():\n\treturn input().strip().split()\n\ndef binary_search(low, high, w, h, n):\n\twhile low < high:\n\t\tmid = low + (high - low) // 2\n\t\tif check(mid, w, h, n):\n\t\t\tlow = mid + 1\n\t\telse:\n\t\t\thigh = mid\n\treturn low\n\ndef check(beauty, s, n, count):\n\tpass\nMOD = int(1000000000.0) + 7\nCMOD = 998244353\nINF = float('inf')\nNINF = -float('inf')\nalphs = 'abcdefghijklmnopqrstuvwxyz'\nfrom itertools import permutations\nimport math\nimport bisect as bis\nimport random\nimport sys\nimport collections as collect\n\ndef solve():\n\tn = scanner()\n\t(l, *a) = get_list()\n\t(r, *b) = get_list()\n\ta = collect.deque(a)\n\tb = collect.deque(b)\n\ts = 0\n\twhile a and b:\n\t\ts += 1\n\t\tif s > 1000:\n\t\t\tprint(-1)\n\t\t\treturn\n\t\t(x, y) = (a.popleft(), b.popleft())\n\t\tif x > y:\n\t\t\ta.append(y)\n\t\t\ta.append(x)\n\t\telse:\n\t\t\tb.append(x)\n\t\t\tb.append(y)\n\tif a:\n\t\tprint(s, 1)\n\telse:\n\t\tprint(s, 2)\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n\ndef print(*args, **kwargs):\n\t(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))\n\tat_start = True\n\tfor x in args:\n\t\tif not at_start:\n\t\t\tfile.write(sep)\n\t\tfile.write(str(x))\n\t\tat_start = False\n\tfile.write(kwargs.pop('end', '\\n'))\n\tif kwargs.pop('flush', False):\n\t\tfile.flush()\nif sys.version_info[0] < 3:\n\t(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))\nelse:\n\t(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nfor i in range(1):\n\tsolve()\n", "n = int(input())\na1 = list(map(int, input().split()))[1:]\na2 = list(map(int, input().split()))[1:]\ncnt = 0\nwhile a1 != [] and a2 != []:\n\tcnt += 1\n\tif cnt > 1000000:\n\t\tprint(-1)\n\t\texit(0)\n\t(t1, t2) = (a1.pop(0), a2.pop(0))\n\tif t1 > t2:\n\t\ta1 += [t2, t1]\n\telse:\n\t\ta2 += [t1, t2]\nprint(cnt, 1 if a1 != [] else 2)\n", "from typing import List, Union\n\ndef solve(n: int, k1: List[int], k2: List[int]) -> Union[int, str]:\n\tvisited = set()\n\tpos = (k1[1:], k2[1:])\n\tfights = 0\n\twhile True:\n\t\tif str(pos) in visited:\n\t\t\treturn -1\n\t\tif len(pos[0]) == 0 or len(pos[1]) == 0:\n\t\t\treturn '{0} {1}'.format(fights, [1, 2][len(pos[0]) == 0])\n\t\tvisited.add(str(pos))\n\t\tfights += 1\n\t\tif pos[0][0] > pos[1][0]:\n\t\t\tpos = (pos[0][1:] + [pos[1][0], pos[0][0]], pos[1][1:])\n\t\telse:\n\t\t\tpos = (pos[0][1:], pos[1][1:] + [pos[0][0], pos[1][0]])\nn = input()\nk1 = list(map(int, input().split()))\nk2 = list(map(int, input().split()))\nprint(solve(n, k1, k2))\n", "n = int(input())\nxs1 = [int(x) for x in input().split()][1:]\nxs2 = [int(x) for x in input().split()][1:]\nxs1.reverse()\nxs2.reverse()\nstates = {}\n\ndef hashe(xs):\n\ts = ''\n\tfor x in xs:\n\t\ts += str(x)\n\treturn s\ngames = 0\nwhile True:\n\tstate = hashe(xs1) + hashe(xs2)\n\tif state in states:\n\t\tprint(-1)\n\t\tbreak\n\tstates[state] = True\n\tx1 = xs1.pop()\n\tx2 = xs2.pop()\n\tif x1 > x2:\n\t\txs1.insert(0, x2)\n\t\txs1.insert(0, x1)\n\telse:\n\t\txs2.insert(0, x1)\n\t\txs2.insert(0, x2)\n\tgames += 1\n\tif len(xs1) is 0:\n\t\tprint(games, 2)\n\t\tbreak\n\telif len(xs2) is 0:\n\t\tprint(games, 1)\n\t\tbreak\n", "n = int(input())\nl1 = list(map(int, input().split()))\nl2 = list(map(int, input().split()))\nm1 = l1[1:]\nm2 = l2[1:]\n(c, c1, f) = (0, 0, 0)\nwhile len(m1) != 0 and len(m2) != 0:\n\tif m1[0] < m2[0]:\n\t\tc = m1.pop(0)\n\t\tm2.append(c)\n\t\tc = m2.pop(0)\n\t\tm2.append(c)\n\telse:\n\t\tc = m2.pop(0)\n\t\tm1.append(c)\n\t\tc = m1.pop(0)\n\t\tm1.append(c)\n\tc1 += 1\n\tif c1 > n * n * n or (m1 == l1[1:] and m2 == l2[1:]):\n\t\tf = 1\n\t\tbreak\nif f == 1:\n\tprint(-1)\nelif len(m1) == 0:\n\tprint(c1, 2)\nelse:\n\tprint(c1, 1)\n", "from collections import deque\nn = int(input())\nq1 = deque(list(map(int, input().split()))[1:])\nq2 = deque(list(map(int, input().split()))[1:])\nans = 0\nvisited = {}\n(s1, s2) = ('', '')\nfor i in q1:\n\ts1 = s1 + str(i)\nfor i in q2:\n\ts2 = s2 + str(i)\ns = frozenset([s1, s2])\nvisited[s] = visited.get(s, 0) + 1\nwhile len(q1) and len(q2):\n\tx = q1.popleft()\n\ty = q2.popleft()\n\tif x > y:\n\t\tq1.append(y)\n\t\tq1.append(x)\n\telse:\n\t\tq2.append(x)\n\t\tq2.append(y)\n\t(s1, s2) = ('', '')\n\tfor i in q1:\n\t\ts1 = s1 + str(i)\n\tfor i in q2:\n\t\ts2 = s2 + str(i)\n\ts = frozenset([s1, s2])\n\tvisited[s] = visited.get(s, 0) + 1\n\tif visited[s] == 1:\n\t\tans = ans + 1\n\telse:\n\t\tans = -1\n\t\tbreak\nif ans == -1:\n\tprint(ans)\nelse:\n\tif len(q1) > len(q2):\n\t\tl = 1\n\telse:\n\t\tl = 2\n\tprint(ans, l)\n", "def maker(a, b):\n\t(m, n) = ('', '')\n\tfor i in a:\n\t\tm = m + str(i)\n\tfor i in b:\n\t\tn = n + str(i)\n\treturn (m, n)\nn = int(input())\na = list(map(int, input().split()))\na.pop(0)\nb = list(map(int, input().split()))\nb.pop(0)\nc = set()\nc.add(maker(a, b))\nf = True\nfights = 0\nwhile 1:\n\tfights += 1\n\t(x, y) = (a.pop(0), b.pop(0))\n\tif x < y:\n\t\tb.append(x)\n\t\tb.append(y)\n\telse:\n\t\ta.append(y)\n\t\ta.append(x)\n\tu = maker(a, b)\n\tif len(a) == 0 or len(b) == 0:\n\t\tprint(fights, end=' ')\n\t\tif len(a) != 0:\n\t\t\tprint('1')\n\t\telse:\n\t\t\tprint('2')\n\t\tbreak\n\telif u in c:\n\t\tprint('-1')\n\t\tbreak\n\telse:\n\t\tc.add(u)\n", "n = int(input())\na = list(map(int, input().split()))\nb = list(map(int, input().split()))\ndel a[0]\ndel b[0]\nc = 0\nwhile True:\n\tif a[0] > b[0]:\n\t\ta.append(b[0])\n\t\ta.append(a[0])\n\t\tdel a[0]\n\t\tdel b[0]\n\telse:\n\t\tb.append(a[0])\n\t\tb.append(b[0])\n\t\tdel a[0]\n\t\tdel b[0]\n\tc = c + 1\n\tif a == []:\n\t\tprint(c)\n\t\tprint('2')\n\t\tbreak\n\telif b == []:\n\t\tprint(c)\n\t\tprint('1')\n\t\tbreak\n\telif c > min(2 ** n, 150):\n\t\tprint('-1')\n\t\tbreak\n", "n = int(input())\nl = list(map(int, input().split()))\nm = list(map(int, input().split()))\nc = 0\nl = l[1:]\nm = m[1:]\n(q1, q2) = ([], [])\nq1.extend(l)\nq2.extend(m)\nwhile len(l) > 0 and len(m) > 0:\n\t(a, b) = (l.pop(0), m.pop(0))\n\tif a > b:\n\t\tl.append(b)\n\t\tl.append(a)\n\telse:\n\t\tm.append(a)\n\t\tm.append(b)\n\tc += 1\n\tif c > 500:\n\t\tc = -1\n\t\tbreak\nif c == -1:\n\tprint(-1)\nelse:\n\tx = 2\n\tif len(l) > 0:\n\t\tx = 1\n\tprint(c, x)\n", "N = input()\ns1 = input().split(' ')[1:]\ns2 = input().split(' ')[1:]\ns1 = tuple(map(int, s1))\ns2 = tuple(map(int, s2))\nvisited = set()\ncount = 0\nwhile len(s1) > 0 and len(s2) > 0:\n\tcount += 1\n\tif (s1, s2) in visited:\n\t\tbreak\n\telse:\n\t\tvisited.add((s1, s2))\n\tif s1[0] < s2[0]:\n\t\ts2 = s2[1:] + (s1[0], s2[0])\n\t\ts1 = s1[1:]\n\telse:\n\t\ts1 = s1[1:] + (s2[0], s1[0])\n\t\ts2 = s2[1:]\nif len(s1) == 0:\n\tprint(str(count) + ' 2')\nelif len(s2) == 0:\n\tprint(str(count) + ' 1')\nelse:\n\tprint(-1)\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\nimport math\nfrom itertools import permutations\nfrom decimal import Decimal, getcontext\ngetcontext().prec = 25\nMOD = pow(10, 9) + 7\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nn = int(input())\nl1 = list(map(int, input().split(' ')))\nl2 = list(map(int, input().split(' ')))\na = l1[0]\nl1.pop(0)\nb = l2[0]\nl2.pop(0)\nx = [0] * 10000\ny = [0] * 10000\nfor i in range(a):\n\tx[i] = l1[i]\nfor i in range(b):\n\ty[i] = l2[i]\nans = -1\nans2 = 0\nfor i in range(997):\n\tif not x[i] or not y[i]:\n\t\tif x[i]:\n\t\t\tans = 1\n\t\telse:\n\t\t\tans = 2\n\t\tbreak\n\telif x[i] > y[i]:\n\t\tx[a] = y[i]\n\t\tx[a + 1] = x[i]\n\t\ta += 2\n\telse:\n\t\ty[b] = x[i]\n\t\ty[b + 1] = y[i]\n\t\tb += 2\n\tans2 += 1\nif ans != -1:\n\tprint(ans2, ans)\nelse:\n\tprint(-1)\n", "n = int(input())\nl1 = list(map(int, input().split()))\nl2 = list(map(int, input().split()))\nl1.pop(0)\nl2.pop(0)\ncount = 0\nwhile l1 and l2:\n\tif count >= 100000:\n\t\tprint('-1')\n\t\texit()\n\tcount += 1\n\tt1 = l1.pop(0)\n\tt2 = l2.pop(0)\n\tif t1 > t2:\n\t\tl1 += [t2, t1]\n\telse:\n\t\tl2 += [t1, t2]\nif l1:\n\tprint(count, 1)\nelse:\n\tprint(count, 2)\n", "from math import factorial\nfrom sys import stdin\ninput = stdin.readline\ninput()\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\ns = set()\ne = False\ncnt = 0\nwhile len(a) * len(b):\n\t(c, d) = (a.pop(0), b.pop(0))\n\tif c > d:\n\t\ta += [d, c]\n\telse:\n\t\tb += [c, d]\n\tcnt += 1\n\tt = (tuple(a), tuple(b))\n\tif t in s:\n\t\te = True\n\t\tbreak\n\ts.add(t)\nif e:\n\tprint(-1)\nelse:\n\tprint(cnt, [1, 2][len(a) == 0])\n", "max_count = 1000\ninput()\na = list(map(int, input().split()[1:]))\nb = list(map(int, input().split()[1:]))\ni = 0\nwhile len(a) - i > 0 and len(b) - i > 0 and (i < max_count):\n\tif a[i] > b[i]:\n\t\ta.extend([b[i], a[i]])\n\telse:\n\t\tb.extend([a[i], b[i]])\n\ti += 1\nprint(-1 if i == max_count else ' '.join(map(str, (i, 1))) if len(b) - i == 0 else ' '.join(map(str, (i, 2))))\n", "n = int(input())\ns1 = list(map(int, input().split()))\ns2 = list(map(int, input().split()))\ns1 = s1[1:]\ns2 = s2[1:]\ncount = 0\nwhile len(s1) != 0 and len(s2) != 0:\n\tif s1[0] > s2[0]:\n\t\ts1.append(s2[0])\n\t\ts1.append(s1[0])\n\t\ts1 = s1[1:]\n\t\ts2 = s2[1:]\n\telif count > 500:\n\t\tbreak\n\telse:\n\t\ts2.append(s1[0])\n\t\ts2.append(s2[0])\n\t\ts2 = s2[1:]\n\t\ts1 = s1[1:]\n\tcount = count + 1\nif len(s1) == 0:\n\tprint(count, end=' ')\n\tprint('2')\nelif len(s2) == 0:\n\tprint(count, end=' ')\n\tprint('1')\nelif count > 500:\n\tprint('-1')\n", "I = lambda : list(map(int, input().split()))[1:]\nI()\na = I()\nb = I()\nt = 0\nwhile t < 10 ** 5 and a and b:\n\tt += 1\n\tif a[0] > b[0]:\n\t\ta += [b[0], a[0]]\n\telse:\n\t\tb += [a[0], b[0]]\n\ta = a[1:]\n\tb = b[1:]\nprint([str(t) + ' ' + str(1 + bool(b)), -1][t == 10 ** 5])\n", "import sys\nn = int(input())\n(a, b) = (list(map(int, input().split())), list(map(int, input().split())))\nstep = 0\ni = 0\na.pop(i)\nb.pop(i)\nhist = list()\nwhile a != [] and b != []:\n\tif [a, b] in hist:\n\t\tprint(-1)\n\t\tsys.exit(0)\n\thist.append([[x for x in a], [x for x in b]])\n\tif a[i] > b[i]:\n\t\ta.append(b[i])\n\t\ta.append(a[i])\n\telse:\n\t\tb.append(a[i])\n\t\tb.append(b[i])\n\ta.pop(i)\n\tb.pop(i)\n\tstep += 1\nprint(step, 2 if a == [] else 1)\n", "z = lambda : list(map(int, input().split()))\nn = int(input())\nk1 = z()[1:]\nk2 = z()[1:]\nc = 0\nwhile k1 and k2:\n\t(x, y) = (k1.pop(0), k2.pop(0))\n\tif x > y:\n\t\tk1 += [y, x]\n\telse:\n\t\tk2 += [x, y]\n\tif c > 1000:\n\t\tprint(-1)\n\t\tbreak\n\tc += 1\nelse:\n\tprint(c, 1 if k1 else 2)\n", "n = int(input())\nk1 = list(map(int, input().split()))\nk1.pop(0)\nk2 = list(map(int, input().split()))\nk2.pop(0)\ntrick = 0\nwhile k1 and k2:\n\ttrick += 1\n\tif trick > 110:\n\t\tprint(-1)\n\t\texit()\n\ta = k1.pop(0)\n\tb = k2.pop(0)\n\tif a > b:\n\t\tk1.append(b)\n\t\tk1.append(a)\n\telse:\n\t\tk2.append(a)\n\t\tk2.append(b)\nprint(str(trick) + ' ' + str(1 if k1 else 2))\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\n\ndef main():\n\tn = input()\n\ta = list(map(int, input().split(' ')))[1:]\n\tb = list(map(int, input().split(' ')))[1:]\n\ttemp = max(a + b)\n\tif a.count(temp) > 0 and b.count(temp) > 0:\n\t\tprint(-1)\n\telse:\n\t\tchk = 0\n\t\tcnt = 0\n\t\twhile len(a) > 0 and len(b) > 0:\n\t\t\tif a[0] > b[0]:\n\t\t\t\ta = a[1:] + b[0:1] + a[0:1]\n\t\t\t\tb = b[1:]\n\t\t\telif b[0] > a[0]:\n\t\t\t\tb = b[1:] + a[0:1] + b[0:1]\n\t\t\t\ta = a[1:]\n\t\t\telse:\n\t\t\t\ta = a[1:] + a[0:1]\n\t\t\t\tb = b[1:] + b[0:1]\n\t\t\tcnt += 1\n\t\t\tif cnt > 1000:\n\t\t\t\tchk = 1\n\t\t\t\tbreak\n\t\tif chk == 1:\n\t\t\tprint(-1)\n\t\telif len(b) == 0:\n\t\t\tprint(cnt, 1)\n\t\telse:\n\t\t\tprint(cnt, 2)\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nmain()\n", "def main():\n\tn = int(input())\n\ttemp = input().split()\n\tp1n = int(temp[0])\n\tp1cards = [int(i) for i in temp[1:]]\n\ttemp = input().split()\n\tp2n = int(temp[0])\n\tp2cards = [int(i) for i in temp[1:]]\n\tstale = False\n\thistory1 = []\n\thistory2 = []\n\tcounter = 0\n\twhile len(p1cards) != 0 and len(p2cards) != 0 and (stale == False):\n\t\tcounter += 1\n\t\thistory1.append(p1cards)\n\t\thistory2.append(p2cards)\n\t\tif p1cards[0] > p2cards[0]:\n\t\t\t(p1cards, p2cards) = switchero(p1cards, p2cards)\n\t\telif p2cards[0] > p1cards[0]:\n\t\t\t(p2cards, p1cards) = switchero(p2cards, p1cards)\n\t\tfor cards1 in history1:\n\t\t\tif p1cards == cards1:\n\t\t\t\tfor cards2 in history2:\n\t\t\t\t\tif p2cards == cards2:\n\t\t\t\t\t\tstale = True\n\t\t\t\t\tcontinue\n\tif stale == True:\n\t\tcounter = -1\n\tif len(p1cards) == 0:\n\t\twinner = 2\n\t\tprint(counter, winner)\n\telif len(p2cards) == 0:\n\t\twinner = 1\n\t\tprint(counter, winner)\n\telse:\n\t\tprint(counter)\n\ndef switchero(winner, loser):\n\tif len(loser) == 1:\n\t\tlosernew = []\n\telse:\n\t\tlosernew = loser[1:]\n\twinnernew = winner[1:]\n\twinnernew.append(loser[0])\n\twinnernew.append(winner[0])\n\treturn (winnernew, losernew)\nmain()\n", "n = int(input())\na = list(map(int, input().split()))\nb = list(map(int, input().split()))\nans = 0\n(a.pop(0), b.pop(0))\nwhile a and b:\n\tif ans > 110:\n\t\tprint(-1)\n\t\texit(0)\n\tans += 1\n\tx = a.pop(0)\n\ty = b.pop(0)\n\tif x > y:\n\t\ta.append(y)\n\t\ta.append(x)\n\telse:\n\t\tb.append(x)\n\t\tb.append(y)\nif a:\n\tprint(ans, 1)\nelse:\n\tprint(ans, 2)\n", "import sys\ninput = sys.stdin.readline\nsys.setrecursionlimit(10 ** 6)\n\ndef in_int():\n\treturn int(input())\n\ndef in_list():\n\treturn list(map(int, input().split()))\n\ndef in_str():\n\ts = input()\n\treturn list(s[:len(s) - 1])\n\ndef in_ints():\n\treturn map(int, input().split())\nn = in_int()\na = in_list()\nb = in_list()\nxx = a.pop(0)\nxx = b.pop(0)\nss = set()\ncount = 0\nwhile str(a) + str(b) not in ss and len(a) > 0 and (len(b) > 0):\n\tss.add(str(a) + str(b))\n\taa = a.pop(0)\n\tbb = b.pop(0)\n\tif aa > bb:\n\t\ta = a + [bb, aa]\n\telse:\n\t\tb = b + [aa, bb]\n\tcount += 1\nif len(a) == 0:\n\tprint(count, 2)\nelif len(b) == 0:\n\tprint(count, 1)\nelse:\n\tprint(-1)\n", "from sys import stdout\nfrom sys import stdin\n\ndef get():\n\treturn stdin.readline().strip()\n\ndef getf(sp=' '):\n\treturn [int(i) for i in get().split(sp)]\n\ndef put(a, end='\\n'):\n\tstdout.write(str(a) + end)\n\ndef putf(a, sep=' ', end='\\n'):\n\tstdout.write(sep.join([str(i) for i in a]) + end)\nfrom collections import deque\n\ndef main():\n\tn = int(get())\n\t(k1, *a) = getf()\n\t(k2, *b) = getf()\n\ta = deque(a[::-1])\n\tb = deque(b[::-1])\n\tst = set()\n\tst.add((tuple(a), tuple(b)))\n\tf = -1\n\tres = 0\n\twhile True:\n\t\tx = a.pop()\n\t\ty = b.pop()\n\t\tres += 1\n\t\tif x > y:\n\t\t\ta.appendleft(y)\n\t\t\ta.appendleft(x)\n\t\t\tk1 += 1\n\t\t\tk2 -= 1\n\t\telse:\n\t\t\tb.appendleft(x)\n\t\t\tb.appendleft(y)\n\t\t\tk1 -= 1\n\t\t\tk2 += 1\n\t\tif k1 == 0:\n\t\t\tf = 1\n\t\t\tbreak\n\t\tif k2 == 0:\n\t\t\tf = 0\n\t\t\tbreak\n\t\ti = (tuple(a), tuple(b))\n\t\tif i in st:\n\t\t\tbreak\n\t\tst.add(i)\n\tif f == -1:\n\t\tput('-1')\n\telse:\n\t\tput('%d %d' % (res, f + 1))\nmain()\n", "from __future__ import division, print_function\nfrom fractions import Fraction\nimport sys\nimport os\nfrom io import BytesIO, IOBase\nfrom functools import cmp_to_key\nfrom heapq import *\nfrom math import gcd, factorial, floor, ceil\nfrom copy import deepcopy\nfrom collections import deque\nfrom bisect import bisect_left as bl\nfrom bisect import bisect_right as br\nfrom bisect import bisect\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n\ndef print(*args, **kwargs):\n\t(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))\n\tat_start = True\n\tfor x in args:\n\t\tif not at_start:\n\t\t\tfile.write(sep)\n\t\tfile.write(str(x))\n\t\tat_start = False\n\tfile.write(kwargs.pop('end', '\\n'))\n\tif kwargs.pop('flush', False):\n\t\tfile.flush()\nif sys.version_info[0] < 3:\n\t(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))\nelse:\n\t(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\nfrom types import GeneratorType\n\ndef iterative(f, stack=[]):\n\n\tdef wrapped_func(*args, **kwargs):\n\t\tif stack:\n\t\t\treturn f(*args, **kwargs)\n\t\tto = f(*args, **kwargs)\n\t\twhile True:\n\t\t\tif type(to) is GeneratorType:\n\t\t\t\tstack.append(to)\n\t\t\t\tto = next(to)\n\t\t\t\tcontinue\n\t\t\tstack.pop()\n\t\t\tif not stack:\n\t\t\t\tbreak\n\t\t\tto = stack[-1].send(to)\n\t\treturn to\n\treturn wrapped_func\n\ndef inp():\n\treturn sys.stdin.readline().rstrip('\\r\\n')\n\ndef out(var):\n\tsys.stdout.write(str(var))\n\ndef lis():\n\treturn list(map(int, inp().split()))\n\ndef stringlis():\n\treturn list(map(str, inp().split()))\n\ndef sep():\n\treturn map(int, inp().split())\n\ndef strsep():\n\treturn map(str, inp().split())\n\ndef zerolist(n):\n\treturn [0] * n\n\ndef nextline():\n\tout('\\n')\n\ndef testcase(t):\n\tfor pp in range(t):\n\t\tsolve(pp)\n\ndef printlist(a):\n\tfor p in range(0, len(a)):\n\t\tout(str(a[p]) + ' ')\n\ndef google(p):\n\tprint('Case #' + str(p) + ': ', end='')\n\ndef lcm(a, b):\n\treturn a * b // gcd(a, b)\n\ndef power(x, y, p):\n\tres = 1\n\tx = x % p\n\tif x == 0:\n\t\treturn 0\n\twhile y > 0:\n\t\tif y & 1 == 1:\n\t\t\tres = res * x % p\n\t\ty = y >> 1\n\t\tx = x * x % p\n\treturn res\n\ndef ncr(n, r):\n\treturn factorial(n) // (factorial(r) * factorial(max(n - r, 1)))\n\ndef isPrime(n):\n\tif n <= 1:\n\t\treturn False\n\tif n <= 3:\n\t\treturn True\n\tif n % 2 == 0 or n % 3 == 0:\n\t\treturn False\n\ti = 5\n\twhile i * i <= n:\n\t\tif n % i == 0 or n % (i + 2) == 0:\n\t\t\treturn False\n\t\ti = i + 6\n\treturn True\ninf = pow(10, 20)\nmod = 10 ** 9 + 7\n\ndef solve(case):\n\tn = int(inp())\n\ta = deque(lis())\n\tb = deque(lis())\n\ta.popleft()\n\tb.popleft()\n\tcnt = 0\n\twhile len(a) != 0 and len(b) != 0:\n\t\tx = a.popleft()\n\t\ty = b.popleft()\n\t\tif x < y:\n\t\t\tb.append(x)\n\t\t\tb.append(y)\n\t\telse:\n\t\t\ta.append(y)\n\t\t\ta.append(x)\n\t\tcnt += 1\n\t\tif cnt > 200:\n\t\t\tprint(-1)\n\t\t\treturn\n\tif len(a) == 0:\n\t\tprint(cnt, 2)\n\telse:\n\t\tprint(cnt, 1)\ntestcase(1)\n", "from collections import deque\n\ndef wars():\n\tn = int(input())\n\tcards = input().split()\n\ttot = int(cards[0])\n\tqueue1 = deque((int(i) for i in cards[1:]))\n\ttest = queue1\n\tqueue2 = deque((int(i) for i in input().split()[1:]))\n\tc = 0\n\twhile queue1 and queue2:\n\t\tif c > 5000:\n\t\t\treturn -1\n\t\tc1 = queue1.popleft()\n\t\tc2 = queue2.popleft()\n\t\tc += 1\n\t\tif c1 > c2:\n\t\t\tqueue1.append(c2)\n\t\t\tqueue1.append(c1)\n\t\telse:\n\t\t\tqueue2.append(c1)\n\t\t\tqueue2.append(c2)\n\tw = '1' if queue1 else '2'\n\treturn str(c) + ' ' + w\nprint(wars())\n", "def lista_string_int(l):\n\tfor i in range(len(l)):\n\t\tl[i] = int(l[i])\nn = int(input())\nl1 = list(input().split(' '))\nl2 = list(input().split(' '))\nn1 = l1.pop(0)\nn2 = l2.pop(0)\nlista_string_int(l1)\nlista_string_int(l2)\nganador = False\nfor i in range(5000):\n\tif len(l1) == 0:\n\t\tganador = True\n\t\tprint(i, 2)\n\t\tbreak\n\telif len(l2) == 0:\n\t\tganador = True\n\t\tprint(i, 1)\n\t\tbreak\n\tif l1[0] > l2[0]:\n\t\tl1.append(l2.pop(0))\n\t\tl1.append(l1.pop(0))\n\telif l2[0] > l1[0]:\n\t\tl2.append(l1.pop(0))\n\t\tl2.append(l2.pop(0))\nif ganador == False:\n\tprint(-1)\n", "n = int(input())\nlist1 = list(map(int, input().split()))\nlist2 = list(map(int, input().split()))\nk1 = list1[0]\ndel list1[0]\nk2 = list2[0]\ndel list2[0]\ni = 0\ncount = 0\nwhile len(list1) != 0 and len(list2) != 0:\n\tif list1[i] < list2[i]:\n\t\tlist2.append(list1[i])\n\t\tlist2.append(list2[i])\n\t\tdel list1[i]\n\t\tdel list2[i]\n\t\tcount += 1\n\telif list2[i] < list1[i]:\n\t\tlist1.append(list2[i])\n\t\tlist1.append(list1[i])\n\t\tdel list1[i]\n\t\tdel list2[i]\n\t\tcount += 1\n\telse:\n\t\tdel list1[i]\n\t\tdel list2[i]\n\tif count == n ** 2 + 10:\n\t\tprint('-1')\n\t\tbreak\nif len(list1) == 0:\n\tprint('{} 2'.format(count))\nif len(list2) == 0:\n\tprint('{} 1'.format(count))\n", "n = int(input())\nl1 = list(map(int, input().split()))\nl2 = list(map(int, input().split()))\nres = 0\nl1 = l1[1:]\nl2 = l2[1:]\nwhile len(l1) > 0 and len(l2) > 0:\n\tres += 1\n\tif res > 1000:\n\t\tbreak\n\tx = l1.pop(0)\n\ty = l2.pop(0)\n\tif x > y:\n\t\tl1.append(y)\n\t\tl1.append(x)\n\telse:\n\t\tl2.append(x)\n\t\tl2.append(y)\nif len(l2) == 0:\n\tprint(res, end=' ')\n\tprint(1)\nelif len(l1) == 0:\n\tprint(res, end=' ')\n\tprint(2)\nelse:\n\tprint(-1)\n", "n = int(input())\na = [int(x) for x in input().split()]\nb = [int(x) for x in input().split()]\nk1 = a.pop(0)\nk2 = b.pop(0)\nflag = False\ncnt = 0\nn = 106\nwhile k1 != 0 and k2 != 0 and (n > 0):\n\tn -= 1\n\tcnt += 1\n\tx = a.pop(0)\n\ty = b.pop(0)\n\tif x > y:\n\t\ta.append(y)\n\t\ta.append(x)\n\t\tk1 += 1\n\t\tk2 -= 1\n\telse:\n\t\tb.append(x)\n\t\tb.append(y)\n\t\tk2 += 1\n\t\tk1 -= 1\nif k1 != 0 and k2 != 0:\n\tprint('-1')\nelse:\n\tprint(cnt, '1' if k1 != 0 else '2')\n", "import sys\n\ndef solve(n, f, s):\n\tmoves = 0\n\tmax_till = 200\n\twhile f and s:\n\t\tif f[0] > s[0]:\n\t\t\tf.append(s[0])\n\t\t\tf.append(f[0])\n\t\t\ts.pop(0)\n\t\t\tf.pop(0)\n\t\t\tmoves += 1\n\t\telse:\n\t\t\ts.append(f[0])\n\t\t\ts.append(s[0])\n\t\t\ts.pop(0)\n\t\t\tf.pop(0)\n\t\t\tmoves += 1\n\t\tif moves > max_till:\n\t\t\treturn -1\n\tif not f:\n\t\treturn (moves, 2)\n\telse:\n\t\treturn (moves, 1)\ntake = sys.stdin.readline\nn = int(take())\nf = list(map(int, take().split()))[1:]\ns = list(map(int, take().split()))[1:]\nresult = solve(n, f, s)\nif result == -1:\n\tprint(-1)\n\texit(0)\nprint(*result)\n", "def is_loop(l1, l2, cards_sets=[]):\n\tcard_set = set([tuple(l1), tuple(l2)])\n\tif card_set in cards_sets:\n\t\treturn True\n\telse:\n\t\tcards_sets.append(card_set)\n\t\treturn False\ncards_amount = int(input())\nfirst_player = input().split()[1:]\nfirst_player = [int(i) for i in first_player]\nsecond_player = input().split()[1:]\nsecond_player = [int(i) for i in second_player]\nresult = 0\nwhile True:\n\tif is_loop(first_player, second_player):\n\t\tprint(-1)\n\t\tbreak\n\tresult += 1\n\tfp_card = first_player.pop(0)\n\tsp_card = second_player.pop(0)\n\tif fp_card > sp_card:\n\t\tfirst_player.extend([sp_card, fp_card])\n\telse:\n\t\tsecond_player.extend([fp_card, sp_card])\n\tif not second_player:\n\t\tprint(result, 1)\n\t\tbreak\n\tif not first_player:\n\t\tprint(result, 2)\n\t\tbreak\n", "n = int(input())\nls1 = list(map(int, input().split()))\nls2 = list(map(int, input().split()))\ndel ls1[0]\ndel ls2[0]\ncp1 = []\ncp2 = []\nfor i in ls1:\n\tcp1.append(i)\nfor i in ls2:\n\tcp2.append(i)\nfights = 0\nflag = 0\nwhile len(ls1) != 0 and len(ls2) != 0:\n\tif fights > 1000:\n\t\tflag += 1\n\t\tbreak\n\tif ls1[0] > ls2[0]:\n\t\tls1.append(ls2[0])\n\t\tls1.append(ls1[0])\n\t\tdel ls1[0]\n\t\tdel ls2[0]\n\telse:\n\t\tls2.append(ls1[0])\n\t\tls2.append(ls2[0])\n\t\tdel ls1[0]\n\t\tdel ls2[0]\n\tif ls1 == cp1 and ls2 == cp2:\n\t\tflag += 1\n\t\tbreak\n\tfights += 1\nif flag != 0:\n\tprint(-1)\nelif len(ls1) != 0:\n\tprint(fights, 1)\nelse:\n\tprint(fights, 2)\n", "def fight(k1, k2):\n\tl = k1.pop(0)\n\tr = k2.pop(0)\n\tif l < r:\n\t\tk2.extend([l, r])\n\telse:\n\t\tk1.extend([r, l])\nn = int(input())\nk1 = list(map(int, input().split()))\nk1.pop(0)\nk2 = list(map(int, input().split()))\nk2.pop(0)\ncombs = set()\nfights = 0\nwhile k1 and k2:\n\ttopcomb = (''.join(map(str, k1)), ''.join(map(str, k2)))\n\tif topcomb in combs:\n\t\tfights = -1\n\t\tbreak\n\tcombs.add(topcomb)\n\tfight(k1, k2)\n\tfights += 1\nprint(fights, end=' ')\nif fights > -1:\n\tprint(1 if k1 else 2)\n", "import sys\nfrom collections import deque\ninput = sys.stdin.readline\nn = int(input())\nk1 = list(map(int, input().split()))\nk2 = list(map(int, input().split()))\ne1 = deque(k1)\ne2 = deque(k2)\ne1.popleft()\ne2.popleft()\nans = 0\nwhile len(e1) > 0 and len(e2) > 0 and (ans < 10 ** 7):\n\tx1 = e1.popleft()\n\tx2 = e2.popleft()\n\tif x1 > x2:\n\t\te1.append(x2)\n\t\te1.append(x1)\n\telse:\n\t\te2.append(x1)\n\t\te2.append(x2)\n\tans += 1\nif ans == 10 ** 7:\n\tprint(-1)\n\texit()\nif len(e2) == 0:\n\tprint(ans, 1)\nelse:\n\tprint(ans, 2)\n", "n = int(input())\nq1 = list(map(int, input().split()))[1:]\nq2 = list(map(int, input().split()))[1:]\ns = set()\nc = 0\nwhile q1 and q2:\n\tc += 1\n\ta = q1.pop(0)\n\tb = q2.pop(0)\n\tif a < b:\n\t\tq2.append(a)\n\t\tq2.append(b)\n\telse:\n\t\tq1.append(b)\n\t\tq1.append(a)\n\tt = (tuple(q1), tuple(q2))\n\tif t in s:\n\t\tbreak\n\ts.add(t)\nif q1 and q2:\n\tprint(-1)\nelse:\n\tprint(c, 1 if q1 else 2)\n", "n = int(input())\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\nfor ans in range(1, 100000):\n\tif a[0] < b[0]:\n\t\tb.append(a.pop(0))\n\t\tb.append(b.pop(0))\n\telse:\n\t\ta.append(b.pop(0))\n\t\ta.append(a.pop(0))\n\tif not a or not b:\n\t\tprint(ans, 1 if a else 2)\n\t\tbreak\nelse:\n\tprint(-1)\n", "from collections import deque\nn = int(input())\np1 = deque(map(int, input().split()))\np2 = deque(map(int, input().split()))\nk1 = p1.popleft()\nk2 = p2.popleft()\nfor i in range(int(40000000.0)):\n\ta = p1.popleft()\n\tb = p2.popleft()\n\tif a > b:\n\t\tp1.append(b)\n\t\tp1.append(a)\n\telse:\n\t\tp2.append(a)\n\t\tp2.append(b)\n\tif not p1 or not p2:\n\t\tprint(i + 1, end=' ')\n\t\tif n in p1:\n\t\t\tprint(1)\n\t\telse:\n\t\t\tprint(2)\n\t\texit(0)\nprint(-1)\n", "n = int(input())\np1 = list(map(int, input().split()))\np2 = list(map(int, input().split()))\np1_score = 0\np2_score = 0\np1.pop(0)\np2.pop(0)\nprev_stacks = []\nfound = False\nwhile p1 and p2:\n\tprev_stacks.append(str(p1) + '/' + str(p2))\n\tx = p1.pop(0)\n\ty = p2.pop(0)\n\tif x > y:\n\t\tp1_score += 1\n\t\tp1.append(y)\n\t\tp1.append(x)\n\telse:\n\t\tp2_score += 1\n\t\tp2.append(x)\n\t\tp2.append(y)\n\tif str(p1) + '/' + str(p2) in prev_stacks:\n\t\tfound = True\n\t\tbreak\nif found or p1_score == p2_score:\n\tprint(-1)\nelse:\n\twinner = 0\n\tif p1_score > p2_score:\n\t\twinner = 1\n\telse:\n\t\twinner = 2\n\tprint(p1_score + p2_score, winner)\n", "import sys\nfrom functools import lru_cache, cmp_to_key\nfrom heapq import merge, heapify, heappop, heappush, nlargest, nsmallest\nfrom math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log\nfrom collections import defaultdict as dd, deque, Counter as C\nfrom itertools import combinations as comb, permutations as perm\nfrom bisect import bisect_left as bl, bisect_right as br, bisect\nfrom time import perf_counter\nfrom fractions import Fraction\nmod = pow(10, 9) + 7\nmod2 = 998244353\n\ndef data():\n\treturn sys.stdin.readline().strip()\n\ndef out(*var, end='\\n'):\n\tsys.stdout.write(' '.join(map(str, var)) + end)\n\ndef l():\n\treturn list(sp())\n\ndef sl():\n\treturn list(ssp())\n\ndef sp():\n\treturn map(int, data().split())\n\ndef ssp():\n\treturn map(str, data().split())\n\ndef l1d(n, val=0):\n\treturn [val for i in range(n)]\n\ndef l2d(n, m, val=0):\n\treturn [l1d(n, val) for j in range(m)]\nn = int(data())\n(s1, s2) = (deque(l()[1:]), deque(l()[1:]))\nstate = set()\nstate.add((tuple(s1), tuple(s2)))\ncnt = 0\nwhile True:\n\t(t1, t2) = (s1.popleft(), s2.popleft())\n\tif t1 > t2:\n\t\ts1.append(t2)\n\t\ts1.append(t1)\n\telse:\n\t\ts2.append(t1)\n\t\ts2.append(t2)\n\tif not s1:\n\t\tout(cnt + 1, 2)\n\t\texit()\n\tif not s2:\n\t\tout(cnt + 1, 1)\n\t\texit()\n\tcnt += 1\n\tif (tuple(s1), tuple(s2)) in state or (tuple(s2), tuple(s1)) in state:\n\t\tout(-1)\n\t\texit()\n\tstate.add((tuple(s1), tuple(s2)))\n", "from collections import deque\n\ndef main():\n\tn = int(input())\n\ta = deque([int(c) for c in input().split()][::-1])\n\tb = deque([int(c) for c in input().split()][::-1])\n\ta.pop()\n\tb.pop()\n\tconf = set()\n\ti = 1\n\twhile True:\n\t\tcur = (tuple(a), tuple(b))\n\t\tif cur in conf:\n\t\t\tprint(-1)\n\t\t\treturn\n\t\telse:\n\t\t\tconf.add(cur)\n\t\t(x, y) = (a.pop(), b.pop())\n\t\tif x > y:\n\t\t\ta.appendleft(y)\n\t\t\ta.appendleft(x)\n\t\telse:\n\t\t\tb.appendleft(x)\n\t\t\tb.appendleft(y)\n\t\tif not a:\n\t\t\tprint(i, 2)\n\t\t\treturn\n\t\tif not b:\n\t\t\tprint(i, 1)\n\t\t\treturn\n\t\ti += 1\nmain()\n", "from collections import defaultdict as dd, deque as dq\nfrom math import factorial\np = int(input())\n(a1, *a) = list(map(int, input().split()))\n(b1, *b) = list(map(int, input().split()))\nt = factorial(11)\nm = dq(a)\nn = dq(b)\nd = {}\nfights = 0\nwhile m and n:\n\tif fights > t:\n\t\tfights = -1\n\t\tbreak\n\tk = m.popleft()\n\tl = n.popleft()\n\tif k > l:\n\t\tm.append(l)\n\t\tm.append(k)\n\telse:\n\t\tn.append(k)\n\t\tn.append(l)\n\tfights += 1\nif fights == -1:\n\tprint(fights)\nelif list(m) == []:\n\tprint(fights, 2)\nelse:\n\tprint(fights, 1)\n", "n = int(input())\nl1 = [int(i) for i in input().split()]\nk1 = l1.pop(0)\nl1.reverse()\nl2 = [int(i) for i in input().split()]\nk2 = l2.pop(0)\nl2.reverse()\ni = 0\ndup = set()\nwhile len(l1) > 0 and len(l2) > 0:\n\tc = tuple(l1 + [0.5] + l2)\n\tif c in dup:\n\t\tbreak\n\tdup.add(c)\n\ta = l1.pop()\n\tb = l2.pop()\n\tif a > b:\n\t\tl1.insert(0, b)\n\t\tl1.insert(0, a)\n\telse:\n\t\tl2.insert(0, a)\n\t\tl2.insert(0, b)\n\ti += 1\nif len(l1) == 0:\n\tprint(i, 2)\nelif len(l2) == 0:\n\tprint(i, 1)\nelse:\n\tprint(-1)\n", "from copy import deepcopy\nn = int(input())\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\ndata = []\ncount = 0\nwhile a and b:\n\tif (a, b) in data:\n\t\tprint(-1)\n\t\tbreak\n\tdata.append((deepcopy(a), deepcopy(b)))\n\tif a > b:\n\t\ta += [b.pop(0), a.pop(0)]\n\telif a < b:\n\t\tb += [a.pop(0), b.pop(0)]\n\tcount += 1\nif not a or not b:\n\tprint(count, 1 if a else 2)\n", "n = int(input())\nc_1 = [int(x) for x in input().split()]\nc_1.pop(0)\nc_2 = [int(x) for x in input().split()]\nc_2.pop(0)\nreached_before = dict()\nreached_before[str(c_1) + ' ' + str(c_2)] = True\ncntr = 0\nover = False\nwhile not over:\n\tcard_1 = c_1.pop(0)\n\tcard_2 = c_2.pop(0)\n\tif card_1 > card_2:\n\t\tc_1.append(card_2)\n\t\tc_1.append(card_1)\n\telse:\n\t\tc_2.append(card_1)\n\t\tc_2.append(card_2)\n\tcntr += 1\n\tif not c_1:\n\t\tover = True\n\t\tprint(cntr, 2)\n\telif not c_2:\n\t\tover = True\n\t\tprint(cntr, 1)\n\telif reached_before.get(str(c_1) + ' ' + str(c_2)):\n\t\tover = True\n\t\tprint(-1)\n\telse:\n\t\treached_before[str(c_1) + ' ' + str(c_2)] = True\n", "def main():\n\tn = int(input())\n\ta = list(map(int, input().split()))[1:]\n\tb = list(map(int, input().split()))[1:]\n\tst = set()\n\tst.add((tuple(a), tuple(b)))\n\tt = 0\n\twhile len(a) and len(b):\n\t\tt += 1\n\t\tif a[0] > b[0]:\n\t\t\ta.extend([b[0], a[0]])\n\t\telse:\n\t\t\tb.extend([a[0], b[0]])\n\t\ta.pop(0)\n\t\tb.pop(0)\n\t\tif (tuple(a), tuple(b)) in st:\n\t\t\tprint(-1)\n\t\t\treturn 0\n\t\tst.add((tuple(a), tuple(b)))\n\tprint(t, 1 + (len(a) == 0))\nmain()\n", "def winner(a: list, b: list) -> int:\n\tif a == []:\n\t\treturn 2\n\telif b == []:\n\t\treturn 1\n\telse:\n\t\treturn -1\n\ndef _compare(_f_list: list, _s_list: list, f: int, s: int) -> None:\n\tif max(f, s) == f:\n\t\t_f_list.append(min(f, s))\n\t\t_f_list.append(max(f, s))\n\telse:\n\t\t_s_list.append(min(f, s))\n\t\t_s_list.append(max(f, s))\n\ndef swap(_F_list: list, _S_list: list) -> None:\n\t(F_out, S_out) = (_F_list[0], _S_list[0])\n\tdel _F_list[0]\n\tdel _S_list[0]\n\t_compare(_F_list, _S_list, F_out, S_out)\nnumber = int(input())\n(F, S) = ([], [])\ntemp_list = []\nF = list(map(int, input().split()))\nS = list(map(int, input().split()))\ndel F[0]\ndel S[0]\n(dup_F, dup_S) = ([], [])\nfor i in range(len(F)):\n\tdup_F.append(F[i])\nfor i in range(len(S)):\n\tdup_S.append(S[i])\ncurrent_situ = []\ncurrent_situ.append(dup_F)\ncurrent_situ.append(dup_S)\ntemp_list.append(current_situ)\ntime = 0\nOMG = False\nwhile winner(F, S) == -1:\n\tif time >= 1:\n\t\t(dup_F, dup_S) = ([], [])\n\t\tfor i in range(len(F)):\n\t\t\tdup_F.append(F[i])\n\t\tfor i in range(len(S)):\n\t\t\tdup_S.append(S[i])\n\t\tcurrent_situ = []\n\t\tcurrent_situ.append(dup_F)\n\t\tcurrent_situ.append(dup_S)\n\t\ttemp_list.append(current_situ)\n\t\tfor i in temp_list[:-1]:\n\t\t\tif current_situ == i:\n\t\t\t\tOMG = True\n\tif OMG:\n\t\tbreak\n\tswap(F, S)\n\ttime += 1\nif winner(F, S) != -1:\n\tprint(time, end=' ')\n\tprint(winner(F, S))\nelse:\n\tprint(winner(F, S))\n", "from sys import stdin, stdout\nn = int(stdin.readline())\narray1 = list(map(int, stdin.readline().split()))[1:]\narray2 = list(map(int, stdin.readline().split()))[1:]\ni = 0\nwhile array1 and array2 and (i < 1000):\n\tif array1[0] > array2[0]:\n\t\tarray1.append(array2[0])\n\t\tarray1.append(array1[0])\n\t\ti += 1\n\telse:\n\t\tarray2.append(array1[0])\n\t\tarray2.append(array2[0])\n\t\ti += 1\n\tarray1.pop(0)\n\tarray2.pop(0)\nif len(array1) == 0:\n\tstdout.write(str(i) + ' ' + '2' + '\\n')\nelif len(array2) == 0:\n\tstdout.write(str(i) + ' ' + '1' + '\\n')\nelse:\n\tstdout.write('-1' + '\\n')\n", "from collections import deque\nn = int(input())\na = deque([int(i) for i in input().split()[1:]])\nb = deque([int(i) for i in input().split()[1:]])\nmark = set()\nans = 0\nwhile a and b:\n\tif str(a) + ' ' + str(b) in mark:\n\t\tprint(-1)\n\t\texit()\n\tmark.add(str(a) + ' ' + str(b))\n\tans += 1\n\taa = a.popleft()\n\tbb = b.popleft()\n\tif aa > bb:\n\t\ta.append(bb)\n\t\ta.append(aa)\n\telse:\n\t\tb.append(aa)\n\t\tb.append(bb)\nif a:\n\tprint(ans, 1)\nelse:\n\tprint(ans, 2)\n", "n = int(input())\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\nc = 0\ns = set()\ns.add(tuple(a) + tuple('#') + tuple(b))\nflag = 1\nwhile a and b and flag:\n\tc1 = a[0]\n\tc2 = b[0]\n\ta = a[1:]\n\tb = b[1:]\n\tif c1 < c2:\n\t\tb += [c1] + [c2]\n\telse:\n\t\ta += [c2] + [c1]\n\tk = tuple(a) + tuple('#') + tuple(b)\n\tif k in s:\n\t\tprint(-1)\n\t\tflag = 0\n\telse:\n\t\ts.add(k)\n\tc += 1\nif flag:\n\tif a:\n\t\tprint(c, 1)\n\telse:\n\t\tprint(c, 2)\n", "import os, io\nimport sys\n\ndef binomial_coefficient(n, k):\n\tif 0 <= k <= n:\n\t\tntok = 1\n\t\tktok = 1\n\t\tfor t in range(1, min(k, n - k) + 1):\n\t\t\tntok *= n\n\t\t\tktok *= t\n\t\t\tn -= 1\n\t\treturn ntok // ktok\n\telse:\n\t\treturn 0\n\ndef getState(d1, d2):\n\treturn ''.join(d1) + '-' + ''.join(d2)\nn = int(sys.stdin.readline().strip())\nd1 = sys.stdin.readline().strip().split(' ')[1:]\nd2 = sys.stdin.readline().strip().split(' ')[1:]\nstates = set([getState(d1, d2)])\npossible = True\ncnt = 0\nwhile len(d1) and len(d2):\n\tc1 = d1.pop(0)\n\tc2 = d2.pop(0)\n\tif int(c1) > int(c2):\n\t\td1.append(c2)\n\t\td1.append(c1)\n\telse:\n\t\td2.append(c1)\n\t\td2.append(c2)\n\tstate = getState(d1, d2)\n\tcnt += 1\n\tif state in states:\n\t\tprint(-1)\n\t\tpossible = False\n\t\tbreak\n\tstates.add(state)\nif possible:\n\tprint(cnt, end=' ')\n\tif len(d1):\n\t\tprint('1')\n\telse:\n\t\tprint('2')\n", "class Soldado:\n\n\tdef __init__(self, cartas, numero):\n\t\tcartas = cartas[1:]\n\t\tself.mano = cartas.split()\n\t\tself.nombre = numero\n\n\tdef perder(self):\n\t\tif len(self.mano) > 0:\n\t\t\tself.mano.remove(self.mano[0])\n\n\tdef ganar(self, otro_soldado):\n\t\tself.mano.append(otro_soldado.mano[0])\n\t\tself.mano.append(self.mano[0])\n\t\tself.mano.remove(self.mano[0])\n\n\tdef __gt__(self, otro_soldado):\n\t\treturn int(self.mano[0]) > int(otro_soldado.mano[0])\n\n\tdef __lt__(self, otro_soldado):\n\t\treturn int(self.mano[0]) < int(otro_soldado.mano[0])\n\n\tdef __eq__(self, otro_soldado):\n\t\treturn len(self.mano) == len(otro_soldado.mano)\n\n\tdef __str__(self):\n\t\tprint(self.nombre)\nmazo = int(input())\ncartas1 = input()\ncartas2 = input()\nsoldado1 = Soldado(cartas1, 1)\nsoldado2 = Soldado(cartas2, 2)\njuego = True\nt = 0\nwhile juego == True:\n\tif soldado1 > soldado2:\n\t\tsoldado1.ganar(soldado2)\n\t\tsoldado2.perder()\n\t\tt += 1\n\telif soldado1 < soldado2:\n\t\tsoldado2.ganar(soldado1)\n\t\tsoldado1.perder()\n\t\tt += 1\n\tif t > 500:\n\t\tprint('-1')\n\t\tjuego = False\n\telif len(soldado1.mano) == 0:\n\t\tjuego = False\n\t\tprint(str(t) + ' ' + '2')\n\telif len(soldado2.mano) == 0:\n\t\tjuego = False\n\t\tprint(str(t) + ' ' + '1')\n", "n = int(input())\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\nused = set()\ncnt = 0\nwhile len(a) != 0 and len(b) != 0:\n\tif (tuple(a), tuple(b)) not in used:\n\t\tused.add((tuple(a), tuple(b)))\n\telse:\n\t\tprint(-1)\n\t\treturn\n\t(x, y) = (a.pop(0), b.pop(0))\n\tif x > y:\n\t\ta.append(y)\n\t\ta.append(x)\n\telse:\n\t\tb.append(x)\n\t\tb.append(y)\n\tcnt += 1\nprint(cnt, '1' if len(b) == 0 else '2')\n", "n = int(input())\na = list(map(int, input().split()))\nb = list(map(int, input().split()))\na = a[1:]\nx = a\nb = b[1:]\ny = b\nf = -1\ncnt = 0\nwhile cnt < 500:\n\tk1 = a.pop(0)\n\tk2 = b.pop(0)\n\tif k1 > k2:\n\t\ta.append(k2)\n\t\ta.append(k1)\n\telse:\n\t\tb.append(k1)\n\t\tb.append(k2)\n\tcnt += 1\n\tif len(a) == 0:\n\t\tf = 2\n\t\tbreak\n\tif len(b) == 0:\n\t\tf = 1\n\t\tbreak\nif cnt == 500 or f == -1:\n\tprint(-1)\nelse:\n\tprint(cnt, f)\n", "import time\nstart_time = time.time()\nn = int(input())\nfir = list(map(int, input().split()))\nsec = list(map(int, input().split()))\nfir.pop(0)\nsec.pop(0)\nfight_cnt = 0\nimpossible = False\nwhile fir and sec:\n\tfir_card = fir.pop(0)\n\tsec_card = sec.pop(0)\n\tif fir_card > sec_card:\n\t\tfir.append(sec_card)\n\t\tfir.append(fir_card)\n\telse:\n\t\tsec.append(fir_card)\n\t\tsec.append(sec_card)\n\tfight_cnt += 1\n\tif time.time() - start_time > 1.9:\n\t\timpossible = True\n\t\tbreak\nif impossible:\n\tprint(-1)\nelse:\n\tprint(fight_cnt, 1 if fir else 2)\n", "n = int(input())\nl1 = list(map(int, input().split()))\nk1 = l1[0]\ndel l1[0]\nl2 = list(map(int, input().split()))\nk2 = l2[0]\ndel l2[0]\norg_state1 = l1[:]\norg_state2 = l2[:]\noccured_states = [[org_state1, org_state2]]\ncount = 0\nwhile l1 != [] and l2 != []:\n\tc1 = l1.pop(0)\n\tc2 = l2.pop(0)\n\tif c1 > c2:\n\t\tl1.append(c2)\n\t\tl1.append(c1)\n\telse:\n\t\tl2.append(c1)\n\t\tl2.append(c2)\n\tif [l1, l2] not in occured_states:\n\t\toccured_states.append([l1[:], l2[:]])\n\t\tcount += 1\n\telse:\n\t\tprint(-1)\n\t\tbreak\nif l1 == []:\n\tprint(count, 2)\nelif l2 == []:\n\tprint(count, 1)\n", "n = int(input())\na = list(map(int, input().split()))\nb = list(map(int, input().split()))\n(k, l) = (a[0], b[0])\na.pop(0)\nb.pop(0)\ncnt = 0\nwhile len(a) > 0 and len(b) > 0 and (cnt < 1000):\n\tif a[0] > b[0]:\n\t\ta.append(b[0])\n\t\ta.append(a[0])\n\tif b[0] > a[0]:\n\t\tb.append(a[0])\n\t\tb.append(b[0])\n\tcnt += 1\n\ta.pop(0)\n\tb.pop(0)\nif len(a) == 0:\n\tprint(cnt, 2)\n\tquit()\nif len(b) == 0:\n\tprint(cnt, 1)\n\tquit()\nif len(a) > 0 and len(b) > 0:\n\tprint(-1)\nelse:\n\tprint(cnt)\n", "import math, sys\nfrom collections import Counter, defaultdict, deque\nfrom sys import stdin, stdout\ninput = stdin.readline\nlili = lambda : list(map(int, sys.stdin.readlines()))\nli = lambda : list(map(int, input().split()))\nI = lambda : int(input())\nS = lambda : input().strip()\nn = I()\na = li()\nb = li()\nc = deque()\nd = deque()\nfor i in range(1, a[0] + 1):\n\tc.appendleft(a[i])\nfor i in range(1, b[0] + 1):\n\td.appendleft(b[i])\na = a[1:]\nb = b[1:]\nt = 0\nf = 0\nh = defaultdict(list)\nwhile len(c) != 0 and len(d) != 0:\n\tt += 1\n\tp = c.pop()\n\tq = d.pop()\n\tif t >= 1000:\n\t\tf = 1\n\t\tbreak\n\tif p < q:\n\t\td.appendleft(p)\n\t\td.appendleft(q)\n\telse:\n\t\tc.appendleft(q)\n\t\tc.appendleft(p)\nif f == 1:\n\tprint(-1)\n\texit()\nif len(c) == 0:\n\tprint(t, 2)\nelse:\n\tprint(t, 1)\n", "n = int(input())\n(n1, *c1) = list(map(int, input().split()))\n(n2, *c2) = list(map(int, input().split()))\nfrom collections import deque\nq1 = deque(c1)\nq2 = deque(c2)\ncount = 0\nwhile len(q1) > 0 and len(q2) > 0:\n\tcount += 1\n\tif count > 99999:\n\t\tprint('-1')\n\t\texit(0)\n\ti1 = q1.popleft()\n\ti2 = q2.popleft()\n\tif i1 > i2:\n\t\tq1.append(i2)\n\t\tq1.append(i1)\n\telse:\n\t\tq2.append(i1)\n\t\tq2.append(i2)\nif len(q1):\n\tprint('%d 1' % count)\nelse:\n\tprint('%d 2' % count)\n", "n = int(input())\nlst1 = list(map(int, input().split()))\nk1 = lst1.pop(0)\nlst2 = list(map(int, input().split()))\nk2 = lst2.pop(0)\ncnt = 0\n(last1, last2) = (lst1[0], lst2[0])\nwhile 1:\n\tif len(lst1) == 0 or len(lst2) == 0 or cnt > 10000000.0:\n\t\tbreak\n\ta = lst1.pop(0)\n\tb = lst2.pop(0)\n\tif last1 == b and last2 == a:\n\t\tcnt = -1\n\t\tbreak\n\tlast1 = a\n\tlast2 = b\n\tif a > b:\n\t\tlst1.append(b)\n\t\tlst1.append(a)\n\telse:\n\t\tlst2.append(a)\n\t\tlst2.append(b)\n\tcnt += 1\nif cnt == -1 or cnt > 10000000.0:\n\tprint(-1)\nelif len(lst1) == 0:\n\tprint(cnt, 2)\nelse:\n\tprint(cnt, 1)\n", "n = int(input())\narr1 = [int(i) for i in input().split()]\narr2 = [int(i) for i in input().split()]\nn1 = arr1.pop(0)\nn2 = arr2.pop(0)\nstart = [i for i in arr1]\nend = [i for i in arr2]\ncount = 0\nflag = 0\nwhile count != 100000:\n\tif arr1[0] > arr2[0]:\n\t\tarr1.append(arr2.pop(0))\n\t\tarr1.append(arr1.pop(0))\n\t\tn1 += 1\n\t\tn2 -= 1\n\telse:\n\t\tarr2.append(arr1.pop(0))\n\t\tarr2.append(arr2.pop(0))\n\t\tn2 += 1\n\t\tn1 -= 1\n\tcount += 1\n\tif n1 == 0 or n2 == 0:\n\t\tif n1 == 0:\n\t\t\tprint(count, 2)\n\t\telse:\n\t\t\tprint(count, 1)\n\t\tflag = 1\n\t\tbreak\nif flag == 0:\n\tprint(-1)\n", "n = int(input())\nar1 = list(map(int, input().split()))\nar2 = list(map(int, input().split()))\na = ar1[1:]\nb = ar2[1:]\nnewa = a[:]\nflag = 1\nnewb = b[:]\ncnt = 0\nwhile len(newa) != 0 or len(newb) != 0:\n\tif newa[0] > newb[0]:\n\t\tt = newa[0]\n\t\tnewa.remove(newa[0])\n\t\tnewa.append(newb[0])\n\t\tnewa.append(t)\n\t\tcnt += 1\n\t\tnewb.remove(newb[0])\n\telse:\n\t\tt = newb[0]\n\t\tnewb.remove(newb[0])\n\t\tnewb.append(newa[0])\n\t\tnewb.append(t)\n\t\tcnt += 1\n\t\tnewa.remove(newa[0])\n\tif len(newa) == 0:\n\t\tz = 2\n\tif len(newb) == 0:\n\t\tz = 1\n\tif len(newa) == 0 or len(newb) == 0:\n\t\tbreak\n\tif cnt > 10000:\n\t\tflag = 0\n\t\tbreak\nif flag == 1:\n\tprint(str(cnt) + ' ' + str(z))\nelse:\n\tprint(-1)\n", "n = int(input())\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\nlog = []\nr = 0\n\ndef spam(x, y):\n\treturn 2 if not x else 1\nwhile a != [] and b != []:\n\tr += 1\n\tl = '{} - {}'.format(str(a), str(b))\n\tif a[0] > b[0]:\n\t\ta.append(b[0])\n\t\ta.append(a[0])\n\t\tb.remove(b[0])\n\t\ta.remove(a[0])\n\telse:\n\t\tb.append(a[0])\n\t\tb.append(b[0])\n\t\tb.remove(b[0])\n\t\ta.remove(a[0])\n\tif l in log:\n\t\tprint(-1)\n\t\tbreak\n\telse:\n\t\tlog += [l]\nelse:\n\tprint(r, spam(a, b))\n", "from collections import deque\n\ndef main():\n\tn = int(input())\n\ts1 = deque(list(map(int, input().split()))[1:])\n\ts2 = deque(list(map(int, input().split()))[1:])\n\tmove = 0\n\tok = True\n\twhile s1 and s2:\n\t\tc1 = s1.popleft()\n\t\tc2 = s2.popleft()\n\t\tif c1 < c2:\n\t\t\ts2.append(c1)\n\t\t\ts2.append(c2)\n\t\telif c2 < c1:\n\t\t\ts1.append(c2)\n\t\t\ts1.append(c1)\n\t\tmove += 1\n\t\tif move >= 100000:\n\t\t\tok = False\n\t\t\tbreak\n\tif ok:\n\t\twinner = 1 if not s2 else 2\n\t\tprint(move, winner)\n\telse:\n\t\tprint(-1)\nmain()\n", "n = int(input())\np1 = list(map(int, input().strip().split(' ')))\np2 = list(map(int, input().strip().split(' ')))\ndel p1[0]\ndel p2[0]\nplayer1 = []\nplayer2 = []\nk = 0\nq = 0\ncnt = 0\nr = 0\nplayer1.append(p1)\nplayer2.append(p2)\nwhile True:\n\tq1 = []\n\tq2 = []\n\tfor i in p1:\n\t\tq1.append(i)\n\tfor i in p2:\n\t\tq2.append(i)\n\tif len(p1) == 0:\n\t\tk = 1\n\t\tbreak\n\tif len(p2) == 0:\n\t\tq = 1\n\t\tbreak\n\tif q1[0] > q2[0]:\n\t\tw1 = q1[0]\n\t\tw2 = q2[0]\n\t\tdel q2[0]\n\t\tdel q1[0]\n\t\tq1.append(w2)\n\t\tq1.append(w1)\n\telse:\n\t\tw1 = q1[0]\n\t\tw2 = q2[0]\n\t\tdel q2[0]\n\t\tdel q1[0]\n\t\tq2.append(w1)\n\t\tq2.append(w2)\n\tif q1 in player1 and q2 in player2:\n\t\tr = 1\n\t\tbreak\n\telse:\n\t\tp1 = [0 for i in range(len(q1))]\n\t\tp2 = [0 for i in range(len(q2))]\n\t\tfor i in range(len(q1)):\n\t\t\tp1[i] = q1[i]\n\t\tfor i in range(len(q2)):\n\t\t\tp2[i] = q2[i]\n\t\tplayer1.append(p1)\n\t\tplayer2.append(p2)\n\tcnt += 1\nif r == 1:\n\tprint(-1)\nelif k == 1:\n\tprint(cnt, '2')\nelse:\n\tprint(cnt, '1')\n", "n = int(input())\nfrom collections import deque\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\na = deque(a)\nb = deque(b)\nimport copy\nc = a.copy()\nd = b.copy()\n(l, r) = (0, 0)\nwhile len(a) != 0 and len(b) != 0:\n\tp = a.popleft()\n\tq = b.popleft()\n\tif p > q:\n\t\ta.append(q)\n\t\ta.append(p)\n\telse:\n\t\tb.append(p)\n\t\tb.append(q)\n\tl += 1\n\tif c == a and d == b or l >= 100000:\n\t\tr = r + 1\n\t\tbreak\nif r == 0:\n\tprint(l, end=' ')\n\tif len(b) > 0:\n\t\tprint(2)\n\telse:\n\t\tprint(1)\nelse:\n\tprint(-1)\n", "nos = int(input())\nplayer1 = set([])\nplayer2 = set([])\ndec1 = [int(x) for x in input().split()][1:]\ndec2 = [int(x) for x in input().split()][1:]\ncount = 0\nwhile dec1 and dec2:\n\tcard1 = dec1.pop(0)\n\tcard2 = dec2.pop(0)\n\tif card1 > card2:\n\t\tdec1.append(card2)\n\t\tdec1.append(card1)\n\telse:\n\t\tdec2.append(card1)\n\t\tdec2.append(card2)\n\tcount += 1\n\tt2 = tuple(dec2)\n\tt1 = tuple(dec1)\n\tif t1 in player1 and t2 in player2:\n\t\tprint(-1)\n\t\tbreak\n\telse:\n\t\tplayer1.add(t1)\n\t\tplayer2.add(t2)\nelse:\n\tprint(count, 1 if dec1 else 2)\n", "n = int(input())\nb1 = list(map(int, input().split()))\nk1 = b1.pop(0)\nb2 = list(map(int, input().split()))\nk2 = b2.pop(0)\nfor i in range(1, 120):\n\tt1 = b1.pop(0)\n\tt2 = b2.pop(0)\n\tif t1 > t2:\n\t\tb1.extend([t2, t1])\n\telse:\n\t\tb2.extend([t1, t2])\n\tif [] == b1:\n\t\tprint(i, 2)\n\t\tbreak\n\telif [] == b2:\n\t\tprint(i, 1)\n\t\tbreak\nelse:\n\tprint(-1)\n", "def comp(a, b):\n\tif len(a) != len(b):\n\t\treturn False\n\tn = len(a)\n\tfor i in range(n):\n\t\tif a[i] != b[i]:\n\t\t\treturn False\n\treturn True\nn = int(input())\na = []\nb = []\na = list(map(int, input().split()))\nb = list(map(int, input().split()))\nk1 = a[0]\nk2 = b[0]\ndel a[0]\ndel b[0]\np = []\nq = []\nfor i in a:\n\tp.append(i)\nfor i in b:\n\tq.append(i)\ntotal = 0\nflag = True\nwhile True:\n\tx = a[0]\n\ty = b[0]\n\tif x > y:\n\t\ta.pop(0)\n\t\tb.pop(0)\n\t\ta.append(y)\n\t\ta.append(x)\n\telse:\n\t\ta.pop(0)\n\t\tb.pop(0)\n\t\tb.append(x)\n\t\tb.append(y)\n\ttotal += 1\n\tif p == a and q == b or total >= 100000:\n\t\tflag = False\n\t\tbreak\n\tif len(a) == 0 or len(b) == 0:\n\t\tbreak\nif flag == False:\n\tprint(-1)\nelif a == []:\n\tprint(total, 2)\nelse:\n\tprint(total, 1)\n", "import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy\nfrom itertools import chain, dropwhile, permutations, combinations\nfrom collections import defaultdict, deque\n\ndef VI():\n\treturn list(map(int, input().split()))\n\ndef I():\n\treturn int(input())\n\ndef LIST(n, m=None):\n\treturn [0] * n if m is None else [[0] * m for i in range(n)]\n\ndef ELIST(n):\n\treturn [[] for i in range(n)]\n\ndef MI(n=None, m=None):\n\tif n is None:\n\t\t(n, m) = VI()\n\tarr = LIST(n)\n\tfor i in range(n):\n\t\tarr[i] = VI()\n\treturn arr\n\ndef MS(n=None, m=None):\n\tif n is None:\n\t\t(n, m) = VI()\n\tarr = LIST(n)\n\tfor i in range(n):\n\t\tarr[i] = input()\n\treturn arr\n\ndef MIT(n=None, m=None):\n\tif n is None:\n\t\t(n, m) = VI()\n\ta = MI(n, m)\n\tarr = LIST(m, n)\n\tfor (i, l) in enumerate(a):\n\t\tfor (j, x) in enumerate(l):\n\t\t\tarr[j][i] = x\n\treturn arr\n\ndef run(n, k1, k2, a, b):\n\tp = deque(a)\n\tq = deque(b)\n\tfor i in range(2 * n ** 2):\n\t\tif len(p) == 0 or len(q) == 0:\n\t\t\tprint(i, [1, 2][len(p) == 0])\n\t\t\treturn\n\t\tx = p.popleft()\n\t\ty = q.popleft()\n\t\tif x > y:\n\t\t\tp.append(y)\n\t\t\tp.append(x)\n\t\telse:\n\t\t\tq.append(x)\n\t\t\tq.append(y)\n\tprint(-1)\n\ndef main(info=0):\n\tn = I()\n\t(k1, *a) = VI()\n\t(k2, *b) = VI()\n\trun(n, k1, k2, a, b)\nmain()\n", "def to_int(lis):\n\tfor i in range(len(lis)):\n\t\tlis[i] = int(lis[i])\n\treturn lis\nn = int(input())\np1 = input().split()\np2 = input().split()\np1.pop(0)\np2.pop(0)\np1 = to_int(p1)\np2 = to_int(p2)\nset1 = [p1.copy()]\nset2 = [p2.copy()]\nf = 0\ne = 1\nwhile 1:\n\tif len(p1) == 0:\n\t\tprint(f, end=' ')\n\t\tprint(2)\n\t\tbreak\n\telif len(p2) == 0:\n\t\tprint(f, end=' ')\n\t\tprint(1)\n\t\tbreak\n\telse:\n\t\tf += 1\n\t\tn1 = p1[0]\n\t\tn2 = p2[0]\n\t\tp1.pop(0)\n\t\tp2.pop(0)\n\t\tif n1 > n2:\n\t\t\tp1.append(n2)\n\t\t\tp1.append(n1)\n\t\telse:\n\t\t\tp2.append(n1)\n\t\t\tp2.append(n2)\n\tfor i in range(len(set1)):\n\t\tif p1 == set1[i] and p2 == set2[i] and (f != 0):\n\t\t\tprint(-1)\n\t\t\te = 0\n\t\t\tbreak\n\tif e == 0:\n\t\tbreak\n\tset1.append(p1.copy())\n\tset2.append(p2.copy())\n", "def main():\n\ttrash = input()\n\ta = [int(i) for i in input().split()]\n\tb = [int(i) for i in input().split()]\n\tqa = a[1:]\n\tqb = b[1:]\n\tctr = 0\n\twhile ctr < 10 ** 6:\n\t\tif len(qa) == 0:\n\t\t\tprint(ctr, 2)\n\t\t\treturn\n\t\telif len(qb) == 0:\n\t\t\tprint(ctr, 1)\n\t\t\treturn\n\t\tif qa[0] > qb[0]:\n\t\t\tqa.append(qb[0])\n\t\t\tqa.append(qa[0])\n\t\t\tdel qa[0]\n\t\t\tdel qb[0]\n\t\telif qb[0] > qa[0]:\n\t\t\tqb.append(qa[0])\n\t\t\tqb.append(qb[0])\n\t\t\tdel qa[0]\n\t\t\tdel qb[0]\n\t\telse:\n\t\t\tbreak\n\t\tctr += 1\n\tprint(-1)\nmain()\n", "import sys\ninput = sys.stdin.readline\nfrom collections import deque\nn = int(input())\nll1 = deque(map(int, input().split()))\nll2 = deque(map(int, input().split()))\ns1 = ll1[0]\ns2 = ll2[0]\nll1.popleft()\nll2.popleft()\nl1 = deque(ll1)\nl2 = deque(ll2)\ncou = 0\nlol = 0\nwhile len(l1) != 0 and len(l2) != 0:\n\tif l1[0] > l2[0]:\n\t\tl1.append(l2.popleft())\n\t\tl1.append(l1.popleft())\n\telse:\n\t\tl2.append(l1.popleft())\n\t\tl2.append(l2.popleft())\n\tcou += 1\n\tf1 = 0\n\tf2 = 0\n\tif len(l1) == s1:\n\t\tfor i in range(s1):\n\t\t\tif ll1[i] != l1[i]:\n\t\t\t\tf1 = 1\n\t\t\t\tbreak\n\telse:\n\t\tf1 = 1\n\tif len(l2) == s2:\n\t\tfor i in range(s2):\n\t\t\tif ll2[i] != l2[i]:\n\t\t\t\tf2 = 1\n\t\t\t\tbreak\n\telse:\n\t\tf2 = 1\n\tif f1 == 0 and f2 == 0:\n\t\tlol = 1\n\t\tbreak\n\tif cou == 100000:\n\t\tbreak\nif len(l1) == 0:\n\tprint(cou, 2)\nelif len(l2) == 0:\n\tprint(cou, 1)\nelse:\n\tprint(-1)\n", "kart = input()\nplayer_1 = input()\nplayer_2 = input()\nkart_1 = []\nkart_2 = []\nkart_save = []\nkart_save_2 = []\ni = 0\nwhile i < len(player_1):\n\tif i > 0:\n\t\tif player_1[i] != ' ':\n\t\t\tif i < len(player_1) - 1:\n\t\t\t\tif player_1[i + 1] != ' ':\n\t\t\t\t\ttmp = player_1[i] + player_1[i + 1]\n\t\t\t\t\tkart_1.append(int(tmp))\n\t\t\t\t\ti = i + 1\n\t\t\t\telse:\n\t\t\t\t\tkart_1.append(int(player_1[i]))\n\t\t\telse:\n\t\t\t\tkart_1.append(int(player_1[i]))\n\ti = i + 1\ni = 0\nwhile i < len(player_2):\n\tif i > 0:\n\t\tif player_2[i] != ' ':\n\t\t\tif i < len(player_2) - 1:\n\t\t\t\tif player_2[i + 1] != ' ':\n\t\t\t\t\ttmp = player_2[i] + player_2[i + 1]\n\t\t\t\t\tkart_2.append(int(tmp))\n\t\t\t\t\ti = i + 1\n\t\t\t\telse:\n\t\t\t\t\tkart_2.append(int(player_2[i]))\n\t\t\telse:\n\t\t\t\tkart_2.append(int(player_2[i]))\n\ti = i + 1\ncounter = 0\nif kart_1.count(int(kart)) == 1:\n\tfor i in kart_1:\n\t\tkart_save.append(i)\n\tfor i in kart_2:\n\t\tkart_save_2.append(i)\n\twhile len(kart_1) < int(kart):\n\t\tif kart_1[0] > kart_2[0]:\n\t\t\ttmp = kart_2.pop(0)\n\t\t\tkart_1.append(tmp)\n\t\t\ttmp = kart_1.pop(0)\n\t\t\tkart_1.append(tmp)\n\t\telse:\n\t\t\ttmp = kart_1.pop(0)\n\t\t\tkart_2.append(tmp)\n\t\t\ttmp = kart_2.pop(0)\n\t\t\tkart_2.append(tmp)\n\t\tcounter = counter + 1\n\t\tif kart_1 == kart_save and kart_2 == kart_save_2 or counter > 100000:\n\t\t\tprint('-1')\n\t\t\tbreak\n\tif len(kart_1) == int(kart):\n\t\tprint(counter, 1)\nelse:\n\tfor i in kart_1:\n\t\tkart_save.append(i)\n\tfor i in kart_2:\n\t\tkart_save_2.append(i)\n\twhile len(kart_2) < int(kart):\n\t\tif kart_1[0] > kart_2[0]:\n\t\t\ttmp = kart_2.pop(0)\n\t\t\tkart_1.append(tmp)\n\t\t\ttmp = kart_1.pop(0)\n\t\t\tkart_1.append(tmp)\n\t\telse:\n\t\t\ttmp = kart_1.pop(0)\n\t\t\tkart_2.append(tmp)\n\t\t\ttmp = kart_2.pop(0)\n\t\t\tkart_2.append(tmp)\n\t\tcounter = counter + 1\n\t\tif kart_1 == kart_save and kart_2 == kart_save_2 or counter > 100000:\n\t\t\tprint('-1')\n\t\t\tbreak\n\tif len(kart_2) == int(kart):\n\t\tprint(counter, 2)\n", "n = int(input())\nl1 = list(map(int, input().split()))\nl2 = list(map(int, input().split()))\n(l1, l2) = (l1[1:], l2[1:])\n(temp, count, poppp) = ([], 0, 0)\nwhile 1:\n\tif count > 200:\n\t\tpoppp = 1\n\t\tbreak\n\telif l1[0] == l2[0]:\n\t\tpoppp = 1\n\t\tbreak\n\telse:\n\t\tif l1[0] > l2[0]:\n\t\t\tl1.append(l2[0])\n\t\t\tl1.append(l1[0])\n\t\t\tl1.pop(0)\n\t\t\tl2.pop(0)\n\t\telse:\n\t\t\tl2.append(l1[0])\n\t\t\tl2.append(l2[0])\n\t\t\tl1.pop(0)\n\t\t\tl2.pop(0)\n\t\tcount += 1\n\tif len(l1) == 0:\n\t\twinner = 2\n\t\tbreak\n\tif len(l2) == 0:\n\t\twinner = 1\n\t\tbreak\nif poppp:\n\tprint(-1)\nelse:\n\tprint(count, winner)\n", "n = int(input())\ns1 = [int(card) for card in input().split()]\ns2 = [int(card) for card in input().split()]\ns1 = s1[1:]\ns2 = s2[1:]\nmagic = 1000000\nfights = 0\nflag = False\nwhile True:\n\t(card1, card2) = (s1.pop(0), s2.pop(0))\n\tfights += 1\n\tif fights > magic:\n\t\tflag = True\n\t\tbreak\n\tif card1 > card2:\n\t\ts1.append(card2)\n\t\ts1.append(card1)\n\telse:\n\t\ts2.append(card1)\n\t\ts2.append(card2)\n\tif s1 == []:\n\t\twinner = 2\n\t\tbreak\n\tif s2 == []:\n\t\twinner = 1\n\t\tbreak\nif flag:\n\tprint(-1)\nelse:\n\tprint(fights, winner)\n", "n = int(input())\nc1 = list(map(int, input().split()))[1:]\nc2 = list(map(int, input().split()))[1:]\nres = 0\nwhile c1 and c2:\n\tif res > 1000:\n\t\tprint(-1)\n\t\texit()\n\tres = res + 1\n\t(x, y) = (c1.pop(0), c2.pop(0))\n\tif x > y:\n\t\tc1 += [y, x]\n\telse:\n\t\tc2 += [x, y]\nif c1:\n\tprint(res, 1)\nelse:\n\tprint(res, 2)\n", "n = int(input())\ns = input()\nli = list(map(int, s.split()))\ns = input()\nli2 = list(map(int, s.split()))\nli.pop(0)\nli2.pop(0)\nnn = 1\nfor i in range(n):\n\tnn *= i + 1\ncnt = 0\nfor i in range(nn):\n\tsz = len(li)\n\tsz2 = len(li2)\n\tif sz == 0:\n\t\tprint(cnt, 2)\n\t\texit()\n\telif sz2 == 0:\n\t\tprint(cnt, 1)\n\t\texit()\n\tcnt += 1\n\tx = li[0]\n\tx2 = li2[0]\n\tif x > x2:\n\t\tli.pop(0)\n\t\tli.append(x2)\n\t\tli.append(x)\n\t\tli2.pop(0)\n\telse:\n\t\tli2.pop(0)\n\t\tli2.append(x)\n\t\tli2.append(x2)\n\t\tli.pop(0)\nprint(-1)\n", "mod = 1000000007\nii = lambda : int(input())\nsi = lambda : input()\ndgl = lambda : list(map(int, input()))\nf = lambda : map(int, input().split())\nil = lambda : list(map(int, input().split()))\nls = lambda : list(input())\nn = ii()\n(k1, *l1) = f()\n(k2, *l2) = f()\nld1 = l1[:]\nld2 = l2[:]\nans = 0\nwhile 1:\n\tif ld1[0] < ld2[0]:\n\t\tld2.append(ld1[0])\n\t\tld2.append(ld2[0])\n\t\tld1.pop(0)\n\t\tld2.pop(0)\n\telse:\n\t\tld1.append(ld2[0])\n\t\tld1.append(ld1[0])\n\t\tld1.pop(0)\n\t\tld2.pop(0)\n\tans += 1\n\tif ld1 == []:\n\t\tprint(ans, 2)\n\t\texit(0)\n\telif ld2 == []:\n\t\tprint(ans, 1)\n\t\texit(0)\n\telif ans > 10000:\n\t\tprint(-1)\n\t\texit()\n", "n = list(map(int, input().split()))\nl_1 = list(map(int, input().split()))[1:]\nl_2 = list(map(int, input().split()))[1:]\nl_1c = l_1.copy()\nl_2c = l_2.copy()\nt = 0\nwhile len(l_1c) != 0 and len(l_2c) != 0:\n\tif l_1c[0] == l_2c[0]:\n\t\tl_1c.append(l_1c[0])\n\t\tl_2c.append(l_2c[0])\n\telif l_1c[0] > l_2c[0]:\n\t\tl_1c.append(l_2c[0])\n\t\tl_1c.append(l_1c[0])\n\telse:\n\t\tl_2c.append(l_1c[0])\n\t\tl_2c.append(l_2c[0])\n\tdel l_1c[0]\n\tdel l_2c[0]\n\tt += 1\n\tif t > 10000:\n\t\tprint(-1)\n\t\tquit()\nprint('%d %d' % (t, 2 if len(l_1c) == 0 else 1))\n", "n = int(input())\nc1 = list(map(int, input().split()))[1:]\nc2 = list(map(int, input().split()))[1:]\n\ndef check(c1, c2):\n\t(t1, t2) = (c1.pop(0), c2.pop(0))\n\tif t1 < t2:\n\t\tc2 += [t1, t2]\n\telse:\n\t\tc1 += [t2, t1]\ncount = 0\nwhile c1 != [] and c2 != []:\n\t(prev_c1, prev_c2) = (c1[:], c2[:])\n\tcheck(c1, c2)\n\tif count > 10000:\n\t\tbreak\n\tcount += 1\nelse:\n\tprint(count, 2 if c1 == [] else 1)\n\tquit()\nprint(-1)\n", "__author__ = 'trunghieu11'\nn = int(input())\nfirst = list(map(int, input().split()))[1:]\nsecond = list(map(int, input().split()))[1:]\nfirst.reverse()\nsecond.reverse()\nround = 0\nwhile round < 10000:\n\tif len(first) * len(second) == 0:\n\t\tbreak\n\tfCard = first.pop()\n\tsCard = second.pop()\n\tif fCard > sCard:\n\t\tfirst.insert(0, sCard)\n\t\tfirst.insert(0, fCard)\n\telse:\n\t\tsecond.insert(0, fCard)\n\t\tsecond.insert(0, sCard)\n\tround += 1\nif len(first) == 0:\n\tprint(round, 2)\nelif len(second) == 0:\n\tprint(round, 1)\nelse:\n\tprint(-1)\n", "n = int(input())\na = list(map(int, input().split(' ')))\nk = a.pop(0)\nb = list(map(int, input().split(' ')))\nk1 = b.pop(0)\ntotal = 0\ns = 1\nwhile len(a) > 0 and len(b) > 0:\n\tif a[0] > b[0]:\n\t\ttotal = total + 1\n\t\ta.append(b[0])\n\t\tb.pop(0)\n\t\tc = a.pop(0)\n\t\ta.append(c)\n\telse:\n\t\ttotal = total + 1\n\t\tb.append(a[0])\n\t\ta.pop(0)\n\t\tc = b.pop(0)\n\t\tb.append(c)\n\tif total > 5000:\n\t\ts = 0\n\t\tbreak\nif s == 0:\n\tprint('-1')\nelif len(a) > len(b):\n\tprint(total, '1')\nelse:\n\tprint(total, '2')\n", "import sys\nn = int(sys.stdin.readline())\na = list(map(int, sys.stdin.readline().split()))\nb = list(map(int, sys.stdin.readline().split()))\nkone = a[0]\na.pop(0)\nktwo = b[0]\nb.pop(0)\nrec = []\nrec.append((a.copy(), b.copy()))\ncount = 0\nrepeat = False\nwhile len(a) != 0 and len(b) != 0:\n\tatemp = a.pop(0)\n\tbtemp = b.pop(0)\n\tif atemp > btemp:\n\t\ta.append(btemp)\n\t\ta.append(atemp)\n\telse:\n\t\tb.append(atemp)\n\t\tb.append(btemp)\n\ttemp = (a.copy(), b.copy())\n\tif temp in rec:\n\t\trepeat = True\n\t\tprint(-1)\n\t\tbreak\n\telse:\n\t\trec.append(temp)\n\t\tcount += 1\nif not repeat:\n\tres = 1 if len(b) == 0 else 2\n\tprint(str(count) + ' ' + str(res))\n", "nCards = int(input())\n(n1Cards, *cards1) = [int(x) for x in input().split()]\n(n2Cards, *cards2) = [int(x) for x in input().split()]\ntop1 = 0\ntop2 = 0\nend1 = n1Cards\nend2 = n2Cards\nseenStates = set((tuple(sum([cards1, [0], cards2], [])),))\nturns = 0\nwhile top1 != end1 and top2 != end2:\n\tif cards1[top1] > cards2[top2]:\n\t\tcards1.append(cards2[top2])\n\t\tcards1.append(cards1[top1])\n\t\ttop1 += 1\n\t\ttop2 += 1\n\t\tend1 += 2\n\telse:\n\t\tcards2.append(cards1[top1])\n\t\tcards2.append(cards2[top2])\n\t\ttop1 += 1\n\t\ttop2 += 1\n\t\tend2 += 2\n\tif (tuple(sum([cards1[top1:end1 + 1], [0], cards2[top2:end2 + 1]], [])),) in seenStates:\n\t\tprint(-1)\n\t\tbreak\n\tseenStates.add((tuple(sum([cards1[top1:end1 + 1], [0], cards2[top2:end2 + 1]], [])),))\n\tturns += 1\nelse:\n\tif top1 == end1:\n\t\tprint(turns, 2)\n\telse:\n\t\tprint(turns, 1)\n", "from collections import deque\nn = int(input())\na = deque([int(i) for i in input().split()[1:]])\nb = deque([int(i) for i in input().split()[1:]])\nans = 0\nwhile a and b:\n\tans += 1\n\tif ans > 1000000:\n\t\tprint(-1)\n\t\texit()\n\taa = a.popleft()\n\tbb = b.popleft()\n\tif aa > bb:\n\t\ta.append(bb)\n\t\ta.append(aa)\n\telse:\n\t\tb.append(aa)\n\t\tb.append(bb)\nif a:\n\tprint(ans, 1)\nelse:\n\tprint(ans, 2)\n", "import copy\ncartas = int(input())\np1 = list(map(int, input().split(' ')))[1:]\np2 = list(map(int, input().split(' ')))[1:]\ncont = 0\nestados = []\nwhile True:\n\tp1t = copy.copy(p1)\n\tp2t = copy.copy(p2)\n\testados.append([p1t, p2t])\n\tif p1 == []:\n\t\tprint('{} 2'.format(cont))\n\t\tbreak\n\tif p2 == []:\n\t\tprint('{} 1'.format(cont))\n\t\tbreak\n\tif p1[0] > p2[0]:\n\t\tcont += 1\n\t\tp1.append(p2.pop(0))\n\t\tp1.append(p1.pop(0))\n\telse:\n\t\tcont += 1\n\t\tp2.append(p1.pop(0))\n\t\tp2.append(p2.pop(0))\n\tp = [p1, p2]\n\tif p in estados:\n\t\tprint(-1)\n\t\tbreak\n", "class Gamer:\n\n\tdef __init__(self):\n\t\tself.cards = []\n\n\tdef play_card(self):\n\t\tif not self.cards:\n\t\t\treturn False\n\t\treturn self.cards.pop(0)\n\n\tdef win_round(self, *cards):\n\t\tself.cards.extend(cards)\n\ndef game_round(gamer_a, gamer_b):\n\tcard_a = gamer_a.play_card()\n\tcard_b = gamer_b.play_card()\n\tif card_a > card_b:\n\t\tgamer_a.win_round(card_b, card_a)\n\t\tif not gamer_b.cards:\n\t\t\treturn 1\n\t\treturn False\n\tif card_a < card_b:\n\t\tgamer_b.win_round(card_a, card_b)\n\t\tif not gamer_a.cards:\n\t\t\treturn 2\n\t\treturn False\n\ndef game(gamer_a, gamer_b, c_q):\n\tcount = 1\n\tfor i in range(106):\n\t\tx = game_round(gamer_a, gamer_b)\n\t\tif x:\n\t\t\tprint(count, x, sep=' ')\n\t\t\tbreak\n\t\tcount += 1\n\telse:\n\t\tprint(-1)\nsoldier1 = Gamer()\nsoldier2 = Gamer()\ncards_quantity = int(input().rstrip())\ncard_set1 = list(map(int, input().split()))\ncard_set2 = list(map(int, input().split()))\nsoldier1.win_round(*card_set1[1:])\nsoldier2.win_round(*card_set2[1:])\ngame(soldier1, soldier2, cards_quantity)\n", "n = int(input())\nl1 = list(map(int, input().strip().split()))\nl2 = list(map(int, input().strip().split()))\nk1 = l1.pop(0)\nk2 = l2.pop(0)\nstate = []\nf = 0\nwhile True:\n\tif [l1, l2] in state:\n\t\tprint(-1)\n\t\tbreak\n\telse:\n\t\tstate.append([[k for k in l1], [i for i in l2]])\n\tif l1 == []:\n\t\tprint(f, 2)\n\t\tbreak\n\tif l2 == []:\n\t\tprint(f, 1)\n\t\tbreak\n\tf += 1\n\tif l1[0] > l2[0]:\n\t\tl1.append(l2.pop(0))\n\t\tl1.append(l1.pop(0))\n\telse:\n\t\tl2.append(l1.pop(0))\n\t\tl2.append(l2.pop(0))\n", "from collections import deque\nn = int(input())\nplayer1 = [int(x) for x in input().split()]\nplayer2 = [int(x) for x in input().split()]\nplayer1Cards = deque([])\nplayer2Cards = deque([])\nfor i in range(1, player1[0] + 1):\n\tplayer1Cards.append(player1[i])\nfor i in range(1, player2[0] + 1):\n\tplayer2Cards.append(player2[i])\nitera = 0\na = True\nwhile itera < n * 50:\n\tcard1 = player1Cards.popleft()\n\tcard2 = player2Cards.popleft()\n\tif card1 > card2:\n\t\tplayer1Cards.append(card2)\n\t\tplayer1Cards.append(card1)\n\t\tif len(player2Cards) == 0:\n\t\t\tprint(str(itera + 1) + ' ' + str(1))\n\t\t\ta = False\n\t\t\tbreak\n\telse:\n\t\tplayer2Cards.append(card1)\n\t\tplayer2Cards.append(card2)\n\t\tif len(player1Cards) == 0:\n\t\t\tprint(str(itera + 1) + ' ' + str(2))\n\t\t\ta = False\n\t\t\tbreak\n\titera += 1\nif a:\n\tprint(str(-1))\n", "n = int(input())\n(k1, *l1) = map(int, input().split())\n(k2, *l2) = map(int, input().split())\nl = [l1, l2]\nr = []\nc = 0\nwhile 1:\n\tif l[0] == []:\n\t\tprint(c, end=' ')\n\t\tprint(2)\n\t\tbreak\n\telif l[1] == []:\n\t\tprint(c, end=' ')\n\t\tprint(1)\n\t\tbreak\n\telse:\n\t\tb = l[0][0] > l[1][0]\n\t\tif b:\n\t\t\tl[0].append(l[1][0])\n\t\t\tl[0].append(l[0][0])\n\t\t\tl[1].pop(0)\n\t\t\tl[0].pop(0)\n\t\t\tlr = list(map(str, l))\n\t\t\ta = ''.join(lr[:][0]) + ':' + ''.join(lr[:][1])\n\t\t\tif a in r:\n\t\t\t\tprint(-1)\n\t\t\t\tbreak\n\t\t\tr.append(a)\n\t\t\tc += 1\n\t\telse:\n\t\t\tl[1].append(l[0][0])\n\t\t\tl[1].append(l[1][0])\n\t\t\tl[0].pop(0)\n\t\t\tl[1].pop(0)\n\t\t\tlr = list(map(str, l))\n\t\t\ta = ''.join(lr[:][0]) + ':' + ''.join(lr[:][1])\n\t\t\tif a in r:\n\t\t\t\tprint(-1)\n\t\t\t\tbreak\n\t\t\tr.append(a)\n\t\t\tc += 1\n", "n = int(input())\na = [int(c) for c in input().split()]\nb = [int(c) for c in input().split()]\ndel a[0]\ndel b[0]\ndict = set()\nmoves = 0\nwhile True:\n\tif len(a) == 0 or len(b) == 0 or ''.join((str(e) for e in a)) + '.' + ''.join((str(e) for e in b)) in dict:\n\t\tbreak\n\tdict.add(''.join((str(e) for e in a)) + '.' + ''.join((str(e) for e in b)))\n\twinner = []\n\tlooser = []\n\tif a[0] > b[0]:\n\t\twinner = a\n\t\tlooser = b\n\telse:\n\t\twinner = b\n\t\tlooser = a\n\twinner.append(looser[0])\n\twinner.append(winner[0])\n\tdel winner[0]\n\tdel looser[0]\n\tmoves += 1\nif len(a) == 0:\n\tprint('%d 2' % moves)\nelif len(b) == 0:\n\tprint('%d 1' % moves)\nelse:\n\tprint('-1')\n", "n = int(input())\nl = list(map(int, input().split()))\nl1 = list(map(int, input().split()))\nk = 0\ndel l[0]\ndel l1[0]\nc = 1\nv = []\nv1 = []\nv.append(str(l))\nv1.append(str(l1))\nwhile len(l) != 0 and len(l1) != 0:\n\tp = l[0]\n\tp1 = l1[0]\n\tdel l[0]\n\tdel l1[0]\n\tif p > p1:\n\t\tl.append(p1)\n\t\tl.append(p)\n\tif p < p1:\n\t\tl1.append(p)\n\t\tl1.append(p1)\n\tif p == p1:\n\t\tl.append(p)\n\t\tl1.append(p)\n\tif str(l) in v and str(l1) in v1:\n\t\tc = 0\n\t\tbreak\n\tv.append(str(l))\n\tv1.append(str(l1))\n\tk = k + 1\nif c == 0:\n\tprint(-1)\nelse:\n\tg = []\n\tif len(l) > len(l1):\n\t\tg.append(1)\n\telse:\n\t\tg.append(2)\n\tprint(k, *g)\n", "import sys\nn = int(input())\narr1 = list(map(int, input().split()))\narr2 = list(map(int, input().split()))\nk1 = arr1[0]\nk2 = arr2[0]\narr1 = arr1[1:]\narr2 = arr2[1:]\niterx = 0\nwhile iterx < 2000:\n\tif len(arr1) == 0:\n\t\tprint(iterx, 2, sep=' ')\n\t\tsys.exit()\n\tif len(arr2) == 0:\n\t\tprint(iterx, 1, sep=' ')\n\t\tsys.exit()\n\tif arr1[0] > arr2[0]:\n\t\tarr1 = arr1 + [arr2[0], arr1[0]]\n\t\tarr2 = arr2[1:]\n\t\tarr1 = arr1[1:]\n\telse:\n\t\tarr2 = arr2 + [arr1[0], arr2[0]]\n\t\tarr1 = arr1[1:]\n\t\tarr2 = arr2[1:]\n\titerx += 1\nprint(-1)\n", "import time\nx = input()\nnumberCards = int(x)\ncardsPlayer1 = [int(i) for i in input().split()]\ncardsPlayer2 = [int(i) for i in input().split()]\nnumberCardsPlayer1 = cardsPlayer1.pop(0)\nnumberCardsPlayer2 = cardsPlayer2.pop(0)\noriginalCardsPlayer1 = []\noriginalCardsPlayer2 = []\nnumberBattles = 0\ngamePossible = True\nwhile 1:\n\toriginalCardsPlayer1.append(list(cardsPlayer1))\n\toriginalCardsPlayer2.append(list(cardsPlayer2))\n\tif len(cardsPlayer1) == 0 or len(cardsPlayer2) == 0:\n\t\tbreak\n\tnumberBattles += 1\n\telement1 = cardsPlayer1.pop(0)\n\telement2 = cardsPlayer2.pop(0)\n\tif element2 > element1:\n\t\tcardsPlayer2.append(element1)\n\t\tcardsPlayer2.append(element2)\n\telse:\n\t\tcardsPlayer1.append(element2)\n\t\tcardsPlayer1.append(element1)\n\tif cardsPlayer1 in originalCardsPlayer1 and cardsPlayer2 in originalCardsPlayer2:\n\t\tgamePossible = False\n\t\tbreak\nif gamePossible == True:\n\tif len(cardsPlayer1) == 0:\n\t\tprint(numberBattles, 2)\n\telse:\n\t\tprint(numberBattles, 1)\nelse:\n\tprint(-1)\n", "import sys\ninput = sys.stdin.readline\nfrom collections import deque\nx = int(input())\n(*li1,) = map(int, input().split())\n(*li2,) = map(int, input().split())\n(a, b) = (li1[0], li2[0])\ncnt = 0\nli1 = deque(li1[1:])\nli2 = deque(li2[1:])\nwhile cnt < 1000:\n\tc = li1.popleft()\n\td = li2.popleft()\n\tif c > d:\n\t\tli1 += [d, c]\n\telse:\n\t\tli2 += [c, d]\n\tcnt += 1\n\tif len(li1) == 0 or len(li2) == 0:\n\t\tprint(cnt, 1 if len(li2) == 0 else 2)\n\t\tbreak\nelse:\n\tprint(-1)\n", "def top(a, b):\n\tif a > b:\n\t\treturn 1\n\tif b > a:\n\t\treturn 2\ncc = int(input())\np1 = list(map(int, input().split()))\np2 = list(map(int, input().split()))\ngame = 1\ntie = []\nf = 0\nlog = {}\ncheck = 0\ng = []\nwhile game == 1:\n\td = top(p1[1], p2[1])\n\tif d == 1:\n\t\tp1.append(p2[1])\n\t\tp2.pop(1)\n\t\tp1.append(p1[1])\n\t\tp1.pop(1)\n\t\tf += 1\n\telif d == 2:\n\t\tp2.append(p1[1])\n\t\tp1.pop(1)\n\t\tp2.append(p2[1])\n\t\tp2.pop(1)\n\t\tf += 1\n\tif len(p1) == 1:\n\t\twinner = 2\n\t\tgame = 0\n\tif len(p2) == 1:\n\t\twinner = 1\n\t\tgame = 0\n\tif f > 20:\n\t\tif p1[1:] not in g:\n\t\t\tg.append(p1[1:])\n\t\te = g.index(p1[1:])\n\t\tlog[e] = log.get(e, 0) + 1\n\t\tif log[e] == 10:\n\t\t\twinner = 0\n\t\t\tgame = 0\nif winner > 0:\n\tprint(str(f) + ' ' + str(winner))\nif winner == 0:\n\tprint(-1)\n", "from collections import deque\nn = int(input())\nd = set()\n(k1, *a) = [int(s) for s in input().split()]\n(k2, *b) = [int(s) for s in input().split()]\na = deque(a)\nb = deque(b)\nres = 0\nwhile a and b:\n\tcu = tuple([tuple(a), tuple(b)])\n\tif not cu in d:\n\t\td.add(cu)\n\t\tres += 1\n\t\tcur1 = a.popleft()\n\t\tcur2 = b.popleft()\n\t\tif cur1 > cur2:\n\t\t\ta.append(cur2)\n\t\t\ta.append(cur1)\n\t\telse:\n\t\t\tb.append(cur1)\n\t\t\tb.append(cur2)\n\telse:\n\t\tprint(-1)\n\t\tbreak\nelse:\n\tif len(a) > len(b):\n\t\tprint(res, 1)\n\telse:\n\t\tprint(res, 2)\n", "__author__ = 'Andrey'\nimport collections\nimport time\nstart = time.time()\nn = int(input())\ncards_1 = collections.deque(list(map(int, input().split()))[1:])\ncards_2 = collections.deque(list(map(int, input().split()))[1:])\nc = 0\nbad = False\nwhile cards_1 and cards_2:\n\tend = time.time()\n\tif end - start > 1.85:\n\t\tbad = True\n\t\tbreak\n\tn_1 = cards_1.popleft()\n\tn_2 = cards_2.popleft()\n\tc += 1\n\tif n_1 > n_2:\n\t\tcards_1.append(n_2)\n\t\tcards_1.append(n_1)\n\telse:\n\t\tcards_2.append(n_1)\n\t\tcards_2.append(n_2)\nelse:\n\tprint(c, 1 if cards_1 else 2)\nif bad:\n\tprint(-1)\n", "from collections import deque\nn = int(input())\nA = deque(list(map(int, input().split())))\nB = deque(list(map(int, input().split())))\nA.popleft()\nB.popleft()\nfor i in range(3 * 10 ** 6):\n\tif len(A) == 0:\n\t\tprint(i, 2)\n\t\texit(0)\n\telif len(B) == 0:\n\t\tprint(i, 1)\n\t\texit(0)\n\telse:\n\t\tx = A.popleft()\n\t\ty = B.popleft()\n\t\tif x > y:\n\t\t\tA.append(y)\n\t\t\tA.append(x)\n\t\telse:\n\t\t\tB.append(x)\n\t\t\tB.append(y)\nprint(-1)\n", "from collections import deque\nn = int(input())\nA = [int(i) for i in input().split()][1:]\nB = [int(i) for i in input().split()][1:]\nA = deque(A)\nB = deque(B)\nres = 0\nstates = {}\nstates[tuple(A), tuple(B)] = 1\nwhile len(A) and len(B):\n\tres += 1\n\tif A[0] > B[0]:\n\t\tA.extend([B[0], A[0]])\n\telse:\n\t\tB.extend([A[0], B[0]])\n\tA.popleft()\n\tB.popleft()\n\tif (tuple(A), tuple(B)) in states:\n\t\tprint(-1)\n\t\texit()\n\tstates[tuple(A), tuple(B)] = 1\nprint(res, 1 + (len(A) == 0))\n", "n = int(input())\nk1 = list(map(int, input().split()[1:]))\nk2 = list(map(int, input().split()[1:]))\ni = 0\nwhile True:\n\tif len(k1) == 0 or len(k2) == 0:\n\t\tbreak\n\tif i > 10000:\n\t\tbreak\n\tif k1[0] > k2[0]:\n\t\tk1 = k1[1:] + k2[0:1] + k1[0:1]\n\t\tk2 = k2[1:]\n\telse:\n\t\tk2 = k2[1:] + k1[0:1] + k2[0:1]\n\t\tk1 = k1[1:]\n\ti += 1\nif i > 10000:\n\tprint(-1)\nelse:\n\twinner = 1\n\tif len(k1) == 0:\n\t\twinner = 2\n\tprint(i, winner)\n", "from collections import deque\nn = int(input())\np1 = list(map(int, input().split()))\np2 = list(map(int, input().split()))\nq1 = deque(p1[1:])\nq2 = deque(p2[1:])\ni = 0\nwhile i <= n * n + n and q1 and q2:\n\tc1 = q1.popleft()\n\tc2 = q2.popleft()\n\tif c1 > c2:\n\t\tq1.append(c2)\n\t\tq1.append(c1)\n\telse:\n\t\tq2.append(c1)\n\t\tq2.append(c2)\n\ti += 1\nif q1 and q2:\n\tprint(-1)\nelse:\n\twinner = 1\n\tif q2:\n\t\twinner = 2\n\tprint(i, winner)\n", "n = int(input())\ns1 = [int(i) for i in input().split()]\ns1.pop(0)\ns2 = [int(i) for i in input().split()]\ns2.pop(0)\nt = 0\nwhile s1 and s2:\n\tt += 1\n\ta = s1.pop(0)\n\tb = s2.pop(0)\n\tif a > b:\n\t\ts1.append(b)\n\t\ts1.append(a)\n\telse:\n\t\ts2.append(a)\n\t\ts2.append(b)\n\tif t > 1000:\n\t\tbreak\nif t > 1000:\n\tprint(-1)\nelif s1:\n\tprint(t, 1)\nelse:\n\tprint(t, 2)\n", "import copy\nn = int(input())\ns1 = list(map(int, input().split()))\ns2 = list(map(int, input().split()))\n(k1, k2) = (s1.pop(0), s2.pop(0))\ns11 = copy.deepcopy(s1)\ns22 = copy.deepcopy(s2)\ncnt = 0\nwhile True:\n\tcnt += 1\n\t(a, b) = (s11.pop(0), s22.pop(0))\n\tif a > b:\n\t\ts11.append(b)\n\t\ts11.append(a)\n\telse:\n\t\ts22.append(a)\n\t\ts22.append(b)\n\tif s11 == s1 and s22 == s2:\n\t\tprint(-1)\n\t\tbreak\n\tif len(s11) == 0:\n\t\tprint(cnt, 2)\n\t\tbreak\n\tif len(s22) == 0:\n\t\tprint(cnt, 1)\n\t\tbreak\n\tif cnt > 10000:\n\t\tprint(-1)\n\t\tbreak\n", "from collections import deque\nn = int(input())\nfirst = deque(map(int, input().split()[1:]))\nsecond = deque(map(int, input().split()[1:]))\ncnt = 0\nwhile first and second:\n\tp = first.popleft()\n\tq = second.popleft()\n\tif p > q:\n\t\tfirst.extend([q, p])\n\telse:\n\t\tsecond.extend([p, q])\n\tcnt += 1\n\tif cnt > 10 ** 3:\n\t\tprint(-1)\n\t\texit(0)\nprint(cnt, 1 if first else 2)\n", "def main():\n\tn = input()\n\tfirst = [int(x) for x in input().split()][1:]\n\tsec = [int(x) for x in input().split()][1:]\n\ttemp_first = first\n\ttemp_sec = sec\n\tcounter = 0\n\twhile True:\n\t\tif len(first) > 0 and len(sec) > 0:\n\t\t\tif counter > 99999:\n\t\t\t\treturn -1\n\t\t\telse:\n\t\t\t\tif first[0] > sec[0]:\n\t\t\t\t\tfirst = first[1:] + [sec[0], first[0]]\n\t\t\t\t\tsec = sec[1:]\n\t\t\t\telse:\n\t\t\t\t\tsec = sec[1:] + [first[0], sec[0]]\n\t\t\t\t\tfirst = first[1:]\n\t\t\t\tcounter += 1\n\t\telse:\n\t\t\treturn str(counter) + ' ' + str(2 ** (len(sec) > 0))\nprint(main())\n", "from collections import deque\nfrom math import factorial\nn = int(input())\nk = list(map(int, input().split()))\nk1 = k[0]\nq1 = deque(k[1:])\nk = list(map(int, input().split()))\nk2 = k[0]\nq2 = deque(k[1:])\nwin = 0\nmax = factorial(n)\ncur = 0\ndone = set()\nwhile cur < max:\n\tif not q1:\n\t\twin = 2\n\t\tbreak\n\tif not q2:\n\t\twin = 1\n\t\tbreak\n\ts1 = ''.join((str(i) for i in q1))\n\ts2 = ''.join((str(i) for i in q2))\n\tif s1 in done and s2 in done:\n\t\tbreak\n\tdone.add(s1)\n\tdone.add(s2)\n\tcur += 1\n\tc1 = q1.popleft()\n\tc2 = q2.popleft()\n\tif c1 > c2:\n\t\tq1.append(c2)\n\t\tq1.append(c1)\n\telse:\n\t\tq2.append(c1)\n\t\tq2.append(c2)\nif win == 0:\n\tprint(-1)\nelse:\n\tprint(cur, win)\n", "from collections import deque\np1 = p2 = deque()\nmp = dict()\nans = 0\nwinner = 0\n\ndef generate():\n\tglobal p1, p2, ans, winner\n\tif len(p1) == 0:\n\t\twinner = 2\n\t\treturn\n\telif len(p2) == 0:\n\t\twinner = 1\n\t\treturn\n\tif mp.get(str(p1)) == str(p2) or mp.get(str(p2)) == str(p1):\n\t\tans = -1\n\t\treturn\n\tmp[str(p1)] = str(p2)\n\ta = p1.popleft()\n\tb = p2.popleft()\n\tans += 1\n\tif a > b:\n\t\tp1.append(b)\n\t\tp1.append(a)\n\telse:\n\t\tp2.append(a)\n\t\tp2.append(b)\n\tgenerate()\n\ndef main():\n\tglobal p1, p2, mp, ans\n\tmp[str([-1])] = [-1]\n\tm = input()\n\tp1 = deque((int(x) for x in input().split()))\n\tnump1 = p1.popleft()\n\tp2 = deque((int(x) for x in input().split()))\n\tnump2 = p2.popleft()\n\tgenerate()\n\tif winner != 0:\n\t\tprint(str(ans) + ' ' + str(winner))\n\telse:\n\t\tprint(-1)\nmain()\n", "n = int(input())\na = list(map(int, input().split()))[1:]\nb = list(map(int, input().split()))[1:]\ncount = 0\nvisited = {tuple(a): True, tuple(b): True}\nwhile a and b:\n\tc = a.pop(0)\n\td = b.pop(0)\n\tif c > d:\n\t\ta.append(d)\n\t\ta.append(c)\n\telse:\n\t\tb.append(c)\n\t\tb.append(d)\n\tcount += 1\n\t(ta, tb) = (tuple(a), tuple(b))\n\tif visited.get(ta) == None:\n\t\tvisited[ta] = True\n\telif visited.get(tb) == None:\n\t\tvisited[tb] = True\n\telse:\n\t\tprint(-1)\n\t\tbreak\nelse:\n\tprint(count, [1, 2][len(a) == 0])\n", "n = int(input())\nf_deck = list(map(int, input().split(' ')))[1:]\ns_deck = list(map(int, input().split(' ')))[1:]\nidx = 0\nrec = []\nwhile 1:\n\trec.append(''.join(list(map(str, f_deck))) + '_' + ''.join(list(map(str, s_deck))))\n\tif f_deck[0] > s_deck[0]:\n\t\tf_deck.append(s_deck.pop(0))\n\t\tf_deck.append(f_deck.pop(0))\n\telse:\n\t\ts_deck.append(f_deck.pop(0))\n\t\ts_deck.append(s_deck.pop(0))\n\tidx += 1\n\tif ''.join(list(map(str, f_deck))) + '_' + ''.join(list(map(str, s_deck))) in rec:\n\t\tbreak\n\tif len(f_deck) == 0 or len(s_deck) == 0:\n\t\tbreak\nif len(f_deck) == 0:\n\tprint('{} {}'.format(idx, 2))\nelif len(s_deck) == 0:\n\tprint('{} {}'.format(idx, 1))\nelse:\n\tprint(-1)\n", "from collections import deque\nn = int(input())\nc1 = [int(v) for v in input().split()]\nc2 = [int(v) for v in input().split()]\np1 = deque(c1[1:])\np2 = deque(c2[1:])\nvis = set()\nfround = 0\nwhile len(p1) > 0 and len(p2) > 0 and (str(p1) + str(p2) not in vis):\n\tvis.add(str(p1) + str(p2))\n\tu = p1.popleft()\n\tv = p2.popleft()\n\tif u > v:\n\t\tp1.extend([v, u])\n\telse:\n\t\tp2.extend([u, v])\n\tfround += 1\nif len(p1) == 0 or len(p2) == 0:\n\tprint(fround, 1 if len(p2) == 0 else 2)\nelse:\n\tprint(-1)\n", "n = int(input())\np1 = list(map(int, input().split()))\np1 = p1[1:]\np2 = list(map(int, input().split()))\np2 = p2[1:]\nfor i in range(1000000):\n\tp1_card = p1.pop(0)\n\tp2_card = p2.pop(0)\n\tif p1_card > p2_card:\n\t\tp1 += [p2_card, p1_card]\n\telif p2_card > p1_card:\n\t\tp2 += [p1_card, p2_card]\n\tif len(p1) == 0:\n\t\tprint(f'{i + 1} 2')\n\t\texit()\n\telif len(p2) == 0:\n\t\tprint(f'{i + 1} 1')\n\t\texit()\nprint(-1)\n"]

difficulty: MEDIUM

source: codeforces

raw_problem:
C. Soldier and Cards

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a "war"-like card game.

The rules are following. On each turn a fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins this fight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins.

You have to calculate how many fights will happen and who will win the game, or state that game won't end.

Input

First line contains a single integer n (2 ≤ n ≤ 10), the number of cards.

Second line contains integer k1 (1 ≤ k1 ≤ n - 1), the number of the first soldier's cards. Then follow k1 integers that are the values on the first soldier's cards, from top to bottom of his stack.

Third line contains integer k2 (k1 + k2 = n), the number of the second soldier's cards. Then follow k2 integers that are the values on the second soldier's cards, from top to bottom of his stack.

All card values are different.

Output

If somebody wins in this game, print 2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2 showing which player has won.

If the game won't end and will continue forever output  - 1.

Examples

Input

4
2 1 3
2 4 2

Output

6 2

Input

3
1 2
2 1 3

Output

-1

Note

First sample:

Second sample:

question:
Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a "war"-like card game.
The rules are following. On each turn a fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins this fight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins.
You have to calculate how many fights will happen and who will win the game, or state that game won't end.

Input
First line contains a single integer n (2 ≤ n ≤ 10), the number of cards.
Second line contains integer k_{1} (1 ≤ k_{1} ≤ n - 1), the number of the first soldier's cards. Then follow k_{1} integers that are the values on the first soldier's cards, from top to bottom of his stack.
Third line contains integer k_{2} (k_{1} + k_{2} = n), the number of the second soldier's cards. Then follow k_{2} integers that are the values on the second soldier's cards, from top to bottom of his stack.
All card values are different.

Output
If somebody wins in this game, print 2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2 showing which player has won.
If the game won't end and will continue forever output  - 1.

Examples
Input
4
2 1 3
2 4 2

Output
6 2

Input
3
1 2
2 1 3

Output
-1

Note
First sample:
 ![image](1.png) Second sample:
 ![image](2.png)

tags: ['Graph traversal', 'Game theory', 'Complete search']

skill_types: ['Complete search']

raw_tags: ['brute force', 'dfs and similar', 'games']

image_tags:
Linear Data Structure
Linear Data Structure

data_split: test