Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

(n,k),p,c,*a=[[*map(int,t.split())]for t in open(0)]
while n:
 n-=1;s,x,*l=0,n;f=j=k
 while f:x=p[x]-1;s+=c[x];l+=s,;f=x!=n
 for t in l[:k]:j-=1;a+=t+j//len(l)*s*(s>0),
print(max(a))

### FIXED CODE ###

def main():
    (n,k),p,c=[[*map(int,t.split())]for t in open(0)]
    m=-10**18
    for i in range(n):
        s=i
        t=1
        x=c[s]
        while p[s]-1!=i:
            s=p[s]-1
            t+=1
            x+=c[s]
        if x>0:
            a=(k//t-1)*x
            if a>m:m=a
            s=i
            for _ in range(k%t+t):
                s=p[s]-1
                a+=c[s]
                if a>m:m=a
        else:
            a=0
            s=i
            for _ in range(min(k,t)):
                s=p[s]-1
                a+=c[s]
                if a>m:m=a
    print(m)
main()
### BUGGY CODE ###

def ac_count(s):
    cnt = 0
    for i in range(len(s)-1):
        if s[i:i+2] == 'AC':
            cnt += 1
    return cnt


N, Q = map(int, input().split())
S = input()
l = []
r = []
for i in range(Q):
    l_i, r_i = map(int, input().split())
    l.append(l_i)
    r.append(r_i)

for i in range(Q):
    print(ac_count(S[l[i]-1:r[i]]))


### FIXED CODE ###

N, Q = map(int, input().split())
S = input()

c = [0] * N

for i in range(N-1):
    if S[i:i+2] == 'AC':
        c[i+1] = c[i] + 1
    else:
        c[i+1] = c[i]

for _ in range(Q):
    l, r = map(int, input().split())
    print(c[r-1] - c[l-1])
### BUGGY CODE ###


import sys
input = sys.stdin.readline
def solve():
  n = int(input())
  l = list(int(i) for i in input().split())
  l = sorted(l,reverse = True)
  ans = 0
  for i in range(n):
    for j in range(i+1,n):
      for k in range(j+1,n):
        a = l[i]
        b = l[j]
        c = l[k]
        if a< b+c and b< c+a and c < a+b:
          ans += 1
  print(ans)
  
solve()

### FIXED CODE ###

import bisect
import sys
input = sys.stdin.readline
def solve():
  n = int(input())
  l = list(int(i) for i in input().split())
  l = sorted(l)
  ans = 0
  #print(l)
  for i in range(n):
    for j in range(i+1,n):
        a = l[i]
        b = l[j]
        c_max = bisect.bisect_left(l,a+b)
        if c_max <= j+1:
          continue
        else:
          ans += c_max-j-1
          #print(c_max-2)
        #print(c_max,i,j)

  print(ans)
  
solve()
### BUGGY CODE ###

n, m= map(int, input().split())

gates = [list(map(int, input().split())) for _ in range(m)]
count=0
for i in range(1, n+1):
    tf = True
    for j in gates:
        if j[0] > i or j[1] < i:
            tf = False
            break
        
    if tf:
        count+=1

print(count)


### FIXED CODE ###

n, m= map(int, input().split())

gates = [list(map(int, input().split())) for _ in range(m)]
up = min([i[1] for i in gates])
down = max([i[0] for i in gates])


if up >= down:
    print(up-down+1)
else:
    print(0)
### BUGGY CODE ###

n=int(input())
a=list(map(int, input().split()))
b=sorted(a)
c=0
d=str(a.index(b[0])+1)
for i in range(1,n):
    c=a.index(b[i])+1
    d=d+" " + str(c)
print(d)

### FIXED CODE ###

n=int(input())
a=list(map(int, input().split()))
b=sorted(a)
c=[0]*n
for i in range(n):
    c[a[i]-1] = i+1
print(' '.join(map(str, c)))
### BUGGY CODE ###

n, x, y = map(int, input().split())


def distance(i,j):
    d1 = abs(i-j)
    d2 = abs(i-x)+1+abs(j-y)
    d3 = abs(i-y)+1+abs(j-x)    
    d = min(d1,d2,d3)
    return d

d = [0]*(n-1)
for i in range(1, n+1):
    for j in range(i+1,n+1):
        d[distance(i,j)-1] = d[distance(i,j)-1] + 1

for i in d:
    print(i)

### FIXED CODE ###


n, x, y = map(int, input().split())

d = [0]*(n-1)
for i in range(1, n+1):
    for j in range(i+1,n+1):
        c = min(j-i, abs(i-x)+1+abs(j-y))
        d[c-1] = d[c-1] + 1

for i in d:
    print(i)
### BUGGY CODE ###

n, m = [int(v) for v in input().split(' ')]

costs = []
for i in range(n):
    costs.append([0] * i)

for i in range(m):
    l, r, c = [int(v) for v in input().split(' ')]

    for j in range(l, r):
        for k in range(j + 1, r + 1):
            c0 = costs[k - 1][j - 1]
            if c0 == 0 or c0 > c:
                costs[k - 1][j - 1] = c

import heapq
h = []
closed = 0

heapq.heappush(h, (0, n-1))

while len(h) > 0:
    c0, n1 = heapq.heappop(h)
    closed |= (1 << n1)

    if n1 == 0:
        print(c0)
        exit()

    for i in range(n):
        if i == n1:
            continue
        if closed & (1 << i) != 0:
            continue

        c1 = 0
        if i < n1:
            c1 = costs[n1][i]
        elif i > n1:
            c1 = costs[i][n1]
        if c1 > 0:
            heapq.heappush(h, (c0 + c1, i))

print("-1")

### FIXED CODE ###

import heapq

n, m = [int(v) for v in input().split(' ')]

links = []
for i in range(n):
    links.append([])

for i in range(m):
    l, r, c = [int(v) for v in input().split(' ')]
    l -= 1
    r -= 1
    links[r].append((l, c))

h = []
heapq.heappush(h, (0, n-1))
closed = 0
while len(h) > 0:
    c0, n0 = heapq.heappop(h)
    if (closed & (1 << n0)) != 0:
        continue
    closed |= (1 << n0)
    if n0 == 0:
        print(c0)
        exit()

    for n1, c1 in links[n0]:
        if (closed & (1 << n1)) != 0:
            continue
        heapq.heappush(h, (c0 + c1, n1))

    n2 = n0 + 1
    if n2 < n:
        if (closed & (1 << n2)) == 0:
            heapq.heappush(h, (c0, n2))

print("-1")
### BUGGY CODE ###

n,a=[int(_) for _ in input().split()]
x=[int(_) for _ in input().split()]
dp=[[[0 for k in range(2551)] for j in range(51)] for i in range(n)]
dp[0][1][x[0]]=1;dp[0][0][0]=1
for i in range(n-1):
    for j in range(50):
        for k in range(2501):
            dp[i+1][j+1][k+x[i+1]] += dp[i][j][k]
            dp[i+1][j][k] += dp[i][j][k]
ans=0
for j in range(1,n+1):ans += dp[-1][j][j*a]
print(ans)

### FIXED CODE ###

n,a=[int(_) for _ in input().split()]
x=[int(_) for _ in input().split()]
dp=[[[0 for k in range(2551)] for j in range(51)] for i in range(n)]
dp[0][1][x[0]]=1;dp[0][0][0]=1
for i in range(n-1):
    for j in range(50):
        for k in range(2501):
            dp[i+1][j+1][k+x[i+1]] += dp[i][j][k]
            dp[i+1][j][k] += dp[i][j][k]
ans=0
for j in range(1,n+1):ans += dp[-1][j][j*a]
print(ans)
### BUGGY CODE ###

n,m=map(int,input().split())
a=[int(input()) for i in range(m)]
dp=[0]*(n+1)
dp[0]=1

if 1 in a:
  dp[1]=0
else:
  dp[1]=1

for i in range(1,n):
  if i+1 not in a:
    
    dp[i+1]=(dp[i]+dp[i-1])%(10**9+7)
  else:
    dp[i+1]=0
    
print(dp[n]%(10**9+7))


### FIXED CODE ###

N,M = map(int,input().split())
a = set(int(input()) for x in range(M))
A = [0]*(N+1)
mod = 1000000007
A[0] = 1
if not(1 in a):
  A[1] = 1
for i in range(2,N+1):
    if i in a:
      A[i] = 0
    else:
      A[i] = (A[i-2] + A[i-1]) % mod
print(A[N])
### BUGGY CODE ###

n = int(input())
a = [int(input()) for i in range(n)]

for i in range(n):
  b = a[:]
  del b[i]
  print(max(b))

### FIXED CODE ###

from heapq import nlargest
import sys
n = int(input())
a = [int(input()) for i in range(n)]
l1, l2 = nlargest(2, a)

if l1 == l2:
  for i in range(n):
    print(l1)
  sys.exit()

for i in a:
  if i == l1:
    print(l2)
  else:
    print(l1)
### BUGGY CODE ###

s = str(input())

l = len(s)
for st in ['01', '10']:
    while st in s:
        s = s.replace('01', '').replace('10', '')

print(l - len(s))


### FIXED CODE ###

s = str(input())
z = sum([str(x)=='0' for x in s])
o = sum([str(x)=='1' for x in s])
print(2*min(z, o))
### BUGGY CODE ###

def resolve():
    n=int(input())
    a=list(map(int,input().split()))
    ans=1
    for x in a:
        ans=ans*x
    if ans>10**18:
        print(-1)
    else:
        print(ans)
resolve()

### FIXED CODE ###

def resolve():
    n=int(input())
    a=list(map(int,input().split()))
    ans=1
    if 0 in a:
        ans=0
    else:
        for x in a:
            ans=ans*x
            if ans>10**18:
                ans=-1
                break
    print(ans)
resolve()
### BUGGY CODE ###

N,P=map(int,input().split())
S=input()

answer=0
for i in range(N):
  for j in range(i,N):
    si=int(S[i:j+1])
    if si%P==0:
      answer+=1
      
print(answer)
    

### FIXED CODE ###

N,P=map(int,input().split())
S=input()

answer=0
if P==2 or P==5:
  for i in range(N):
    si=int(S[i])
    if si%P==0:
      answer+=i+1
else:
  mod_table=[0]*P
  mod_table[0]=1
  revS=S[::-1]
  
  modp=0
  mod10=1
  for i in range(N):
    mod10=(mod10*10)%P
    #modp=(10*modp+int(revS[i]))%P
    modp=(modp+mod10*int(revS[i]))%P
    mod_table[modp]+=1
    
  #print(mod_table)
  for i in range(P):
    answer+=mod_table[i]*(mod_table[i]-1)//2
  
print(answer)
### BUGGY CODE ###

n = int(input())

A = list(map(int,input().split()))	

#print(A)

flag = 0
b = 1

for a in A:
  if a == 0:
    flag = 2

if flag == 0:
  for a in A:
    b = b * a

if(b > 10**18):
  flag = 1
  
  
if flag == 1:
  print(-1)
elif flag == 0:
  print(b)
else:
  print(0)


### FIXED CODE ###

n = int(input())

A = list(map(int,input().split()))	

#print(A)

flag = 0
b = 1

for a in A:
  if a == 0:
    flag = 2

if flag == 0:
  for a in A:
    b = b * a
    if(b > 10**18):
      flag = 1
      break

#if(b > 10**18):
#  flag = 1
  
  
if flag == 1:
  print(-1)
elif flag == 0:
  print(b)
else:
  print(0)
### BUGGY CODE ###

import sys
from collections import deque
sys.setrecursionlimit(10**7)

n, m = [int(i) for i in sys.stdin.readline().split()]
parent = [i for i in range(n)]

graph = {j:[] for j in range(n)}
for i in range(m):
    l, r, d = [int(i) for i in sys.stdin.readline().split()]
    l -= 1
    r -= 1
    graph[l].append((r, d))
    graph[r].append((l, -d))

yet = set(list(range(n)))
x_ls = [None for i in range(n)]
x_ls[0] = 0
que = deque([0])
flg = True
while len(que) > 0:
    cur = que.popleft()
    if cur in yet:
        yet.remove(cur)
    cur_pos = x_ls[cur]
    for _next, dis in graph[cur]:
        if x_ls[_next] is None:
            x_ls[_next] = cur_pos + dis
            que.append(_next)
        else:
            if x_ls[_next] != cur_pos + dis:
                flg = False
                break
    if not flg:
        break
    if len(que) == 0 and len(yet) > 0:
        root = list(yet)[0]
        que.append(root)
        x_ls[root] = 0
        yet.remove(root)
if flg:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)

n, m = [int(i) for i in sys.stdin.readline().split()]
parent = [i for i in range(n)]

graph = {j:[] for j in range(n)}
for i in range(m):
    l, r, d = [int(i) for i in sys.stdin.readline().split()]
    l -= 1
    r -= 1
    graph[l].append((r, d))
    graph[r].append((l, -d))

already = set()
x_ls = [None for i in range(n)]
x_ls[0] = 0
stack = list(range(n))[::-1]
flg = True
while len(stack) > 0:
    cur = stack.pop(-1)
    if cur in already:
        continue
    if x_ls[cur] is None:
        cur_pos = 0
    else:
        cur_pos = x_ls[cur]
    already.add(cur)
    for _next, dis in graph[cur]:
        if x_ls[_next] is None:
            x_ls[_next] = cur_pos + dis
            stack.append(_next)
        else:
            if x_ls[_next] != cur_pos + dis:
                flg = False
                break
    if not flg:
        break
if flg:
    print("Yes")
else:
    print("No")
### BUGGY CODE ###

h,w,k=map(int,input().split())
S=[]
for i in range(h):
    s=int(input(),2)
    S.append(s)
ans=100000
for i in range(2**(h-1)):
    now=0 #区切った数
    cnt=0 #1の数
    y=0
    for j in range(h-1):
        if (i>>j)&1:
            y+=1
    C=[0]*(y+1) #1の数保存配列
    for x in range(w):
        T=[] #現在の列での1の数保存配列
        for j in range(h):      
            cnt+=(S[j]>>x)&1
            if (i>>j)&1: #横の区切り判定
                T.append(cnt)
                cnt=0
            if j==h-1: #最後の列
                T.append(cnt)
                cnt=0
        for j in range(len(C)):
            if C[j]+T[j]>k:
                now+=1
                C=T
                break
            else:
                C[j]+=T[j]
    ans=min(ans,now+y)
print(ans)

### FIXED CODE ###

h,w,k=map(int,input().split())
S=[]
for i in range(h):
    s=int(input(),2)
    S.append(s)
ans=h+w-2
for i in range(2**(h-1)):
    flag=False
    now=0 #区切った数
    cnt=0 #1の数
    y=0
    for j in range(h-1):
        if (i>>j)&1:
            y+=1
    C=[0]*(y+1) #1の数保存配列
    for x in range(w):
        T=[] #現在の列での1の数保存配列
        for j in range(h):      
            cnt+=(S[j]>>x)&1
            if cnt>k:
                flag=True
                break
            if (i>>j)&1: #横の区切り判定
                T.append(cnt)
                cnt=0
            if j==h-1: #最後の列
                T.append(cnt)
                cnt=0
        if flag:
            break
        for j in range(len(C)): #それぞれのブロックに対して
            if C[j]+T[j]>k:
                now+=1
                C=T
                break
            else:
                C[j]+=T[j]
    if flag:
        continue
    ans=min(ans,now+y)
print(ans)
### BUGGY CODE ###

from bisect import bisect_left
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC = sorted(BC,key= lambda x: x[1],reverse=True)
A.sort()
for i in range(M):
    if BC[i][1]<A[0]:
        break
    B= BC[i][0]
    C = BC[i][1]
    b = bisect_left(A,C)
    if B>=b:
        A.extend([C for _ in range(b)])
        del A[:b]
    else:
        A.extend([C for _ in range(B)])
        del A[:B]
print(sum(A))

### FIXED CODE ###

N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC = sorted(BC,key= lambda x: x[1],reverse=True)
A.sort()
n = 0
i = 0
m = 0
while A[i]<BC[n][1]:
    A[i] = BC[n][1]
    m = m+1
    if m >=BC[n][0]:
        m = 0
        n = n+1
    i += 1
    if n==M:
      break
    if i == N:
      break
print(sum(A))
### BUGGY CODE ###

n,x = map(int,input().split())
a = list(map(int,input().split()))
MIN = sum(a)
tori = a + [0]
tori.pop()
for i in range(1,n):
  for j in range(n):
    ido = i+j
    if ido >= n:
      ido-=n
    tori[j] = min(a[ido],tori[j])
  MIN = min(sum(tori) + i*x,MIN)
print(MIN)

### FIXED CODE ###

n,x = map(int,input().split())
a = list(map(int,input().split()))
MIN = sum(a)
ans = MIN+0
tori = a + [0]
tori.pop()
for i in range(1,n):
  for j in range(n):
    ido = i+j
    if ido >= n:
      ido-=n
    if tori[j] > a[ido]:
      ans -= (tori[j] - a[ido])
      tori[j] = a[ido]
  MIN = min(ans + i*x,MIN)
print(MIN)
### BUGGY CODE ###

N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
seen = set()
circuits = []

for x in range(N):
    cir = []
    point = 0
    while True:
        if x in seen: 
            if cir != []: circuits.append((cir,point))
            break
        seen.add(x)
        point += C[x]
        cir.append(x)
        x = P[x] - 1
ans = -1000000000000
for x in circuits:
    tmp = 0
    if x[1] > 0 and K >= len(x[0]) * 2: 
        tmp = (K // len(x[0]) - 1) * x[1]
        r = K % len(x[0]) + len(x[0])
    else:
        r = K
    for y in x[0]:
        tmp2 = 0
        for _ in range(r):
            y = P[y] - 1
            tmp2 += C[y]
            ans = max(ans, tmp + tmp2)
print(ans)



### FIXED CODE ###

N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
seen = set()
circuits = []

for x in range(N):
    cir = []
    point = 0
    while True:
        if x in seen: 
            if cir != []: circuits.append((cir,point))
            break
        seen.add(x)
        point += C[x]
        cir.append(x)
        x = P[x] - 1
ans = -1000000000000
for x in circuits:
    tmp = 0
    if  K >= len(x[0]) * 2: 
        if x[1] > 0: tmp = (K // len(x[0]) - 1) * x[1]
        r = K % len(x[0]) + len(x[0])
    else:
        r = K
    for y in x[0]:
        tmp2 = 0
        for _ in range(r):
            y = P[y] - 1
            tmp2 += C[y]
            ans = max(ans, tmp + tmp2)
print(ans)
### BUGGY CODE ###

N, S = map(int, input().split())
A = map(int, input().split())
 
def solve(length, target, nums):
  
  dp = [[0]*(target+1) for i in range(length+1)]
  dp[0][0] = 1
  for i, n in enumerate(nums):
    for j, comb_num in enumerate(dp[i]):
      sub = j - n
      dp[i + 1][j] += comb_num * 2
      dp[i + 1][j] %= 998244353
      #print(i, j, sub, dp[i])
      if sub >= 0:
        dp[i + 1][j] += dp[i][sub]
        dp[i + 1][j] %= 998244353
  #print(dp)
  print(dp[length][target])
  
solve(N, S, A)

### FIXED CODE ###

N, S = map(int, input().split())
A = map(int, input().split())
 
def solve(length, target, nums):
  
  dp = [[0]*(target+1) for i in range(length+1)]
  dp[0][0] = 1
  for i, n in enumerate(nums):
    for j, comb_num in enumerate(dp[i]):
      sub = j - n
      dp[i + 1][j] += comb_num * 2
      #print(i, j, sub, dp[i])
      if sub >= 0:
        dp[i + 1][j] += dp[i][sub]
      dp[i + 1][j] %= 998244353
  #print(dp)
  print(dp[length][target])
  
solve(N, S, A)
### BUGGY CODE ###

lim = 1e18
n = int(input());
nums = list(map(int,str(input()).split(" ")))
ans = 1
for num in nums:
    if num == 0:
        print(0)
        quit()
    ans *= num
if ans > lim:
    print(-1)
else:
    print(int(ans))




### FIXED CODE ###

def main():
    lim = 1e18
    n = int(input())
    nums = list(map(int,str(input()).split(" ")))
    if 0 in nums:
        print(0)
        return
    ans = 1
    for num in nums:
        ans *= num
        if ans > lim:
            print(-1)
            return
    print(ans)
    return

main()
### BUGGY CODE ###

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.par = [-1]*n 
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)      
        if x==y:
            return        
        if self.par[x] > self.par[y]:
            x, y = y, x       
        self.par[x] += self.par[y]
        self.par[y] = x
    def members(self, x):
        root = self.find(x)
        return set([i for i in range(self.n) if self.find(i) == root])

N, K, L = map(int, input().split())
Uni_1 = UnionFind(N)
Uni_2 = UnionFind(N)
for _ in range(K):
  p,q = map(int, input().split())
  Uni_1.union(p-1, q-1)
for _ in range(L):
  p,q = map(int, input().split())
  Uni_2.union(p-1, q-1)
for i in range(N):
  print(len(Uni_1.members(i)&Uni_2.members(i)), end=" ")

### FIXED CODE ###

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.par = [-1]*n 
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)      
        if x==y:
            return        
        if self.par[x] > self.par[y]:
            x, y = y, x       
        self.par[x] += self.par[y]
        self.par[y] = x

N, K, L = map(int, input().split())
Uni_1 = UnionFind(N)
Uni_2 = UnionFind(N)
D = {}
for _ in range(K):
  p,q = map(int, input().split())
  Uni_1.union(p-1, q-1)
for _ in range(L):
  p,q = map(int, input().split())
  Uni_2.union(p-1, q-1)
for i in range(N):
  k = (Uni_1.find(i), Uni_2.find(i))
  if k in D.keys():
    D[k] += 1
  else:
    D[k] = 1
for i in range(N):
  print(D[(Uni_1.find(i), Uni_2.find(i))], end=" ")
### BUGGY CODE ###

n = int(input())
a = []
for i in range(1,n+1):
    a.append(int(input()))
    
mx = max(a)
for j in range(1,n+1):
    if a[j-1] == mx:
        print(max(a[0:j-1]+a[j:n]))
    else:
        print(mx)


### FIXED CODE ###

n = int(input())
a = []
mx12 = [0,0]
for i in range(1,n+1):
    a.append(int(input()))
    if min(mx12) <= a[-1]:
        mx12.sort()
        mx12[0] = mx12[1]
        mx12[1] = a[-1]
mx12.sort()
for j in range(1,n+1):
    if a[j-1] == mx12[1]:
        print(mx12[0])
    else:
        print(mx12[1])
### BUGGY CODE ###

def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    return a * b // gcd(a, b)


n = int(input())
a = list(map(int, input().split(" ")))
alcm = a[0]
for i in a:
    alcm = lcm(alcm, i)

tot = 0
for num in a:
    tot += alcm // num

print(tot % 1000000007)


### FIXED CODE ###

def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a % b)

def lcm(a,b):
  return a * b // gcd(a,b)

n = int(input())
av = list(map(int, input().split(' ')))
alcm = 1
for i in av:
  alcm = lcm(alcm, i)

tot = 0
for i in range(n):
  tot += alcm // av[i]

print(tot % 1000000007)
### BUGGY CODE ###

#B
N = int(input())
A = input().split()
multi= 1
for i in range(N):
    A[i] = int(A[i])
for i in range(N):
    multi *= A[i]
if multi > 10**18:
        print(-1)
else:
    print(multi)

### FIXED CODE ###

N = int(input())
A = input().split()
multi= 1
if('0' in A):
    print(0)
else:
    for i in range(N):
        multi *= int(A[i])
        if multi > 10**18:
            print(-1)
            break
    else:
        print(multi)
### BUGGY CODE ###

import sys
if sys.version_info.minor < 5:
    from fractions import gcd
else:
    from math import gcd
import math

A, B = map(int, input().split())
g = gcd(A, B)

ans = [1]
i= 2
while g >= i:
    # print("i: {}".format(i))

    if g % i == 0:
        # print("This is a cd of A and B.")
        ans.append(i)

    while g % i == 0:
        g /= i

    i += 1

# print(ans)

print(len(ans))


### FIXED CODE ###

import sys
if sys.version_info.minor < 5:
    from fractions import gcd
else:
    from math import gcd
import math

DEBUG = False

A, B = map(int, input().split())
g = gcd(A, B)
if DEBUG:
    print("gcd(A, B): {}".format(g))

ans = [1]
i= 2
temp = g
while i <= int(-(-g**0.5//1))+1:
    if DEBUG:
        print("i: {}, g: {}".format(i, g))

    if temp % i == 0:
        if DEBUG:
            print("This is a cd of A and B.")
        ans.append(i)

    while temp % i == 0:
        temp //= i

    i += 1

if temp != 1:
    ans.append(temp)

if DEBUG:
    print(ans)

print(len(ans))
### BUGGY CODE ###

def try_int(s):
    try:
        return int(s)
    except:
        return s

s = input()
nq = int(input())
query = []
for i in range(nq):
    query.append([try_int(i) for i in input().split()])
    
for q in query:
    if len(q) == 1:
        s = s[::-1]
    else:
        if q[1] == 1:
            s = q[2]+s
        else:
            s = s+q[2]
print(s)

### FIXED CODE ###

from sys import stdin
 
def main():
    S = list(stdin.readline().rstrip())
    Q = int(input())
    rev = 0
    head = []
    tail = []
 
    for _ in range(Q):
        q = stdin.readline()
        if q[0] == "1":
            rev = (rev+1)%2
        else:
            if q[2] == "1":
                if rev == 0:
                    head.append(q[4])
                else:
                    tail.append(q[4])
            else:
                if rev ==0:
                    tail.append(q[4])
                else:
                    head.append(q[4])
 
    if rev == 0:
        ans = "".join(head[::-1] + S + tail)
    else:
        ans = "".join(tail[::-1] + S[::-1] + head)
    
    print(ans)
    
main()
### BUGGY CODE ###

N,M=map(int,input().split())
x=set([int(x) for x in range(1,N+1)])
for i in range(M):
    L,R=map(int,input().split())
    a=set([int(x) for x in range(L,R+1)])
    x=a & x
print(len(list(x)))

### FIXED CODE ###

N,M=map(int,input().split())
l=[]
r=[]
for i in range(M):
    L,R=map(int,input().split())
    l.append(L)
    r.append(R)
a=max(l)
b=min(r)
x=list(range(a,b+1))
print(len(x))
### BUGGY CODE ###

N = int(input())
buttons = [int(input()) for i in range(N)]
visited = [1]

cur = 1
count = 0
for i in range(N):
    cur = buttons[cur-1]
    count += 1

    if cur in visited:
        print(-1)
        exit()

    visited.append(cur)

    if cur == 2:
        print(count)
        exit()

print(-1)

### FIXED CODE ###

N = int(input())
buttons = [int(input()) for i in range(N)]

cur = 1
for i in range(N):
    cur = buttons[cur-1]

    if cur == 2:
        print(i+1)
        exit()

print(-1)