Harryxun/stf_run huggingface dataset:
N,K = list(map(int,input().split()))

A = list(map(int,input().split()))
visited = [1]
visited_list = [0]*N
visited_list[0] = 1
k = 0
l = 0
kaburi = 1
def f(Ai):
    global k,l,visited,kaburi
    # print("Ai",Ai,k)
    if k == K:
        print(A[Ai])
    elif visited_list[Ai-1] == 1 :
        visited.append(Ai)
        kaburi = Ai
        k += 1
        l = k
    else:
        visited.append(Ai)
        visited_list[Ai-1] = 1
        k += 1
        f(A[Ai-1])

f(A[0])
# print(l)
# print(visited)
a = [i for i, x in enumerate(visited) if x == kaburi]
b = visited[a[0]:a[1]]
# print(b)
# print(l)
print(b[(K-a[0])%(l-a[0])])

### FIXED CODE ###

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

A = [0]+ list(map(int,input().split()))

n = 1
visited = [1]
visitedbit = [0] * (N+1) 
visitedbit[1] = 1
k = 0
flag = True
while True:
    
    k += 1
    #A[n]はテレポート先
    if k == K:
        print(A[n])
        flag = False
        break
    if visitedbit[A[n]]  == 0:
        visited.append(A[n])
        visitedbit[A[n]] = 1
        n = A[n]
    else:
        hazime = visited.index(A[n])
        break

if flag == True:
    # print(visited)
    loop = visited[hazime:]
    # print(loop)
    # print(hazime)
    print(loop[ (K-hazime)%(len(loop)) ])
N = int(input())
S = input()
max_range = N//2
count_list = [[0 for j in range 50005] for i in range 5005]
print(count_list.shape)
print(count_list)

### FIXED CODE ###

N = int(input())
S = input()
sride = 1
count = 0
max_num = 0
for i in range(N-1):
  for j in range(N-sride):
    if S[j] == S[j+sride]:
      count += 1
      max_num = max(max_num, count)
    else:
      count = 0
    if count == sride:
      max_num = max(max_num, count)
      break
  sride += 1
  count = 0
print(max_num)
N =
if N % 7 == 0:
	print("Yes")
else:
  	print("No")

### FIXED CODE ###

s = input()
if "7" in s:
    print("Yes")
else:
    print("No")
N = int(input())
a = [int(input()) for _ in range(N)]
c = [0] * N

def f(i, n):
  if a[n] == 2: return i
  if c[n] == 1: return -1
  c[n] = 1
  return f(i + 1, a[n] - 1)

print(f(1, 0))


### FIXED CODE ###

import sys
sys.setrecursionlimit(100000)

N = int(input())
a = [int(input()) for _ in range(N)]
c = [0] * N

def f(i, n):
  if a[n] == 2: return i
  if c[n] == 1: return -1
  c[n] = 1
  return f(i + 1, a[n] - 1)

print(f(1, 0))
def func(A):
  result = 0
  min = 0
  for a in A:
    if a < min:
      result += min - a
    else:
      min = a
  return result

if __name__ == "__main__":
    N = int(input())
    A = list(int,input().split())
    print(func(A))

### FIXED CODE ###

def func(A):
  result = 0
  min = 0
  for a in A:
    if a < min:
      result += min - a
    else:
      min = a
  return result

if __name__ == "__main__":
    N = int(input())
    A = list(map(int,input().split()))
    print(func(A))
C = str(input())

alph = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u

for i, j in enumerate(alph):
  if C == j:
    print(alph[i+1])


### FIXED CODE ###

C = str(input())

alph = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

for i, j in enumerate(alph):
  if C == j:
    print(alph[i+1])
n=int(input())
if n%2<1:
 a=n=n//10
 while n:n//=5;a+=n
print(a)

### FIXED CODE ###

n=int(input())
if n%2: print(0); exit()
c=0
n//=2
while n: n//=5; c+=n
print(c)
from numba import jit
N, Y = map(int, input().split())

count_max = Y / 1000  # all 1000

@jit
def sample(n, y):
    ans = '-1 -1 -1'
    for i in reversed(range(y // 10000 + 1)):
        count_tmp1 = count_max - 9 * i
        price_tmp1 = y - 10000 * i
        if count_tmp1 < n or price_tmp1 < 0:
            continue

        if count_tmp1 == n:
            ans = '{} {} {}'.format(i, 0, N - i)
            return ans

        if price_tmp1 > 5000 * count_tmp1:
            return ans

        for j in reversed(range(price_tmp1 // 5000 + 1)):
            count_tmp2 = count_tmp1 - 4 * j
            price_tmp2 = price_tmp1 - 5000 * j
            if count_tmp2 < n or price_tmp2 < 0:
                continue
            if count_tmp2 == n:
                ans = '{} {} {}'.format(i, j, N - i -j)
                return ans
            if price_tmp2 > 5000 * count_tmp2:
                return ans
    return ans
a = sample(N, Y)
print(a)

### FIXED CODE ###

S = input()

print('2018' + S[4:])
H, N,  = map(int, input().split())


for i in range(1,N+1):
    Damage = []
    Ai = int(input())
    Damage.append(Ai)
Total_Damage = sum(Damage)

if H <= Total_Damage:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

H, N = map(int, input().split())
A_list = []
for i in range(1, N+1):
    A_list.append("A_" + str(i))

A_list = map(int, input().split())
A_total = sum(A_list)

if A_total >= H:
    print("Yes")
else:
    print("No")
n, m = map(int, input().split())
lst = list(map(int, input().split()))
lst = sorted(lst, key = lambda x:-x)
dic = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

s = set()
for i in lst:
    s.add(dic[i])
s = list(sorted(s))

dp = [0] * (n+1)
for i in range(0, n+1):
    max_a = 0
    for j in s:
        if i - j >= 0:
            a = dp[i-j] + 1
            if max_a < a:
                max_a = a
    dp[i] = max_a

def out(dp, dic, n, ans, ans_digit):
    if len(ans) == ans_digit-1:
        for i in lst:
            if n == dic[i]:
                return ans + str(i)
    else:
        for i in lst:
            if n - dic[i] >= 0:
                if dp[n] -1 == dp[n-dic[i]]:
                    ans += str(i)
                    next_n = n - dic[i]
                    break
        return out(dp, dic, next_n, ans, ans_digit)
    
print(out(dp, dic, n, '', dp[n]))

### FIXED CODE ###

n, m = map(int, input().split())
lst = list(map(int, input().split()))
lst = sorted(lst, key = lambda x:-x)
dic = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

s = set()
for i in lst:
    s.add(dic[i])
s = list(sorted(s))

dp = [0] * (n+1)
for i in range(0, n+1):
    a = 0
    max_a = 0
    for j in s:
        if i-j < 0:
            pass
        elif i == j:
            a = 1
        elif i - j > 0 and dp[i-j] > 0:
            a = dp[i-j] + 1
        if max_a < a:
            max_a = a
    dp[i] = max_a

ans = ''
ans_digit = dp[n]
while len(ans) < ans_digit:
    if len(ans) == ans_digit-1:
        for i in lst:
            if n == dic[i]:
                ans += str(i)
                break
    else:
        for i in lst:
            if n - dic[i] >= 0:
                if dp[n] -1 == dp[n-dic[i]]:
                    ans += str(i)
                    n -= dic[i]
                    break
    
print(ans)
x = int(input())
money = 100
year = 0

while money < x:
    money += money // 100;
    money = math.floor(money)
    year += 1
    
print(year)

### FIXED CODE ###

x = int(input())

ans = 0
money = 100
while money < x:
  money *= 1.01
  money = int(money)
  ans += 1
print(ans)
if __name__ == '__main__':
    import sys
    input = sys.stdin.readline
    n = int(input())
    hs = list(map(int, input().split()))
    hs = hs[::-1]
    def jump(p):
        if p == 1:
            return 0
        elif p == 2:
            return abs(hs[1]-hs[0])
        else:
            return min([jump(p-1)+abs(hs[p-1]-hs[p-2]), jump(p-2)+abs(hs[p-1]-hs[p-3])])
    print(jump(n))


### FIXED CODE ###

if __name__ == '__main__':
    import sys
    input = sys.stdin.readline
    n = int(input())
    hs = list(map(int, input().split()))
    hs = hs[::-1]
    ans_list = [0]*n
    ans_list[1] = abs(hs[1]-hs[0])
    for i in range(2,n):
        ans_list[i] = min([ans_list[i-1]+abs(hs[i]-hs[i-1]), ans_list[i-2]+abs(hs[i]-hs[i-2])])
    print(ans_list[n-1])
from collections import defaultdict

k = int(input())

def prime_factorize(n):
    a = defaultdict(int)
    while n % 2 == 0:
        a[2] += 1
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a[f] += 1
            n //= f
        else:
            f += 2
    if n != 1:
        a[n] += 1
    return a

def f(k):
  if k % 2 == 0 or k % 5 == 0:
    return -1    

  val = 7
  cnt = 1
  while True:
    if val % k == 0:
      break
    cnt += 1
    val = val * 10 + 7
  return cnt
    
li = prime_factorize(k)

ans = 1
for k, v in li.items():
  if k <= 10000:
    ans *= f(k) ** v
  else:
    ans *= (l - 1) ** v
    
if ans < 0:
  print(-1)
else:
  print(ans)

### FIXED CODE ###

k = int(input())

def f(k):
  if k % 2 == 0 or k % 5 == 0:
    return -1    

  val = 7
  cnt = 1
  while cnt < 10 ** 6:
    val %= k
    if val % k == 0:
      break
    cnt += 1
    val = val * 10 + 7
  else:
    return -1
  return cnt
    
print(f(k))
ABC = input().split()
A=ABC[0]
B=ABC[1]
C=ABC[2]


count = 50
N=0


for index in range(count+1):
    if (A*N-C)%B ==0:
        print("Yes")
    elif index == (count):
        print("NO")
    else:
        N+=1

### FIXED CODE ###

ABC = input().split()
A=int(ABC[0])
B=int(ABC[1])
C=int(ABC[2])


count = A*B


for index in range(count+1):
    if (A*index)%B==C:
        print("YES")
        break
    elif index ==count:
        print("NO")
from fractions import gcd

def lcm(x, y):
  return (x * y) // gcd(x, y)

n = int(input())
a = list(map(int,input().split()))
g = a[0]
for i in range(n):
  g = lcm(g,a[i])
ans = 0
for i in range(n):
  ans += g/a[i]
print(int(ans%1000000007))


### FIXED CODE ###

from fractions import gcd

def lcm(x, y):
  return (x * y) // gcd(x, y)

n = int(input())
a = list(map(int,input().split()))
g = a[0]
for i in range(n):
  g = lcm(g,a[i])
ans = 0
for i in range(n):
  ans += g // a[i]
print(ans%1000000007)
N = int(input())
L = list(map(int,input().split()))

l_list = list(itertools.combinations(L, 3))

res = 0

for tri in l_list:
  tri.sort()
  if tri[0] + tri[1] > tri[2]
  	res += 1
    
print(res)

### FIXED CODE ###

import itertools

N = int(input())
L = list(map(int,input().split()))
 
l_list = list(itertools.combinations(L, 3))

res = 0
 
for tri in l_list:
  l = list(tri)
  l.sort()
  if (len(l) != len(set(l))):
    continue
  if (l[0] + l[1] > l[2]):
  	res += 1
    
print(res)
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(list(input()))
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
  for j in range(W):
    if s[i][j] == '#':
      continue
    cnt = 0
    for n in range(8):
      ni = i + dx[n]
      nj = j + dy[n]
      if ni < 0 or H <= ni or nj < 0 or W <= nj:
        continue
      if s[ni][nj] == '#':
        cnt += 1
    S[i][j] = str(cnt)


    
for i in range(H):
    print("".join(S[i]))

### FIXED CODE ###

H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(list(input()))
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
  for j in range(W):
    if S[i][j] == '#':
      continue
    cnt = 0
    for n in range(8):
      ni = i + dx[n]
      nj = j + dy[n]
      if ni < 0 or H <= ni or nj < 0 or W <= nj:
        continue
      if S[ni][nj] == '#':
        cnt += 1
    S[i][j] = str(cnt)


    
for i in range(H):
    print("".join(S[i]))
n = int(input())

my_pos_x = 0
my_pos_y = 0

for i in range(n):
    place = list(map(int, input().split()))
    for j in range(place[0]):
        if my_pos_x < place[1]:
            my_pos_x = + 1
            continue
        elif my_pos_x > place[1]:
            my_pos_x = - 1
            continue
        else:
            if my_pos_y < place[2]:
                my_pos_y = + 1
                continue
            elif my_pos_y > place[2]:
                my_pos_y = + -1
                continue
            else:
                my_pos_x += 1

if my_pos_x == place[1] and my_pos_y == place[2]:
    print("YES")
else:
    print("NO")


### FIXED CODE ###

input_val1, input_val2 = map(int, input().split())

prod = input_val1 * input_val2

if (prod % 2) == 0:
    print ('Even')
else:
    print('Odd')
import fractions
from functools import reduce
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)
def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

n=int(input())
l=list(map(int,input().split()))
a=lcm_list(l)
ans_l = []
for i in l:
    ans_l.append(a/i)
print(int(sum(ans_l)%(10**9+7)))

### FIXED CODE ###

import fractions
from functools import reduce
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)
def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

n=int(input())
l=list(map(int,input().split()))
a=lcm_list(l)
ans_l = []
for i in l:
    ans_l.append(a//i)
print(int(sum(ans_l)%(10**9+7)))
n, l = map(int, input().split())
strs = []

for i in range(l):
    strs.append(input())

strs.sort()
print(''.join(strs[:n]))

### FIXED CODE ###

n, l = map(int, input().split())
strs = []

for i in range(n):
    strs.append(input())

strs.sort()
print(''.join(strs))
import sys
input = sys.stdin.readline

n, q = map(int, input().split())
p = []
x = [0] * (n+1)
for _ in range(n-1):
    p.append(list(map(int, input().split())))

for _ in range(q):
    tmp1, tmp2 = map(int, input().split())
    x[tmp1] += tmp2

# 隣接リスト
l = [[] for _ in range(n+1)]
for pi in p:
    l[pi[0]].append(pi[1])
    l[pi[1]].append(pi[0])
p = [0] * (n+1)

import sys
sys.setrecursionlimit(100000)


def dfs(n, v, par):
    p[n] = v
    for chl in l[n]:
        if chl == par:
            continue  # parent
        dfs(chl, v+x[chl], n)
    return

dfs(1, x[1], 0)
print(*p[1:])

### FIXED CODE ###

import sys
input = sys.stdin.readline

n, q = map(int, input().split())
p = []
x = [0] * (n+1)
for _ in range(n-1):
    p.append(list(map(int, input().split())))

for _ in range(q):
    tmp1, tmp2 = map(int, input().split())
    x[tmp1] += tmp2

# 隣接リスト
l = [[] for _ in range(n+1)]
for pi in p:
    l[pi[0]].append(pi[1])
    l[pi[1]].append(pi[0])
p = [0] * (n+1)

import sys
sys.setrecursionlimit(10000000)


def dfs(n, v, par):
    p[n] = v
    for chl in l[n]:
        if chl == par:
            continue  # parent
        dfs(chl, v+x[chl], n)
    return

dfs(1, x[1], 0)
print(*p[1:])
import sys

def input_w():
    w = input()

    while w.islower():

        length = len(w)
        if length >= 100:
            return input_w()

        for n_alpha in "abcdefghijklmnopqrstuvwxyz":
            count_alpha = w.count(n_alpha)
            if count_alpha % 2 == 1:
                print('No')
                return input_w()

        print('Yes')
        sys.exit()

    return input_w()

w=input_w()

### FIXED CODE ###

import string
w = input()
if all([w.count(low_alpha) % 2 == 0 for low_alpha in string.ascii_lowercase ]):
    print('Yes')
else:
    print('No')
import sys

def f(pos, S, goal):
    F = 0
    while len(S) > 0 and S[0] == 'F':
        F += 1
        _ = S.pop(0)

    num_F = sum([c == 'F' for c in S])

    next_pos = set()
    for p in pos:
        x = p[0]
        y = p[1]
        if p[2] == 0:  # Forward to +x
            x += F
        elif p[2] == 1:  # Forward to +y
            y += F
        elif p[2] == 2:  # Forward to -x
            x -= F
        else:              # direct == 3, Foward to -y
            y -= F

        dist = abs(goal[0] - x) + abs(goal[1] - y)
        if num_F < dist:
            continue

        direct1 = (p[2] + 1) % 4
        direct2 = (p[2] + 3) % 4

        np1 = (x, y, direct1)
        np2 = (x, y, direct2)

        next_pos.add(np1)
        next_pos.add(np2)

    if len(next_pos) == 0:
        return False

    if len(S) == 0:
        ans = False
        for p in next_pos:
            if p[0] == goal[0] and p[1] == goal[1]:
                ans = True
                break
        return ans

    _ = S.pop(0)  # Remove 'T' at S[0]
    return f(next_pos, S, goal)


def main():
    input = sys.stdin.readline
    S = [str(c) for c in input().strip()]
    goal = list(map(int, input().split()))

    init = (0, 0, 0)
    pos = set()
    pos.add(init)

    if f(pos, S, goal):
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys

def main():
    input = sys.stdin.readline
    S = [str(c) for c in input().strip()]
    X, Y = map(int, input().split())
    F = []
    while len(S) > 0:
        f = 0
        while len(S) > 0 and S[0] == 'F':
            f += 1
            S.pop(0)
        F.append(f)
        if len(S) > 0:
            S.pop(0)

    Fx = []
    Fy = []
    for i in range(1, len(F)):
        if i % 2 == 0:
            Fx.append(F[i])
        else:
            Fy.append(F[i])

    dpx = [[F[0]]] + [0] * len(Fx)
    for i in range(len(Fx)):
        nx = set()
        for a in dpx[i]:
            nx.add(a + Fx[i])
            nx.add(a - Fx[i])
        dpx[i+1] = nx

    dpy = [[0]] + [0] * len(Fy)
    for i in range(len(Fy)):
        ny = set()
        for a in dpy[i]:
            ny.add(a + Fy[i])
            ny.add(a - Fy[i])
        dpy[i+1] = ny


    if X in dpx[-1] and Y in dpy[-1]:
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()
N,K=list(map(int,input().split()))
l=list(map(int,input().split()))
import math
A=math.gcd(l[0],l[1])
if K<=max(l):
   for i in range(max(0,N-2)):
      A=math.gcd(A,l[i+2])
   if K%A==0:
      print("POSSIBLE")
   else:
      print("IMPOSSIBLE")
else:
   print("IMPOSSIBLE")

### FIXED CODE ###

N,K=list(map(int,input().split()))
l=list(map(int,input().split()))
import math
if N==1:
   if l[0]==K:
      print("POSSIBLE")
      exit()
   else:
      print("IMPOSSIBLE")
      exit()
A=math.gcd(l[0],l[1])
if K<=max(l):
   for i in range(max(0,N-2)):
      A=math.gcd(A,l[i+2])
   if K%A==0:
      print("POSSIBLE")
   else:
      print("IMPOSSIBLE")
else:
   print("IMPOSSIBLE")
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=[0 for i in range(n)]
z=set(a)
y=len(z)
for x in a:
    s[x]+=1
b=sorted(s)
f=0
ans=0
for x in b:
    if x!=0:
        f+=1
        ans+=x
    if f>=y-k:
      	break
print(ans)


### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
s=[0 for i in range(n)]
z=set(a)
y=len(z)
for x in a:
    s[x-1]+=1
b=sorted(s)
f=0
ans=0
for x in b:
    if x!=0:
        f+=1
        ans+=x
    if f>=y-k:
      	break
print(ans)
n = int(input())
a = [int(input()) for i in range(n)]
maxi = max(a)
for i in range(n):
    if a[i] != maxi:
        print(maxi)
    else:
        b = a.pop(a[i])
        print(max(a))


### FIXED CODE ###

n = int(input())
a = [int(input()) for i in range(n)]
maxi = max(a)
s = sorted(a,reverse=True)

for i in range(n):
    if a[i] != maxi:
        print(maxi)
    else:
        print(s[1])
in=input()
ok=0
for i in range(len(in)):
  if(in[i]=='7'):
    print("Yes")
    ok=1
    break
if(ok==0):
  print("No")

### FIXED CODE ###

inp=input()
ok=0
for i in range(len(inp)):
  if(inp[i]=='7'):
    print("Yes")
    ok=1
    break
if(ok==0):
  print("No")
#coding:utf-8
  
import sys
import copy
  
def main():
    line = sys.stdin.readline()
    #case
    while line.split() != ["0","0"]:
        analy(line)
        line = sys.stdin.readline()
    return
  
def analy(line):
    #t
    field = [int(n) for n in line.split()]
    # *&
    num = int(sys.stdin.readline())
    data = []
    for i in range(num):
        a = sys.stdin.readline()
        b = [int(n) for n in a.split()]
        #*&
        data.append(b)
    print (compute(field, data))
  
def compute(field, ng):
    list = []
    for i in range(field[1]):
        for j in range(field[0]):
            #*?&k??WfDO
                if i == 0 :
                    #j
                    if [j+1,i+1] in ng:
                        list.append(0)
                    elif j == 0:
                        #?LjD
                        list.append(1)
                    else:
                        list.append(list[j-1])
                else:
                    if [j+1,i+1] in ng:
                        list[j] = 0
                    elif j != 0:
                    #no left >> the same.
                        list[j] = list[j-1] + list[j]
    return list[len(list)-1]
  
if __name__ == "__main__":
    main()

### FIXED CODE ###

#coding:utf-8
  
import sys
import copy
  
def main():
    line = sys.stdin.readline()
    #case
    while line.split() != ["0","0"]:
        analy(line)
        line = sys.stdin.readline()
    return
  
def analy(line):
    #????
    field = [int(n) for n in line.split()]
    # ???A?c
    num = int(sys.stdin.readline())
    data = []
    for i in range(num):
        a = sys.stdin.readline()
        b = [int(n) for n in a.split()]
        #???A?c
        data.append(b)
    print (compute(field, data))
  
def compute(field, ng):
    list = []
    for i in range(field[1]):
        for j in range(field[0]):
            #???¨?c???????????¢??
                if i == 0 :
                    #??????
                    if [j+1,i+1] in ng:
                        list.append(0)
                    elif j == 0:
                        #?¶?????¢
                        list.append(1)
                    else:
                        list.append(list[j-1])
                else:
                    if [j+1,i+1] in ng:
                        list[j] = 0
                    elif j != 0:
                    #no left >> the same.
                        list[j] = list[j-1] + list[j]
    return list[len(list)-1]
  
if __name__ == "__main__":
    main()
def dfs1(u, pu):
    dp[u] = 1
    for v in tree[u]:
        if v != pu:
            dfs1(v, u)
            dp[u] *= (dp[v] + 1)
            dp[u] %= MOD

def dfs2(u, pu, pval):
    resp[u] = (dp[u] * pval) % MOD
    
    chlds = [v for v in tree[u] if v != pu]
    cumulr = [1] * len(chlds)
    for i in range(len(chlds) - 2, -1, -1):
        cumulr[i] = cumulr[i + 1] * (1 + dp[chlds[i + 1]]) % MOD

    pref = pval
    for i in range(len(chlds)):
        v = chlds[i]
        vval = (1 + pref * cumulr[i]) % MOD

        dfs2(v, u, vval)

        pref *= (dp[v] + 1)
        pref %= MOD


n, MOD = map(int, raw_input().split())
tree = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v = map(lambda u: int(u) - 1, raw_input().split())
    tree[u].append(v)
    tree[v].append(u)

dp = [0] * n
resp = [0] * n
dfs1(0, -1)
dfs2(0, -1, 1)
for i in range(n):
    print(resp[i])


### FIXED CODE ###



n, MOD = map(int, raw_input().split())
tree = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v = map(lambda u: int(u) - 1, raw_input().split())
    tree[u].append(v)
    tree[v].append(u)


cola = [(0, -1)]
qind = 0
while qind < len(cola):
    u, pu = cola[qind]
    qind += 1
    for v in tree[u]:
        if v != pu:
            cola.append((v, u))

dp = [1] * n
for i in range(n - 1, -1, -1):
    u, pu = cola[i]
    for v in tree[u]:
        if v != pu:
            dp[u] *= (dp[v] + 1)
            dp[u] %= MOD

resp = [0] * n
pval = [1] * n
for j in range(n):
    u, pu = cola[j]
    resp[u] = (dp[u] * pval[u]) % MOD

    chlds = [v for v in tree[u] if v != pu]
    cumulr = [1] * len(chlds)
    for i in range(len(chlds) - 2, -1, -1):
        cumulr[i] = cumulr[i + 1] * (1 + dp[chlds[i + 1]]) % MOD

    pref = pval[u]
    for i in range(len(chlds)):
        v = chlds[i]
        pval[v] = (1 + pref * cumulr[i]) % MOD
        pref *= (dp[v] + 1)
        pref %= MOD

for i in range(n):
    print(resp[i])
N = int(input())
A_list = [int(item) for item in input().split()]

all_sum = sum(A_list)

F_sum_list = []

for j in range(1,N):
    F = A_list[:j]
    F_sum = sum(F)
    F_sum_list.append(F_sum)
    if F_sum >= all_sum // 2:
        break

mini = min(all_sum - 2 * F_sum_list[-2], -1*(all_sum - 2 * F_sum_list[-1]))
print(mini)

### FIXED CODE ###

N = int(input())
A_list = [int(item) for item in input().split()]

all_sum = sum(A_list)

F_sum_list = [A_list[0]]

for j in range(1,N):
    F_sum_list.append(F_sum_list[-1] + A_list[j])

delta_list = [abs(all_sum - 2* i) for i in F_sum_list]

print(min(delta_list))