Harryxun/stf_run huggingface dataset:
import sys
#大きすぎるとダメ
h,w=map(int,input().split())
sys.setrecursionlimit(h*w+1)
s=[list(input()) for i in range(h)]
inf=1000000000000000
d=[[inf]*w for i in range(h)]


def dfs(i,j):
    global h,w,s
    nex=[(0,-1),(0,1),(-1,0),(1,0)]
    for k in range(4):
        l,m=i+nex[k][0],j+nex[k][1]
        if 0<=i+nex[k][0]<h and 0<=j+nex[k][1]<w:
            if s[l][m]=="." and d[l][m]>d[i][j]+1:
                d[l][m]=d[i][j]+1
                dfs(l,m)
d[0][0]=0
dfs(0,0)
ans=0
for i in range(h):
    for j in range(w):
        if s[i][j]==".":
            ans+=1
if d[h-1][w-1]==inf:
    print(-1)
else:
    print(ans-d[h-1][w-1]-1)




### FIXED CODE ###

#一個だけRE、わからねえ
#解答見る
import sys
sys.setrecursionlimit(1005)
h,w=map(int,input().split())

def go_next(I,J,n):
    n+=1
    global dp,w,h
    if n>1000:
        return
    if 0<=I<h-1:
        if x[I+1][J]=="." and dp[I+1][J]>dp[I][J]+1:
            dp[I+1][J]=dp[I][J]+1
            go_next(I+1,J,n)
    if 0<I<=h-1:
        if x[I-1][J]=="." and dp[I-1][J]>dp[I][J]+1:
            dp[I-1][J]=dp[I][J]+1
            go_next(I-1,J,n)
    if 0<=J<w-1:
        if x[I][J+1]=="." and dp[I][J+1]>dp[I][J]+1:
            dp[I][J+1]=dp[I][J]+1
            go_next(I,J+1,n)
    if 0<J<=w-1:
        if x[I][J-1]=="." and dp[I][J-1]>dp[I][J]+1:
            dp[I][J-1]=dp[I][J]+1
            go_next(I,J-1,n)

x=[]
for i in range(h):
    x.append(list(input()))
#print(x)
c=0
for i in range(h):
    for j in range(w):
        if x[i][j]==".":
            c+=1
dp=[[10000000000 for i in range(w)]for j in range(h)]
dp[0][0]=1
go_next(0,0,0)
if dp[h-1][w-1]==10000000000:
    print(-1)
else:
    print(c-dp[h-1][w-1])
str = input()
l = []
while True:
    l.append(str)
    str = input()

for i in range(len(l)):
    numl = l[i].split(' ')
    a = int(numl[0])
    b = int(numl[1])
    sum = a + b
    digitstr = "{0}".format(sum)
    print(len(digitstr))

### FIXED CODE ###

import sys
l = []
for line in sys.stdin:
    l.append(line)

for i in range(len(l)):
    numl = l[i].split(' ')
    a = int(numl[0])
    b = int(numl[1])
    sum = a + b
    digitstr = "{0}".format(sum)
    print(len(digitstr))
N = int(input())
L = list(map(int, input().split()))

L.sort()
#print(L)

import sys

sys.setrecursionlimit(10**6)

count = 0
for i in range(N-1):
    for j in range(i+1, N-1):
        L_ = L[j+1:]
        #print(L[i] + L[j], ">", L_, "?")
        key = len(L_) // 2
        #print("init key", key)
        past_key = 0
        n = 2
        while key:
            n *= 2
            if L[i] + L[j] > L_[key]:
                key += round(len(L_) / n)
            elif L[i] + L[j] <= L_[key]:
                #print(n, round(key / n))
                key -= round(len(L_) / n)
            
            if past_key == key:
                break
            else:
                past_key = key
            #print("next key", key)
            
        if L[i] + L[j] > L_[key]:
            count += key + 1
            

print(count)

### FIXED CODE ###

def fnt(arr,n): 
  
     
     
     
    count = 0
  
     
    for i in range(0,n-2): 
  
         
        k = i + 2
  
        # Fix the second element 
        for j in range(i+1,n): 
  
             
            while (k < n and arr[i] + arr[j] > arr[k]): 
                k += 1
  
             
            if(k>j): 
                count += k - j - 1
  
    return count
 
 
n=int(input())
arr=input()
arr=arr.split()
arr=list(map(int,arr))
arr.sort()
print(fnt(arr,n))
import math

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

if(N!=K):
    print(math.ceil(N/(K-1)))
else if((N+1)/K==2):
    print(2)
else:
    print(1)

### FIXED CODE ###

import math

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


print(math.ceil((N-1)/(K-1)))
N = int(input())
S = input()

if len(S) % 2 != 0:
    print("No")
else:
    if S[:(len(S) / 2)] == S[(len(S) / 2) :]:
        print("Yes")
    else:
        print("No")

### FIXED CODE ###

N = int(input())
S = input()

if len(S) % 2 != 0:
    print("No")
else:
    if S[:(len(S) // 2)] == S[(len(S) // 2) :]:
        print("Yes")
    else:
        print("No")
import.math
R = int(input())
print(2*R * math.pi)

### FIXED CODE ###

import numpy as np

R = int(input())
print(2 * R * np.pi)
S = int(input())
h = S // 3600
m S % 3600 // 60
s = S % 60
print(h,m,s,sep = ":")

### FIXED CODE ###

S = int(input())
h = S // 3600
m = S % 3600 // 60
s = (S % 3600) - (m * 60)
print(h,m,s,sep=':')
n = int(input())
l = [i for i in range(1,n+1)]
l = list(filter(lambda x: x%3 != 0,l))
l = list(filter(lambda x; x%5 != 0,l))
print(sum(l))

### FIXED CODE ###

n = int(input())
l = [i for i in range(1,n+1)]
l = list(filter(lambda x: x%3 != 0,l))
l = list(filter(lambda x: x%5 != 0,l))
print(sum(l))
y, m, d = input()
y = '2018'
print(y + '/' + m + '/' + d)

### FIXED CODE ###

y, m, d = input().split('/')
y = '2018'
print(y + '/' + m + '/' + d)
import sys

readline = sys.stdin.buffer.readline
n = int(readline())
A = map(int, readline().split())
mod = 1000000007

caps = [0] * 3
ans = 1
for a in A:
    ans *= caps.count(a)
    caps[caps.index(a)] += 1
    ans %= mod
print(ans)


### FIXED CODE ###

import sys

readline = sys.stdin.buffer.readline
n = int(readline())
A = map(int, readline().split())
mod = 1000000007

caps = [0] * 3
ans = 1
for a in A:
    ans *= caps.count(a)
    # caps[caps.index(a)] += 1
    for i in range(3):
        if caps[i] == a:
            caps[i] += 1
            break
    ans %= mod
print(ans)
import sys
a,b=map(int,input().split())
for i in range(1,1100):#max_a=1
    if int(i*0.08)==a and int(i*0.1)==b:
        ans=i
        bresak
    if i==1099:
        ans=-1

print(ans)

### FIXED CODE ###

import sys
a,b=map(int,input().split())
f=True
for i in range(1,1010):#max_a=1
    if int(i*0.08)==a and int(i*0.1)==b:
        ans=i
        f=False
        break
if f:
  print(-1)
else:
  print(ans)
C,D = [int(e) for e in input().split()]

if C <= D:
    m = D
    n = C
else:
    m = C
    n = D

while  n != 0 :
    a = m%n
    m = n
    n = a

J = []
for i in range(1, int(m**0.5)+1):
    if m % i == 0:
        J.append(i)
        if i != m//i:
            J.append(m//i)


J.sort()

N = int(len(J))
M = [1]
I = []

for i in range(1,N):
    for p in range(1,int(J[i]**0.5)+1:
        if J[i]%p == 0:
            I.extend([J[i]])
    if int(len(I)) == 1:
        M.extend([J[i]])
    I = []


M = int(len(M))

print(M)

### FIXED CODE ###

C,D = [int(e) for e in input().split()]

if C <= D:
    m = D
    n = C
else:
    m = C
    n = D

while  n != 0 :
    a = m%n
    m = n
    n = a

J = []

for i in range(1,int(m**0.5)+1):
    if m % i == 0:
        J.append(i)
        if i != m//i:
            J.append(m//i)


J.sort()

N = int(len(J))
M = [1]
I = []

for i in range(1,N):
    for p in range(1,int(J[i]**0.5)+1):
        if J[i]%p == 0:
            I.extend([J[i]])
            if int(len(I)) >=2:
                break
    if int(len(I)) == 1:
        M.extend([J[i]])
    I = []


M = int(len(M))

print(M)
N, X, M = map(int, input().split())
if X == 0:
    print(0)
tx = X % M
tl = []
ts = set()
ttl = []
for i in range(N):
    tl.append(tx)
    ts.add(tx)
    tx = pow(tx, 2, M)
    if tx in ts:
        ttl = tl[tl.index(tx):]
        break
ans = sum(tl)
d, m = divmod(N-len(tl), len(ttl))
ans += sum(ttl)*d+sum(ttl[:m])
print(ans)


### FIXED CODE ###

N, X, M = map(int, input().split())
if X == 0:
    print(0)
    exit()
tx = X % M
tl = []
ts = set()
ttl = []
for i in range(N):
    tl.append(tx)
    ts.add(tx)
    tx = pow(tx, 2, M)
    if tx in ts:
        ttl = tl[tl.index(tx):]
        break
ans = sum(tl)
if len(ttl)>0:
    d, m = divmod(N-len(tl), len(ttl))
    ans += sum(ttl)*d+sum(ttl[:m])
print(ans)
# とりあえずランレングスhttps://www.kabegiwablog.com/entry/2018/04/09/100000
# 引数を配列に格納
# num = "><>>><<><<<<<>>><"
num = input()

# 変数初期化
count = 1
result = ''

# 配列全体のループ
for i in range(len(num)):
    try:
        if num[i] == num[i+1]:
           count = count + 1
        else:
            result = result + str(count) + str(num[i])
            count = 1
    except:
        # 最後の文字セットがOutOfRangeになるので結果に出力
        result = result + str(count) + str(num[i])
        
def sum_numbers(n):
    if n ==0:
        return 0
    return n + sum_numbers(n-1)

ans = 0
num_list = []
char_list = []

for idx, char in enumerate(result):
    if idx % 2 == 1:
        char_list.append(char)
    else:
        num_list.append(int(char))
        
if char_list[0] == "<":
    if len(char_list) % 2 == 1:
        for i in range(0, len(num_list)-1, 2):
#             print(num_list[i], num_list[i+1])
            if num_list[i] < num_list[i+1]:
                temp_sum = sum_numbers(num_list[i]-1) + sum_numbers(num_list[i+1])
#             print("上", temp_sum)
                ans += temp_sum
            else:
                temp_sum = sum_numbers(num_list[i]) + sum_numbers(num_list[i+1]-1)
#             print("下", temp_sum)
                ans += temp_sum
#             print(ans)
        ans += sum_numbers(num_list[-1])
    else:
        for i in range(0, len(num_list), 2):
#             print(num_list[i], num_list[i+1])
            if num_list[i] < num_list[i+1]:
                temp_sum = sum_numbers(num_list[i]-1) + sum_numbers(num_list[i+1])
#             print("上", temp_sum)
                ans += temp_sum
            else:
                temp_sum = sum_numbers(num_list[i]) + sum_numbers(num_list[i+1]-1)
#             print("下", temp_sum)
                ans += temp_sum
#             print(ans)

if char_list[0] == ">":
#     print("test",len(char_list)-1 % 2)
    if (len(char_list)-1) % 2 == 1:
        for i in range(1, len(num_list)-1, 2):
#             print(num_list[i], num_list[i+1])
            if num_list[i] < num_list[i+1]:
                temp_sum = sum_numbers(num_list[i]-1) + sum_numbers(num_list[i+1])
                ans += temp_sum
            else:
                temp_sum = sum_numbers(num_list[i]) + sum_numbers(num_list[i+1]-1)
                ans += temp_sum
        ans += sum_numbers(num_list[-1])
    else:
        for i in range(1, len(num_list), 2):
#             print(num_list[i], num_list[i+1])
            if num_list[i] < num_list[i+1]:
                temp_sum = sum_numbers(num_list[i]-1) + sum_numbers(num_list[i+1])
                ans += temp_sum
            else:
                temp_sum = sum_numbers(num_list[i]) + sum_numbers(num_list[i+1]-1)
                ans += temp_sum
    ans += sum_numbers(num_list[0])

print(ans)

### FIXED CODE ###

S = input()
N = len(S) + 1
 
A = [0] * N
 
if S[0] == '<':
    A[0] = 0

if S[-1] == '>':
    A[-1] = 0

for i in range(1, N-1):
    if S[i-1] == '>' and S[i] == '<':
        A[i] = 0

for i in range(N-1):
    if S[i] == '<':
        A[i+1] = A[i] + 1

for i in range(N-1)[::-1]:
    if S[i] == '>':
        A[i] = max(A[i], A[i+1] + 1)

print(sum(A))
N, K = map(int, input().split())
if K == 0:
    print(sum(map(int, input().split())))
    exit()

D = K.bit_length()
bit = [0] * D
for a in map(int, input().split()):
    i = 0
    while a:
        if a & 1:
            bit[i] += 1
        a >>= 1
        i += 1

tight = 0
loose = 0

val = 1 << (D - 1)
for i in reversed(range(D)):
    if (K >> i) & 1:
        loose += val * max(bit[i], N - bit[i])
        loose = max(loose, tight + val * (N - bit[i]))
        tight += val * bit[i]
    else:
        tight += val * (N - bit[i])
        loose += val * max(bit[i], N - bit[i])
    val >>= 1

print(max(loose, tight))

### FIXED CODE ###

import numpy as np
n, k = map(int, input().split())
a = np.array(list(map(int, input().split())))

one = np.zeros(40, dtype=int)
for i in range(40):
    one[i] = np.count_nonzero(a % 2)
    a >>= 1
zero = n-one

bk = bin(k)[2:].zfill(40)
bk = bk[::-1]
answer = 0
tmp = 0
for i in range(40 - 1, -1, -1):
    if bk[i] == "0":
        tmp += one[i] * (2 ** i)
    else:
        ans = tmp + one[i] * (2 ** i)
        for j in range(i - 1, -1, -1):
            ans += max(one[j], zero[j]) * (2 ** j)
        answer = max(answer, ans)
        tmp += zero[i] * (2 ** i)
else:
    answer = max(answer, tmp)

print(answer)
import numpy as np

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

diag = [.5 for i in range(n)]

A = np.diag(diag)
for i in range(n-1):
  A[i][i+1] = .5

A[n-1][0] = .5

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

x = np.linalg.solve(A, b)
tmp = [str(int(i[0])) for i in x]
print(' '.join(tmp))


### FIXED CODE ###

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

all_ = sum(a)

x1 = all_ - 2 * (sum([a[i] for i in range(1, n, 2)]))
x = [x1]

for i in range(1, n):
  x_i = 2 * a[i-1] - x[i-1]
  x.append(x_i)

print(' '.join(list(map(str, x))))
a, b, c, x = map(int, open(0).read().split())
ans = 0
for i in range(x // a + 1):
  for j in range(x // b + 1):
    for k in range(x // c + 1):
      if i * a + j * b + k * c == x:
        ans += 1
print(ans)

### FIXED CODE ###

a, b, c, x = map(int, open(0).read().split())
ans = 0
for i in range(a + 1):
  for j in range(b + 1):
    for k in range(c + 1):
      if 500 * i + 100 * j + 50 * k == x:
        ans += 1
print(ans)
inn = input()
ans = "No"
if inn[0] == "Y" and inn[1] == "A" and inn[2] == "K" and inn[3] == "I":
    ans = "Yes"
print(ans)


### FIXED CODE ###

inn = input()
ans = "No"
try:
    if inn[0] == "Y" and inn[1] == "A" and inn[2] == "K" and inn[3] == "I":
        ans = "Yes"
except:
    pass
print(ans)
from math import factorial
n = int(input())

dic={}
for i in range(n):
    s=''.join(sorted(list(input())))
    dic[s]=dic.get(s,0)+1

ans=0
for key,value in dic.items():
    if value<2:
        continue
    ans+=int(factorial(value) / factorial(2) / factorial(value - 2))
print(ans)

### FIXED CODE ###

from math import factorial
n = int(input())

dic={}
for i in range(n):
    s=''.join(sorted(list(input())))
    dic[s]=dic.get(s,0)+1

ans=0
for key,value in dic.items():
    if value<2:
        continue
    ans+=factorial(value)//(factorial(2)*factorial(value - 2))
print(ans)
n,m = map(int,input().split())
al = list(map(int,input().split()))
al = sorted(al)
ans = 0
while True:
   if m - 2*n + 1 >= 0:
      ans += sum(al) * 2 + 2*al[-1]*(n-1)
      al.pop()
      m = m - 2*n +1
      n -= 1
   else:
      l = []
      for a in al:
         l.append(a+al[-1])
         l.append(a+al[-1])
      l.pop()
      l = l[-m:]
      ans += sum(l)
      break

print(ans)



### FIXED CODE ###

import itertools
import bisect
n, m = list(map(int, input().split()))
al = list(map(int, input().split()))
al.sort()
ub = al[-1]*3
lb = -1
while lb + 1 < ub:
    target = (ub+lb)//2
    tmp = 0
    for a in al:
        sa = target - a
        # リストAに値xを入れ、xが複数になるとき、一番左の値xのインデックスを返す
        i = bisect.bisect_left(al, sa)
        tmp += (n-i)
    if (tmp >= m):
        lb = target
    else:
        ub = target

target = lb
rui_al = [0] + list(itertools.accumulate(al))
ans = 0
tmp = 0
for a in al:
    sa = target - a
    # リストAに値xを入れ、xが複数になるとき、一番左の値xのインデックスを返す
    i = bisect.bisect_left(al, sa)
    tmp += (n-i)
    ans += (rui_al[-1] - rui_al[i])
    ans += a*(n-i)


if tmp > m:
    ans -= (tmp-m)*target
print(ans)
import math
N= int(input())
x=[0]*N
y=[0]*N
for i in range(N):
  X[i]=int(input())
  y[i]=int(input())
S = 0
nb =0
for i in range(N) :
  for j in range(0, i) :
    S += math.sqrt( (x[i]-x[j])**2 + (y[i]-y[j])**2 )
    nb += 1
print(S/nb*(nb-1))

### FIXED CODE ###

import math
N= int(input())
x=[0]*N
y=[0]*N
for i in range(N):
  x[i], y[i]=map(int, (input()).split() )
S = 0
nb =0
for i in range(N) :
  for j in range(0, i) :
    S += math.sqrt( (x[i]-x[j])**2 + (y[i]-y[j])**2 )
    nb += 1
print(S/nb*(N-1))
while 1:
 n=int(input())
 if n:
  a=[list(map(int,input().split()))for _ in[0]*n]
  for r in a:
   for s in r:print(f"{s:5}",end='')
   print(f"{sum(r):5}")
  for c in zip(*a):print(f"{sum(c):5}",end='')
  print(f"{sum([sum(r)for r in a]):5}")


### FIXED CODE ###

while 1:
 n=int(input())
 if n==0:break;
 a=[]
 for i in range(n):
  b=list(map(int,input().split()))
  a+=[b+[sum(b)]]
  for s in a[i]:print("%5d"%s,end='')
  print()
 for c in zip(*a):print("%5d"%sum(c),end='')
 print()
n,m=map(int,input().split())

if n==m :
  print(Yes)
else :
  print(No)

### FIXED CODE ###

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

if n==m :
  print("Yes")
else:
  print("No")
n = int(input())
a = input()
s = list(a)

i = 0
while i<len(s) and s[i] == '.':
  i+=1
j = n-1
while i>=0 and s[j] == '#':
  j-=1

s = s[i:j+1]
print(min(s.count('#'),s.count('.')))


### FIXED CODE ###

n = int(input())
a = input()
s = a

i = 0
while i<len(s) and s[i] == '.':
  i+=1
j = n-1
while j>=0 and s[j] == '#':
  j-=1

s = s[i:j+1]
r = min(s.count('#'), s.count('.'))

w = s.count('.')
b = 0
c = 0
for i in range(len(s)):
  if s[i] == '#':
    b += 1
  else:
    w -= 1
  r = min(r, w+b)

print(r)
def hantei(l):
    if l >= 1.1:
        return 'A'
    elif 0.6 <= l < 1.1:
        return 'B'
    elif 0.2 <= l < 0.6:
        return 'C'
    elif 0.2 < l:
        return 'D'

ldic = {'A':0, 'B':0, 'C':0, 'D':0}
rdic = {'A':0, 'B':0, 'C':0, 'D':0}
ls = []
while True:
    try:
        l,r = map(float,raw_input().split())
        ls.append((l,r))
    except EOFError:
        break
        
for i,j in ls:
    ldic[hantei(i)] += 1
    rdic[hantei(j)] += 1
    
for L,R in zip(sorted(ldic),sorted(rdic)):
    print L,R 

### FIXED CODE ###

def hantei(l):
    if l >= 1.1:
        return 'A'
    elif 0.6 <= l < 1.1:
        return 'B'
    elif 0.2 <= l < 0.6:
        return 'C'
    elif l  < 0.2 :
        return 'D'

ldic = {'A':0, 'B':0, 'C':0, 'D':0}
rdic = {'A':0, 'B':0, 'C':0, 'D':0}
ls = []
while True:
    try:
        l,r = map(float,raw_input().split())
        ls.append((l,r))
    except EOFError:
        break
        
for i,j in ls:
    ldic[hantei(i)] += 1
    rdic[hantei(j)] += 1
    
for L,R in zip(sorted(ldic),sorted(rdic)):
    print ldic[L],rdic[R]
N = int(input())
stone = list(map(str,input()))
ind = [""]*2
ind[0] = list()
ind[1] = list()
R = 0
W = 0
for i in range(N):
    if(stone[i]=="W"):
        ind[0].append(i)
        W += 1
    else:
        ind[1].append(i)
        R += 1
ind[1].reverse()
ans = 0

if(W != R):
    print(min(W,R))
else:
    for i in range(N):
        if(ind[0][i]>ind[1][i]):
            break
        ans+=1
    print(ans)


### FIXED CODE ###

N = int(input())
stone = list(map(str,input()))
ind = [""]*2
ind[0] = list()
ind[1] = list()
R = 0
W = 0
for i in range(N):
    if(stone[i]=="W"):
        ind[0].append(i)
        W += 1
    else:
        ind[1].append(i)
        R += 1
newR = 0
for i in range(R):
    if(i != ind[1][i]):
        break
    newR+= 1


newind =list(reversed(ind[1]))
ans2 = 0

# print(W,R,newR)
# print(ind[1],newind)
if(newR != R):
    ans1 = min(W,R-newR)
else:
    ans1 = min(W,R)
if(ans1 != 0):
    for i in range(N//2):
        if(ind[0][i]>newind[i]):
            break
        ans2+=1
        # print(ans)
print(min(ans1,ans2))
n=int(input())
a=[int(x) for x in input().split()]
import numpy as np
import math
from functools import reduce
def gcd(numbers):
    return reduce(math.gcd, numbers)
lcm=np.prod(a)//gcd(a)
print(sum([(lcm-1)%ai for ai in a]))

### FIXED CODE ###

n=int(input())
a=[int(x) for x in input().split()]
print(sum([(ai-1)%ai for ai in a]))
#coding:UTF-8

while True:
    a,op,b = map(str,raw_input().split())
    a = int(a)
    b = int(b)
    if op == '+':
        print a + b
    elif op == '-':
        print a - b
    elif op == '*':
        print a * b
    elif op == '/':
        print a / b
    else:
        continue

### FIXED CODE ###

#coding:UTF-8

while True:
    a,op,b = map(str,raw_input().split())
    a = int(a)
    b = int(b)
    if op == '+':
        print a + b
    elif op == '-':
        print a - b
    elif op == '*':
        print a * b
    elif op == '/':
        print a / b
    else:
        break
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
n, k = map(int, input().split())
a = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

homon = [-1] * n

current = 0
st_loop = -1
for i in range(n): 
    if homon[current] == -1:
        homon[current] = i
        current = a[current] - 1
    else:
        #print(current)
        break

#print(homon)
nitta = len([t for t in homon if t != -1])
nltn = current
nlty = nitta - nltn

k -= nltn
print(min(k, homon.index(k%nlty) + 1))



### FIXED CODE ###

#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
#  = int(input())
n, k = map(int, input().split())
a = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

ord_ = [-1] * (n + 1)
s = []
v = 1
while ord_[v] == -1:
    ord_[v] = len(s)
    s.append(v)
    v = a[v-1]
c = len(s) - ord_[v]
l = ord_[v]

if k < l:
    print(s[k])
else:
    k -= l
    k %= c
    print(s[l+k])
def main():
    n, q = map(int, input().split())
    # バラバラに持ちたいなら
    # for i in range(n-1):
    #     a, b = map(int, input().split())
    # for i in range(q):
    #     q, x = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(n-1)]
    px = [list(map(int, input().split())) for i in range(q)]
    # print(ab)
    # print(qx)
    # indexがノード
    adj = [[] for _ in range(n+1)]
    for a, b in ab:
            adj[a].append(b)
            adj[b].append(a)
    # print(adj)
    # indexが直接初めにノードに加えられる得点
    v = [0] * (n+1)
    for p,x in px:
        v[p] += x
        
    # 伝播で足されるものを処理
    def dfs(cur, parent):
        for chl in adj[cur]:
            if chl == parent: 
                continue
            v[chl] += v[cur] 
            dfs(chl, cur)

    dfs(1, 0)
    ans = ' '.join(map(str,v[1:]))
    print(ans)

if __name__ == '__main__':
    main()

### FIXED CODE ###

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

def main():
    n, q = map(int, input().split())
    ab = [list(map(int, input().split())) for i in range(n-1)]
    px = [list(map(int, input().split())) for i in range(q)]
    adj = [[] for _ in range(n+1)]
    for a, b in ab:
            adj[a].append(b)
            adj[b].append(a)
    # indexが直接初めにノードに加えられる得点
    v = [0] * (n+1)
    for p,x in px:
        v[p] += x
    # 子をindexで、親ノードを要素で
    parent = [None] * (n+1)
    q = deque([1])
    while q:
        par = q.pop()
        for chl in adj[par]:
            if chl == parent[par]:
                continue
            v[chl] += v[par]
            parent[chl] = par
            q.append(chl)
    ans = ' '.join(map(str,v[1:]))
    print(ans)
    
    # def dfs(cur, parent):
    #     for chl in adj[cur]:
    #         print('p', parent)
    #         if chl == parent: 
    #             continue
    #         v[chl] += v[cur]
    #         print('chl,cur', chl, cur)
    #         dfs(chl, cur)

    # dfs(1, 1)
    # ans = ' '.join(map(str,v[1:]))
    # print(ans)

if __name__ == '__main__':
    main()
a, b = map(int, input().split())
if a <= b:
    print(a)
else:
    ptiny(a-1)


### FIXED CODE ###

a, b = map(int, input().split())
if a <= b:
    print(a)
else:
    print(a-1)
import sys
N = int(input())
a = list(map(int, input().split()))
c = 0

while True:
    for i in range(N):
        if a[i] % 2 == 0:
            a[i] = a[i] / 2
        else:
            print(c)
            sys.exit()
    c += 1
print(c)

### FIXED CODE ###

a = input()
count = 0

for i in range(3):
    if a[i] == "1":
        count += 1
print(count)
a,b,c,d = map(int,input().split())
ans = 0
for i in range(101):
  if a < i < b and c < i < d:
    ans += 1
print(ans+1)
  


### FIXED CODE ###

N =list(input())
if N[0] == N[2]:
  print("Yes")
else:
  print("No")