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

n, a = map(int, input().split())
x = list(map(int, input().split()))

# import numpy as np
# dp = np.full((n + 1, n + 1, n * max(x + [a]) + 1), None).tolist()
#
def full(shape, value):
    if not shape:
        return value
    return [full(shape[1:], value) for _ in range(shape[0])]
dp = full((n + 1, n + 1, n * max(x + [a]) + 1), None)

def dfs(j=n, k=n, s=n * max(x)):
    if dp[j][k][s] is not None:
        return dp[j][k][s]
    if j == k == s == 0:
        dp[0][0][0] = 1
        return 1
    result = 0
    if j >= 1:
        if x[j - 1] > s:
            result = dfs(j - 1, k, s)
        elif k >= 1:
            result = dfs(j - 1, k, s) + dfs(j - 1, k - 1, s - x[j - 1])
    dp[j][k][s] = result
    return result

print(sum(dfs(n, k, k * a) for k in range(1, n + 1)))


### FIXED CODE ###

n, a = map(int, input().split())
x = list(map(int, input().split()))

for i in range(n):
    x[i] -= a


def full(shape, value):
    if not shape:
        return value
    return [full(shape[1:], value) for _ in range(shape[0])]

minus_cnt = 0
plus_cnt = 0
for v in x:
    if v < 0:
        minus_cnt -= v
    elif v > 0:
        plus_cnt += v

dp = full((n + 1, minus_cnt + plus_cnt + 5, n + 1), None)
def dfs(k, s=0, depth=n - 1): # ret -> pattern count
    if dp[k][s][depth] is not None:
        return dp[k][s][depth]
    if k == 0:
        if s == 0:
            result = 1
        else:
            result = 0
        dp[k][s][depth] = result
        return result
    if depth == -1:
        dp[k][s][depth] = 0
        return 0
    result = dfs(k - 1, s + x[depth], depth - 1) + dfs(k, s, depth - 1)
    dp[k][s][depth] = result
    return result

result = 0
for i in range(1, n + 1):
    result += dfs(i)
print(result)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

INF = 10**12
N,M = map(int,input().split())
K = []
for i in range(M) :
    a,b = map(int,input().split())
    s = 0 #setをbitで管理.
    cx = list(map(int,input().split()))
    for c in cx :
        s = s | (1 << (c-1)) #setの和集合
    K.append([s, a])

#dp[bit]: 集合=bit のときのコストの最小値
dp = [INF]*(1 << N)
dp[0] = 0

for bit in range(1 << N) :
    for i in range(M) :
        s,a = K[i]
        t = bit | s
        cost = dp[bit] + a
        dp[t] = min(dp[t], cost)
ans = -1 if dp[-1] == INF else dp[-1]

print(ans)


### FIXED CODE ###

#Thanks, snuke!

N,M = map(int,input().split())
key = []
for i in range(M) :
    a,b = map(int,input().split())
    s = 0
    cx = list(map(int,input().split()))
    for j in range(b) :
        s = s | (1 << (cx[j]-1))
    key.append([s,a])

dp = [10**12 for i in range(1 << N)]
dp[0] = 0
for s in range(1 << N) :
    for i in range(M) :
        t = s | key[i][0]
        cost = dp[s] + key[i][1]
        dp[t] = min(dp[t], cost)

ans = dp[-1] if dp[-1] != 10**12 else -1

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

#! -*- coding:utf-8 -*-
n = int(input())
a = map(int,input().split())
a = list(a)

b = ''
for i in range(n):
    b += str(a[i])
    b = b[::-1]

if n == 1:
    print(a[0])
else:
    for i in range(len(b)-1):
        print(int(b[i]),end = ' ')
    print(b[-1])


### FIXED CODE ###

n = int(input())
arr = input().split()
arr = list(map(int,arr))

b = [0] * n
center = n // 2
for i in range(n):
    if i % 2 == 0:
        b[center + (i//2)] = arr[i]
    else:
        b[center - ((i+1)//2)] = arr[i]

if n % 2 == 0:
    pass
else:
    b.reverse()

for i in range(n-1):
    print(b[i], end = ' ')
print(b[-1])
### BUGGY CODE ###


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def main():
    h,w=map(int,input().split())
    s=[input() for _ in range(h)]

    u=UnionFind(h*w)

    for i in range(h):
        for j in range(w):
            if j !=0:
                if s[i][j-1] != s[i][j]:
                    u.union(i*w+j-1,i*w+j)
            if i !=0:
                if s[i-1][j] != s[i][j]:
                    u.union((i-1)*w+j,i*w+j)
    res = 0
    for i in range(h*w):
        for j in range(i,h*w):
            if s[i//w][i%w] != s[j//w][j%w]:
                if u.same(i,j):
                    res += 1
    print(res)

main()

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)

h,w=map(int,input().split())
s=[input() for _ in range(h)]
flag = [[0]*w for _ in range(h)]

def dfs(th,tw):
    global flag,h,w,s
    flag[th][tw] = True
    tblack = 0
    twhite = 0
    if s[th][tw] == "#":
        tblack += 1
    else:
        twhite += 1 
    for x,y in ([0,1],[0,-1],[1,0],[-1,0]):
        nth = th + x
        ntw = tw + y
        if nth >= 0 and nth < h and ntw >= 0 and ntw < w:
            if not flag[nth][ntw] and s[th][tw] != s[nth][ntw]:
                ttb,ttw = dfs(nth,ntw)
                tblack += ttb
                twhite += ttw
    return [tblack,twhite]
                
def main():
    global flag,h,w,s
    res = 0
    for i in range(h):
        for j in range(w):
            if not flag[i][j]:
                black,white = dfs(i,j)
                res += black * white 

    return res
print(main())
### BUGGY CODE ###

N = int(input())
a_list = list(map(int, input().split()))

for i in range(1, N+1):
    print(a_list.count(i))



### FIXED CODE ###

N = int(input())
a_list = list(map(int, input().split()))
result_list = [0] * N

for a in a_list:
    result_list[a-1] += 1

result_list_str = [str(i) for i in result_list]
print('\n'.join(result_list_str))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
def lcs_sol(x, y):
    n = len(x)
    m = len(y)
    dp = [0 for j in range(m + 1)]

    for i in range(1, n + 1):
        dp_prev = dp[:]
        for j in range(1, m + 1):
            if x[i - 1] == y[j - 1]:
                dp[j] = dp_prev[j - 1] + 1
            elif dp_prev[j] < dp[j - 1]:
                dp[j] = dp[j - 1]
            else:
                dp[j] = dp_prev[j]

    return dp[m]


if __name__ == '__main__':
    import sys
    q = int(sys.stdin.readline().strip())
    for i in range(q):
        x = sys.stdin.readline().strip()
        y = sys.stdin.readline().strip()
        print(lcs_sol(x, y))



### FIXED CODE ###

def m():
 e=input
 a=''
 for _ in[0]*int(e()):
  X,z=e(),[]
  for y in e():
   s=i=0
   for k in z:
    t=X.find(y,s)+1
    if t<1:break
    if t<k:z[i]=t
    s=k;i+=1
   else:
    t=X.find(y,s)+1
    if t:z+=[t]
  a+=f'\n{len(z)}'
 print(a[1:])
if'__main__'==__name__:m()
### BUGGY CODE ###

import numpy as np

n = int(input())
A, B = np.zeros(n), np.zeros(n)
for i in range(n):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b

ans = 0

for i in range(n-1, -1, -1):
    if not A[i] % B[i] == 0:
        temp = B[i]*((A[i]//B[i])+1) - A[i]
        ans += temp
        A += temp
print(int(ans))

### FIXED CODE ###

n = int(input())
A, B = [], []
for _ in range(n):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
ans = 0
for i in range(n-1, -1, -1):
    if (A[i]+ans) % B[i] == 0:
        continue
    else:
        ans += B[i] - (A[i]+ans) % B[i]
print(ans)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True
debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def solve():
    N, K = LI()
    a_list = LI()
    f_list = LI()

    # 答えで二分探索

    low = 0
    high = (10**6)*(10**6)*2*(10**5)

    # 妥当な割り当て
    a_list = sorted(a_list)
    f_list = sorted(f_list, reverse=True)
    import math

    dprint(a_list)
    dprint(f_list)

    def judge(ans):
        # ans を答えにできるか
        cnt = 0
        for i in range(N):
            a = a_list[i]
            f = f_list[i]
            if a*f > ans:
                # 差分を訓練で埋める
                ki = math.ceil((a*f - ans) / f)
                cnt += ki
                if cnt > K:
                    dprint('False', ans, cnt)
                    return False
        dprint('True', ans, cnt)
        return True

    while low < high:
        mid = math.floor((low + high) / 2)

        guess = judge(mid)
        # dprint(high, low, mid, guess)

        if guess == True:
            # できるので、これより1少ないのが最大
            high = mid
        else:
            # できないので、これより大きい
            low = mid + 1
        # dprint(high, low)
    print(low)


solve()

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True
debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def solve():
    N, K = LI()
    a_list = LI()
    f_list = LI()

    # 答えで二分探索

    low = 0
    maxa = max(a_list)
    maxf = max(f_list)
    high = (maxa * maxf * N) + 1

    # 妥当な割り当て
    a_list = sorted(a_list)
    f_list = sorted(f_list, reverse=True)
    import math

    dprint(a_list)
    dprint(f_list)

    def judge(ans):
        # ans を答えにできるか
        cnt = 0
        for i in range(N):
            a = a_list[i]
            f = f_list[i]
            if a*f > ans:
                # 差分を訓練で埋める
                ki = math.ceil((a*f - ans) / f)
                cnt += ki
                if cnt > K:
                    dprint('False', ans, cnt)
                    return False
        dprint('True', ans, cnt)
        return True

    while low < high:
        mid = math.floor((low + high) / 2)

        guess = judge(mid)
        # dprint(high, low, mid, guess)

        if guess == True:
            # できるので、これより1少ないのが最大
            high = mid
        else:
            # できないので、これより大きい
            low = mid + 1
        # dprint(high, low)
    print(low)


solve()
### BUGGY CODE ###

import numpy as np

n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]


def match2int(x, y):
    x, y = min(x, y), max(x, y)
    return (2 * n - 1 - x) * x // 2 + y - x - 1


match_sm = n * (n - 1) // 2

adj = [[] for _ in range(match_sm)]
in_deg = np.zeros(match_sm, dtype=np.int16)
for i, li in enumerate(a):
    for j, k in zip(li, li[1:]):
        j -= 1
        k -= 1
        match1 = match2int(i, j)
        match2 = match2int(i, k)
        adj[match1].append(match2)
        in_deg[match2] += 1

cnd = np.array([i for i, e in enumerate(in_deg) if e == 0], dtype=np.int32)
day = 0
while cnd.size:
    in_deg[cnd] -= 1
    for u in cnd:
        in_deg[adj[u]] -= 1

    cnd = np.where(in_deg == 0)[0]
    day += 1

if np.any(in_deg >= 1):
    day = -1

print(day)


### FIXED CODE ###

n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]


def match2int(x, y):
    x, y = min(x, y), max(x, y)
    return (2 * n - 1 - x) * x // 2 + y - x - 1


match_sm = n * (n - 1) // 2

adj = [[] for _ in range(match_sm)]
in_deg = [0] * match_sm
for i, li in enumerate(a):
    for j, k in zip(li, li[1:]):
        j -= 1
        k -= 1
        match1 = match2int(i, j)
        match2 = match2int(i, k)
        adj[match1].append(match2)
        in_deg[match2] += 1

cnd = [i for i, e in enumerate(in_deg) if e == 0]
day = 0
while cnd:
    nxt_cnd = []
    for u in cnd:
        for v in adj[u]:
            in_deg[v] -= 1
            if in_deg[v] == 0:
                nxt_cnd.append(v)

    cnd = nxt_cnd
    day += 1

if in_deg.count(0) != match_sm:
    day = -1

print(day)
### BUGGY CODE ###

N=int(input())
L=list(map(int, input().split()))
S=0
for i in range(N):
    for j in range(i+1,N):
        for k in range(j+1,N):
            if L[i]+L[j]>L[k] and L[j]+L[k]>L[i] and L[k]+L[i]>L[j]:
                S+=1
print(S)

### FIXED CODE ###

import bisect

N=int(input())
L=sorted(list(map(int, input().split())))
S=0

for i in range(N):
    for j in range(i+1,N):
        S+=bisect.bisect_left(L,L[i]+L[j])-j-1
print(S)
### BUGGY CODE ###

import itertools
from collections import defaultdict
N,M,Q=map(int,input().split())
d=defaultdict(int)
for i in range(M):
  a,b=map(int,input().split())
  d[(a,b)]+=1
c={}
for x,y in itertools.product(range(0,N+1),repeat=2):
  c[(x,y)]=sum(d[(x,j)] for j in range(1,y+1))
for i in range(Q):
  x,y=map(int,input().split())
  print(sum(c[(j,y)]-c[j,x-1] for j in range(x,y+1)))

### FIXED CODE ###

N,M,Q=map(int,input().split())
c=[[0 for i in range(N+1)] for j in range(N+1)]
for i in range(M):
  a,b=map(int,input().split())
  c[a][b]+=1
for i in range(1,N):
  for j in range(1,N+1-i):
    c[j][i+j]+=c[j+1][i+j]+c[j][i+j-1]-c[j+1][i+j-1]
for i in range(Q):
  a,b=map(int,input().split())
  print(c[a][b])
### BUGGY CODE ###

N = int(input())
s = [input() for k in range(N)]
s = [''.join(sorted(list(s_element))) for s_element in s]
memory = {}
result = 0
for s_element in s:
    if s_element not in list(memory.keys()):
        memory[s_element] = 1
    else:
        result += memory[s_element]
        memory[s_element] += 1
print(result)

### FIXED CODE ###

N = int(input())
s = [input() for k in range(N)]
s = [''.join(sorted(list(s_element))) for s_element in s]
memory = {}
result = 0
for s_element in s:
    if s_element not in memory:
        memory[s_element] = 1
    else:
        result += memory[s_element]
        memory[s_element] += 1
print(result)
### BUGGY CODE ###

N,W = map(int,input().split())
w = []
v = []
for i in range(N):
    x,y = map(int,input().split())
    w.append(x)
    v.append(y)

dp = [[0]*(W+1) for j in range(N+1)] # DPテーブルの作成

for i in range(N):
    for j in range(W+1):
        if j < w[i]: # 選ばない時
            dp[i+1][j] = dp[i][j]
        else: # 選ぶ時
            dp[i+1][j] = max(dp[i][j],dp[i][j-w[i]]+v[i])

print(dp[N][W])

### FIXED CODE ###

N,W=map(int,input().split())
L = [list(map(int, input().split())) for i in range(N)]
dp = [[0 for j in range(110000)] for i in range(110)]
for i in range(1,N+1):
  for j in range(W+1):
    dp[i][j]=dp[i-1][j]
    if j-L[i-1][0]>=0:
      dp[i][j]=max(dp[i][j],dp[i-1][j-L[i-1][0]]+L[i-1][1])
ans=dp[N][W]
print(ans)
### BUGGY CODE ###

k, s = list(map(int, input().split(' ')))

count = 0

for i in range(k + 1):
    for j in range(k + 1):
        for l in range(k + 1):
            if i + j + l == s:
                count += 1

print(count)


### FIXED CODE ###

k, s = list(map(int, input().split(' ')))

count = 0

for i in range(k + 1):
    for j in range(k + 1):
        if 0 <= s - (i + j) <= k:
            count += 1
print(count)
### BUGGY CODE ###

from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n,p = readInts()
    A = readInts()
    cnt = 0
    for bit in range(1 << n):
        now = 0
        for i in range(n):
            if bit & (1 << i):
                now += A[i]
        if now % 2 == p:
            cnt += 1
    print(cnt)
if __name__ == '__main__':
  main()


### FIXED CODE ###

from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n,p = readInts()
    m = 0
    A = readInts()
    for i in range(n):
        a = A[i]
        if a % 2 == 1:
            m += 1
    if m == 0:
        print(2**n if p == 0 else 0)
    else:
        print(2**(n-1))
if __name__ == '__main__':
  main()
### BUGGY CODE ###

s = str(input())
s=s[::-1]
mod = 2019
li = [0]*(len(s)+1)
cnt=1
for i in range(len(s)):
    li[i+1]=li[i] + int(s[i]) * cnt
    li[i+1]%=mod
    cnt*=10
import collections
A=collections.Counter(li[1:]);ans=0
for u,v in A.items():
    ans+=v*(v-1)//2
    if u==0:
        ans+=v
print(ans)




### FIXED CODE ###

s = str(input())
s=s[::-1]
mod = 2019
li = [0]*(len(s)+1)
cnt=1
for i in range(len(s)):
    li[i+1]=li[i] + int(s[i]) * cnt
    li[i+1]%=mod
    cnt*=10
    cnt%=mod
import collections
A=collections.Counter(li[1:]);ans=0
for u,v in A.items():
    ans+=v*(v-1)//2
    if u==0:
        ans+=v
print(ans)
### BUGGY CODE ###

import sys
import collections
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    s = [input() for _ in range(n)]
    cnt = collections.Counter(s)
    d = sorted([k for k, v in cnt.items() if v == max(cnt.values())])
    print(*d, sep='\n')

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
import collections
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    s = [input() for _ in range(n)]
    cnt = collections.Counter(s)
    values, counts = zip(*cnt.most_common())
    d = sorted(values[:counts.count(counts[0])])
    print(*d, sep='\n')

if __name__ == '__main__':
    main()
### BUGGY CODE ###

N,P=[int(i) for i in input().split()]
i=2
ans = 1
c=0
while P>1:
  if P%i==0:
    P//=i
    c+=1
    if c==N:
      ans*=i
      c=0
  else:
    c=0
    i+=1

print(ans)

### FIXED CODE ###

import sys

N,P=[int(i) for i in input().split()]
i=2
ans = 1
c=0
if N == 1:
  print(P)
  sys.exit()
while P>i*i-4:
  if P%i==0:
    P//=i
    c+=1
    if c==N:
      ans*=i
      c=0
  else:
    c=0
    i+=1
if P==i and c+1 ==N:
  ans*=i
print(ans)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():    
    mod=10**9+7
    N,M=MI()
    adj=[[]for _ in range(N)]
    A=[0]*M
    B=[0]*M
    
    for i in range(M):
        A[i],B[i]=MI()
        A[i]-=1
        B[i]-=1
        adj[A[i]].append(B[i])
    
    import queue

    
    def bfs(a):
        #a=>aの最短距離を求める
        q=queue.Queue()
        q.put((a,-1,0))
        use=[0]*N
        use[a]=1
        while not(q.empty()):
            v,p,d=q.get()
            
            for nv in adj[v]:
                if nv==a:
                    return d+1
                else:
                    if nv!=p:
                        if use[nv]==0:
                            use[nv]=1
                            q.put((nv,v,d+1))
                            
        return -1
    
    def bfs2(a):
        #aからaまでの
        #経路復元も
        P=[-1]*N
        q=queue.Queue()
        q.put((a,-1,0))
        use=[0]*N
        use[a]=1
        flag=0
        
        while not(q.empty()):
            if flag:
                break
            v,p,d=q.get()
            #print(v,p,d)
            P[v]=p

            for nv in adj[v]:
                #print("nv:",nv)
                if nv==a:
                    flag=1
                    break
                if nv!=p:
                    if use[nv]==0:
                        use[nv]=1
                        q.put((nv,v,d+1))
                            
        L=[]
        flag=1
        for i in range(N):
            #print(v)
            L.append(v)
            v=P[v]
            if v==a:
                L.append(a)
                break
        return L
        
                        
                        
        

    
    ans=N+1
    ansi=0

    for i in range(N):#全ての辺をなめる
        temp=bfs(i)#ここはab間の最短距離をbfsで求める
        if temp!=-1:
            if temp<ans:
                ans=temp
                ansi=i

    
    if ans==N+1:
        print(-1)
    else:
        print(ans)
    
    
        
        ans=bfs2(ansi)
        for i in ans:
            print(i+1)
        
        
        

main()


### FIXED CODE ###

n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    edges[a - 1].append(b - 1)

ret = [0] * (n + 1)
for start in range(n):
    parent = [-1] * n
    q = [start]
    while len(q) > 0:
        i = q.pop(0)
        for j in edges[i]:
            if j == start:
                loop = [i]
                while i != start:
                    i = parent[i]
                    loop.append(i)
                if len(ret) > len(loop):
                    ret = loop
                break
            if parent[j] == -1:
                parent[j] = i
                q.append(j)

if len(ret) > n:
    print(-1)
else:
    print(len(ret))
    for i in reversed(ret):
        print(i + 1)
### BUGGY CODE ###

a = [0]*3
a[0],a[1],a[2],n = map(int,input().split())
a.sort()
cnt = 0
for i in range(n//a[2]+1):
    for j in range(n//a[1]+1):
        if (n-a[2]*i-a[1]*j)%a[0] == 0 and (n-a[2]*i-a[1]*j) >= 0:
            cnt += 1
print(cnt)


### FIXED CODE ###

a = [0]*3
a[0],a[1],a[2],n = map(int,input().split())
a.sort()
cnt = 0
for i in range(n//a[2]+1):
    for j in range((n-i*a[2])//a[1]+1):
        d = n-a[2]*i-a[1]*j
        if d%a[0] == 0 and d >= 0:
            cnt += 1
print(cnt)
### BUGGY CODE ###

try:
	while True:
		ans = [2]
		limit = input()
		for n in range(2, limit + 1):
			i = 1
			while True:
				if n % ans[i-1] == 0:
					break
				else:
					if(i == len(ans)):
						ans.append(n)
					i = i + 1
		print len(ans)
except EOFError:
	pass

### FIXED CODE ###

def prime_count(n):
	j = 0
	ans = [1] * (n + 1)
	ans[0] = ans[1] = 0
	for i in xrange(3, n + 1, 2):
		if i * 2 > n:
			break
		if ans[i] != 0:
			for j in xrange(2 * i, n + 1, i):
				ans[j] = 0
	for k in xrange(2 * 2, n + 1, 2):
		ans[k] = 0
	print sum(ans)

try:
	while True:
		n = input()
		prime_count(n)
except EOFError:
	pass
### BUGGY CODE ###

r,g,b,n = list(map(int, input().split()))
cnt = 0
i=0
while r*i <= n:
  j=0
  while (r*i+g*j) <= n:
    if ( (n-(r*i+g*j)) % b ) == 0 :
      cnt += 1
    j += 1
  i += 1
print(str(cnt))

### FIXED CODE ###

r,g,b,n = list(map(int, input().split()))
cnt = 0
for i in range(0, n+1, r):
  if i == n:
    cnt += 1
    continue
  for j in range(0, n-i+1, g):
    if ( (n-(i+j)) % b ) == 0 :
      cnt += 1
print(str(cnt))
### BUGGY CODE ###

n = int(input())
a = []
b = 1
c = 1
d = []
for i in range(n):
    a.append(int(input()))
while True:
    d.append(b-1)
    b = a[b-1]
    if b == 2:
        print(c)
        break
    if (b-1) in d:
        print(-1)
        break
    c += 1
    


### FIXED CODE ###

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

k = 1
p = False
for i in range(n):
    if a[k-1] == 2:
        print(i+1)
        p = True
        break
    k = a[k-1]
if not p:
    print(-1)