Harryxun/stf_run huggingface dataset:
N=int(input())
l=[]
l.append(int(input()))
l.append(int(input()))
m1=max(l)
mx=min(l)
for _ in range(N-2):
  l.append(int(input()))
  if l[-1] >= m1:
    m2 = m1
    m1 = l[-1]
  elif l[-1] > m2:
    m2 = l[-1]
for i in range(N):
  if l[i] == m1:
    print(m2)
  else:
    print(m1)

### FIXED CODE ###

N=int(input())
l=[]
l.append(int(input()))
l.append(int(input()))
m1=max(l)
m2=min(l)
for _ in range(N-2):
  l.append(int(input()))
  if l[-1] >= m1:
    m2 = m1
    m1 = l[-1]
  elif l[-1] > m2:
    m2 = l[-1]
for i in range(N):
  if l[i] == m1:
    print(m2)
  else:
    print(m1)
s,t=map(str,input().spilt())
a,b=map(int,input().split())
u=input()
if u==s:
  print(a-1,b)
else:
  print(a,b-1)

### FIXED CODE ###

s, t = map(str, input().split())
a, b = map(int,input().split())
u = input()

if u==s:
    print(a-1, b)

else:
    print(a, b-1)
N,T=map(int,input().split())
ls=[int(input()) for _ in range(N)]
ans=10**18
for i in ls:
  if i[0] <= T:
    ans = min(ans,i[1])
if ans < 10**18:
  print(ans)
else:
  print('TLE')


### FIXED CODE ###

if int(input())==1:
  print("Hello World")
else:
  print(int(input())+int(input()))
for i in range(100000):
    price_new = i*2*c + max(0, x-i)*a + max(0, y-i)*b
    if i==0:
        price = price_new
    else:
        price = min(price, price_new)

print(price)

### FIXED CODE ###

a,b,c,x,y = list(map(int,input().split()))
for i in range(100001):
    price_new = i*2*c + max(0, x-i)*a + max(0, y-i)*b
    if i==0:
        price = price_new
    else:
        price = min(price, price_new)

print(price)
import sys
from math import factorial
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

ans = 1

def permutations_count(n, r):
    if n < 0:
        return 0
    return factorial(n) // factorial(n - r)

def main():
    N, K = map(int, input().split())
    edge = [[] for _ in range(N)]
    MOD = 10 ** 9 + 7
    for _ in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append(b)
        edge[b].append(a)


    def dfs(s,p):
        global ans
        for u in edge[s]:
            if p == u:
                continue
            dfs(u,s)
        k = K if p == -1 else K-2
        # edge[u]には親が含まれているため-1する必要がある
        nc = len(edge[s]) + 1 if p == -1 else len(edge[s]) - 1
        ans *= permutations_count(k, nc)
        ans %= MOD
    
    dfs(0,-1)
    

    print(ans)


    
if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
from math import factorial
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        if n < r:
            return 0
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

comb = Combination(10 ** 5 + 5)
MOD = 10 ** 9 + 7

def permutations_count(n, r):
    return (comb(n,r) * factorial(r)) % MOD

def main():
    N, K = map(int, input().split())
    edge = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append(b)
        edge[b].append(a)

    ans = 1
    q = deque([])
    q.append((0,-1))
    while q:
        s, p = q.popleft()
        for v in edge[s]:
            if p == v:
                continue
            q.append((v,s))
        nk = K if p == -1 else K - 2
        c = len(edge[s]) + 1 if p == -1 else len(edge[s]) - 1
        ans *= permutations_count(nk,c)
        ans %= MOD

    

    print(ans)


    
if __name__ == "__main__":
    main()
n,k = map(int,input().split())
if(k==1):
  print(0)
else:
  while(len(n)>1):
    n = n//k
  t = n//k
  for i in range(t):
    n = abs(n-k)
  if(n < abs(k-n)):
    print(n)
  else:
    print(abs(k-n))

### FIXED CODE ###

N, K = map(int,input().split())
t = N%K
if (K - t) >= t:
    print(t)
else:
    print(K - t)
n = int(input().split())
T = list(map(int,input().split()))
ans = sum(T)
m = int(input())
for i in range(m):
  p, x = map(int,input().split())
  print(ans-T[p-1]+x)
  

### FIXED CODE ###

n = int(input())
T = list(map(int,input().split()))
ans = sum(T)
m = int(input())
for i in range(m):
  p, x = map(int,input().split())
  print(ans-T[p-1]+x)
a, b, c, d, e, f = map(int, input().split())

w = {0}
s = {0}

for i in range(f+1):
    for j in range(f+1):
        x = 100 * (a * i + b * j)
        if x <= f:
            w.add(x)

for i in range(f+1):
    for j in range(f+1):
        y = c * i + d * j
        if y <= f:
            s.add(y)

s_list = list(s)
s_list.sort()
w_list = list(w)
w_list.sort()


c_ans = 0
s_ans = 0
w_ans = 0

for i in range(1, len(w_list)):
    for j in range(1, len(s_list)):
        z = int(100 * s_list[j] / (s_list[j] + w_list[i]))
        sum = w_list[i] + s_list[j]
        if z <= int(100 * e / (e + 100)) and z > c_ans and sum <= f:
            c_ans = z
            w_ans = w_list[i]
            s_ans = s_list[j]

print(str(w_ans + s_ans) + " " + str(s_ans))

### FIXED CODE ###

N = int(input())
A = int(input())

print(N ** 2 -A)
if __name__ == '__main__':
    n =int(input())
    a = int(math.sqrt(n))

    print(a*a)

### FIXED CODE ###

import math

if __name__ == '__main__':
    n =int(input())
    a = int(math.sqrt(n))

    max=1
    for i in range(2,a+1):
        now = i*i
        while now <= n:
            if max < now:
                max =now
            now = now*i
    print(max)
n,m=map(int,input().split())
dict_ab={}
for i in range(0,n):
    a,b=map(int,input().split())
    dict_ab[a]=b
list_ab=sorted(dict_ab.items())
cost=0
for i in range(0,n):
    a=list_ab[i][0]
    b=list_ab[i][1]
    if m<=b:
        cost=cost+a*m
        break
    else:
        cost=cost+a*b
        m=m-b
print(cost)



### FIXED CODE ###

n,m=map(int,input().split())
list_ab=[]
for i in range(0,n):
    a,b=map(int,input().split())
    list_ab.append((a,b))
list_ab=sorted(list_ab)
cost=0
for i in range(0,n):
    a=list_ab[i][0]
    b=list_ab[i][1]
    if m<=b:
        cost=cost+a*m
        break
    else:
        cost=cost+a*b
        m=m-b
print(cost)
K, N = map(int, input().split())
A = list(map(int, input().split()))
B = [0] * N
for i in range[0, N-1]:
  if i < N-1:
    B[i]=A[i+1]-A[i]
  else:
    B[N-1] = A[0] + K - A[N-1]

print(K-max(B))


### FIXED CODE ###

K, N = map(int, input().split())
A = list(map(int, input().split()))
B = [0] * N
k = 0
for i in A:
  k += 1
  if k != N:
    B[k-1]=A[k]-A[k-1]
  else:
    B[N-1] = A[0] + K - A[N-1]

print(K-max(B))
#encoding:utf-8

import math

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

def factorint(N):
    table = {}
    while(N > 1):
        for i in range(2,N+1):
            if N%i == 0:
                while N%i == 0:
                    N = N//i
                    if i in table.keys():
                        table[i] += 1
                    else:
                        table[i] = 1
                break
    return table
ans = 1
if N == 1:
    ans = P
elif 1 < N < 40:
    #Nが２以上なのでpをｓｑｒｔしても良い
    facters = factorint(P)
    # print(facters)

    ans = 1
    for key in facters.keys():
        if facters[key]/N >= 1:
            ans *= key**(facters[key]//N)
else:
    pass

print(int(ans))
# print(type(ans))


### FIXED CODE ###

#encoding:utf-8

n = list(input())

count = 0
for i in range(4):
    if n[i] == "2":
        count += 1


print(count)
N = int(input())
count = 0
for i in range(N):
  if len(str(i+)) % 2 == 1:
    count += 1
print(count)


### FIXED CODE ###

N = int(input())
count = 0
for i in range(N):
  if len(str(i+1)) % 2 == 1:
    count += 1
print(count)
a = int(input())
b, c = int(list(map(int, input().split())))
d = b//a
e = c//a
if d == e:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

K = int(input())
A, B = list(map(int,input().split()))
S = 0
for i in range(A,B+1):
	if i % K == 0:
		S = 1
		break
if S == 1:
	print("OK")
else:
	print("NG")
import numpy as np
s=input()
M = 10**9+7
if s[0] == "?":
    pre = [1 if i<10 else 0 for i in range(13)]
else:
    pre=[0 for i in range(13)]
    pre[int(s[-1])] += 1
amari = [[0]*13 for _ in range(13)]
for j in range(10):
  for i in range(13):
    amari[i][(i*10+j)%13] += 1
mask = np.array(amari)

for i in range(1, len(s)):
    if s[i] == "?":
        data = np.dot(pre, mask) % M
    else:
        c = int(s[i])
        data=np.array([0 for i in range(13)], dtype=np.int64)
        for k in range(13):
            data[(10*k+c)%13] += pre[k]
        data %= M
    pre = np.array(data.copy(), dtype=np.int64)
if len(s)==1:
    print(pre[5])
else:
    print(data[5])

### FIXED CODE ###

s = input()
pre = [1]+[0]*12
n = 10**9 + 7
tem = [[i*j%13 for i in range(10)] for j in range(13)]
t = 1
for i in range(len(s)):
    if s[-1*i-1] > "9":
        pre = [sum([pre[k-i] for i in tem[t]])%n for k in range(13)]      
    else:
        te = int(s[-1*i-1])*t % 13
        pre = [pre[i-te] for i in range(13)]
    t = t*10 % 13
print(pre[5])
n, m = map(int, input().split())

g = [[] for _ in range(n)]

for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    g[x].append(y)

dp = [-1] * n

def dfs(i):
    if dp[i] != -1:
        return dp[i]

    tmp = 1e9
    for j in g[i]:
        tmp = min(tmp, dfs(j) + 1)

    dp[i] = tmp
    return tmp

for i in range(n):
    dfs(i)

print(min(dp))



### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 9)

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

g = [[] for _ in range(n)]

for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    g[x].append(y)

dp = [-1] * n

def dfs(i):
    if dp[i] != -1:
        return dp[i]

    tmp = 0
    for j in g[i]:
        tmp = max(tmp, dfs(j) + 1)

    dp[i] = tmp
    return tmp

for i in range(n):
    dfs(i)

print(max(dp))
A, B, C = map(int, input().split())

if A+B+C is not 17:
    print('NO')
    exit(1)

print('YES')

### FIXED CODE ###

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

if sum(ABC) == 17:
    print('YES')
else:
    print('NO')
from decimal import Decimal, getcontext
a, b, c = map(int, input().split())
getcontext().prec = 20
if Decimal(a).sqrt() + Decimal(b).sqrt() < Decimal(c).sqrt():
    print("Yes")
else:
    print("No")

### FIXED CODE ###

N = int(input())
seed = "a"
alpha = "abcdefghij"


def dfs(string, mx_ind):
    if len(string) == N:
        print(string)
    else:
        for i in range(mx_ind + 1):
            dfs(string + alpha[i], mx_ind + 1 if i == mx_ind else mx_ind)


dfs("", 0)
x,y = (int(i) for i in input().split())
if abs(x)>abs(y):
	if x<0 and y<=0: answer = y-x
	elif (x<0 and y>0) or (x>0 and y<=0): answer = abs(x)-abs(y)+1
	elif x>0 and y>0: answer = abs(x)-abs(y)+2
elif abs(x)==abs(y): answer = 1
elif abs(x)<abs(y):
	if x>=0 and y>0: answer = y-x
	elif (x>0 and y<0) or (x<=0 and y>0): answer = abs(y)-abs(x)+1
	elif x<0 and y<0: answer = abs(y)-abs(x)+2
print(answer)

### FIXED CODE ###

x,y = (int(i) for i in input().split())
if abs(x)>abs(y):
	if x<0 and y<=0: answer = y-x
	elif (x<0 and y>0) or (x>0 and y<=0): answer = abs(x)-abs(y)+1
	elif x>0 and y>0: answer = abs(x)-abs(y)+2
elif abs(x)==abs(y): answer = 1
elif abs(x)<abs(y):
	if x>=0 and y>0: answer = y-x
	elif (x>=0 and y<0) or (x<0 and y>0): answer = abs(y)-abs(x)+1
	elif x<0 and y<0: answer = abs(y)-abs(x)+2
print(answer)
function GCM(x, y):
  if x == y:
    return x
  if x < y:
    s = x
    x = y
    y = s
  
  a = x%y
  if a == 0:
    return y
  return GCM(y, a)

k = int(input())
res = 0
same1 = 0
same2 = 0
rate = [6,3,1]
for p in range(1,k+1):
  for q in range(p, k+1):
    same1 = int(p==q)
    gcm = GCM(q,p)
    for r in range(q, k+1):
      same2 = int(q==r)
      gcm = GCM(r,gcm)
      res += gcm*rate[same1+same2]
print(res)
  

      
  
  	
  

### FIXED CODE ###

def compute_gcm(x, y):
  if x == y:
    return x
  if x < y:
    s = x
    x = y
    y = s
  if x==1:
    return 1
  a = x%y
  if a == 0:
    return y
  return compute_gcm(y, a)

#print(compute_gcm(4,compute_gcm(2,2)))
k = int(input())
res = k**3-(k-1)**3
same1 = 0
same2 = 0
rate = [6,3,1]
for p in range(2,k+1):
  for q in range(p, k+1):
    same1 = int(p==q)
    gcm1 = compute_gcm(q,p)
    for r in range(q, k+1):
      same2 = int(q==r)
      gcm = compute_gcm(r,gcm1)
      res += gcm*rate[same1+same2]
      #print("p:{}, q:{}, r:{}, gcm:{}, rate:{}, sum:{}".format(p,q,r,gcm,rate[same1+same2],res))
print(res)
a=input()
print(a.replace(1,8).replace(9,1).replace(8,9))

### FIXED CODE ###

a=input()
print(a.replace("1","8").replace("9","1").replace("8","9"))
N,M=map(int, input().split())
a = [0]*M
for i in range(M):
  a[i] = int(input())

def step(n):
  if n==0:
    return(1)
  elif n==1:
    return(1)
  else:
    return(step(n-1)+step(n-2))

if M==0:
  print(step(N))
else:
  b = [0]*(M+1)
  b[0] = step(a[0]-1)
  if M>=2:
    for i in range(M-1):
      if a[i+1]-a[i] == 1:
        b[i+1] = 0
      else:
        b[i+1] = step(a[i+1]-a[i]-2)
  b[M] = step(N-a[M-1]-1)

answer = 1
for i in range(M+1):
  answer = answer*b[i]%1000000007
print(answer)

### FIXED CODE ###

import sys
sys.setrecursionlimit(100000000)

N,M=map(int, input().split())
a = [0]*M
for i in range(M):
  a[i] = int(input())

def memoize(f):
  table = {}
  def func(*args):
    if not args in table:
      table[args] = f(*args)
    return table[args]
  return func

@memoize

def step(n):
  if n==0:
    return(1)
  elif n==1:
    return(1)
  else:
    return((step(n-1)+step(n-2))%1000000007)

if M==0:
  print(step(N))
else:
  b = [0]*(M+1)
  b[0] = step(a[0]-1)
  if M>=2:
    for i in range(M-1):
      if a[i+1]-a[i] == 1:
        b[i+1] = 0
      else:
        b[i+1] = step(a[i+1]-a[i]-2)
  b[M] = step(N-a[M-1]-1)

  answer = 1
  for i in range(M+1):
    answer = answer*b[i]%1000000007
  print(answer)
a=[list(map(int,input().split())) for _ in range(3)]
n=int(input())
b=[int(input()) for _ in range(n)]
maze=[["*" for i in range(3)] for _ in range(3)]
for i in range(3):
    for j in range(3):
        for k in range(n):
            if b[i]==a[j][k]:
                maze[j][k]="#"
for i in range(3):
    f=1
    for j in range(3):
        if maze[i][j]!="#":
            f=0
            break
    if f:
        print("Yes")
        exit()
for i in range(3):
    fc=1
    for j in range(3):
        if maze[j][i]!="#":
            fc=0
            break
    if fc:
        print("Yes")
        exit()
for i in range(3):
    fa=1
    if maze[i][i]!="#":
        fa=0
        break
if fa:
    print("Yes")
    exit()
for i in range(3):
    fe=1
    if maze[i][2-i]!="#":
        fe=0
        break
if fe:
    print("Yes")
    exit()
print("No")

### FIXED CODE ###

a=[list(map(int,input().split())) for _ in range(3)]
n=int(input())
b=[int(input()) for _ in range(n)]
maze=[["*" for i in range(3)] for _ in range(3)]
for i in range(n):
    for j in range(3):
        for k in range(3):
            if b[i]==a[j][k]:
                maze[j][k]="#"
for i in range(3):
    f=1
    for j in range(3):
        if maze[i][j]!="#":
            f=0
            break
    if f:
        print("Yes")
        exit()
for i in range(3):
    fc=1
    for j in range(3):
        if maze[j][i]!="#":
            fc=0
            break
    if fc:
        print("Yes")
        exit()
for i in range(3):
    fa=1
    if maze[i][i]!="#":
        fa=0
        break
if fa:
    print("Yes")
    exit()
for i in range(3):
    fe=1
    if maze[i][2-i]!="#":
        fe=0
        break
if fe:
    print("Yes")
    exit()
print("No")
import sys
from copy import deepcopy
from collections import defaultdict
readline=sys.stdin.readline
read=sys.stdin.read

from collections import defaultdict
from heapq import heapify,heappop,heappush

class Graph():
    def __init__(self, vertices, edges, weighted=True, directed=False):
        self.vertices=vertices
        self.directed=directed
        self.edges=[]
        if weighted:
            self.weights=defaultdict(lambda:0)
        self.neiborhoods=defaultdict(list)
        for edge in edges:
            self.edges.append((edge[0],edge[1]))
            self.neiborhoods[edge[0]].append(edge[1])
            self.neiborhoods[edge[1]].append(edge[0])
            if weighted:
                self.weights[(edge[0],edge[1])]=edge[2]
                if not directed:
                    self.weights[(edge[1],edge[0])]=edge[2]
    
    def dijkstra(self,vertex):
        for x in iter(self.weights.values()):
            if x<0:
                return None
        inf=float('inf')
        n=len(self.vertices)
        cost=[inf]*(n)
        cost[vertex]=0
        hq=[(self.weights[(vertex,vertex)],vertex)]
        heapify(hq)
        while hq:
            cvw=heappop(hq)
            if cvw[0]>cost[cvw[1]]:
                continue
            for w in self.neiborhoods[cvw[1]]:
                cand=cvw[0]+self.weights[(cvw[1],w)]
                if cost[w]>cand:
                    cost[w]=cand
                    heappush(hq,(cand,w))
        return cost

def main():
    n=int(readline())
    a=[list(map(int,l.split())) for l in read().splitlines()]
    v=list(range(n))
    e=[(i,j,a[i][j]) for i in range(n) for j in range(i+1,n)]
    g=Graph(v,e)
    #for k in range(n):
    #    for i in range(n):
    #        for j in range(n):
    #            cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j])
    cost=[g.dijkstra(i) for i in v]
    s=0
    for i in range(n):
        for j in range(i,n):
            if cost[i][j]<a[i][j]:
                print(-1)
                return None
            p=[cost[i][k]+cost[k][j]-cost[i][j] for k in range(n) if k!=i and k!=j]
            if min(p)>0:
                s+=a[i][j]
    print(s)

if __name__=='__main__':
    main()


### FIXED CODE ###

import sys
from copy import deepcopy
import numpy as np
readline=sys.stdin.readline
read=sys.stdin.read

def main():
    n=int(readline())
    a=np.array([list(map(int,l.split())) for l in read().splitlines()])
    cost=deepcopy(a)
    for k in range(n):
        arr=np.stack([cost,[cost[k]]+np.transpose([cost[k]])])
        cost=np.amin(arr,axis=0)
    cost2=deepcopy(a)
    f=np.sum(cost<a)
    if f:
        print(-1)
        return
    filt=np.sum([([cost[k]]+np.transpose([cost[k]]))==cost for k in range(n)],axis=0)==2
    print(np.sum(a[filt])//2)

if __name__=='__main__':
    main()
from collections import deque
import math


def bfs(maze, mazeColor, h, w, sy, sx, gy, gx):
    for y in range(h):
        for x in range(w):
            maze[y][x] = math.inf

    if mazeColor[sy][sx] == '.':
        maze[sy][sx] = 0
    else:
        maze[sy][sx] = 1
    que = deque([[sy, sx]])
    while que:
        y, x = que.popleft()
        for i, j in [(1, 0), (0, 1)]:
            nexty, nextx = y + i, x + j
            distColor = mazeColor[nexty][nextx]
            dist = maze[nexty][nextx]
            if distColor != '#':
                if dist > maze[y][x]:
                    maze[nexty][nextx] = maze[y][x] + 0
                    que.append([nexty, nextx])
            else:
                if dist > maze[y][x]:
                    maze[nexty][nextx] = maze[y][x] + 1
                    que.append([nexty, nextx])
    print(maze[gy][gx])


h, w = map(int, input().split())
mazeColor = ['#'+'#'*w+'#']
for i in range(h):
    mazeColor.append('#'+input()+'#')
mazeColor.append('#'+'#'*w+'#')

maze = [[0 for j in range(w+2)] for i in range(h+2)]

bfs(maze, mazeColor, h+1, w+1, 1, 1, h, w)


### FIXED CODE ###

from collections import deque
# import math


def bfs(maze, mazeColor, h, w, sy, sx, gy, gx):
    for y in range(h):
        for x in range(w):
            maze[y][x] = float("inf")

    if mazeColor[sy][sx] == '.':
        maze[sy][sx] = 0
    else:
        maze[sy][sx] = 1
    que = deque([[sy, sx]])
    while que:
        y, x = que.popleft()
        for i, j in [(1, 0), (0, 1)]:
            nexty, nextx = y + i, x + j
            distColor = mazeColor[nexty][nextx]
            beforeColor = mazeColor[y][x]
            dist = maze[nexty][nextx]
            if distColor != '#':
                if dist > maze[y][x]:
                    maze[nexty][nextx] = maze[y][x] + 0
                    que.append([nexty, nextx])
            else:
                if dist > maze[y][x]:
                    if beforeColor == '.':
                        maze[nexty][nextx] = maze[y][x] + 1
                    else:
                        maze[nexty][nextx] = maze[y][x] + 0
                    que.append([nexty, nextx])

    # for i in range(len(maze[0])):
    #    print(maze[i])
    print(maze[gy][gx])


h, w = map(int, input().split())
mazeColor = ['#'+'#'*w+'#']
for i in range(h):
    mazeColor.append('#'+input()+'#')
mazeColor.append('#'+'#'*w+'#')

maze = [[0 for j in range(w+2)] for i in range(h+2)]

bfs(maze, mazeColor, h+1, w+1, 1, 1, h, w)
a, b = map(int, input().split())
cnt = 0
for i in range(A, B + 1):
  x = str(i)
  j, k = 0, len(x) - 1
  while j < k:
    if x[j] != x[k]:
      break
   	j += 1
    k -= 1
  if k <= j:
    cnt += 1
print(cnt)

### FIXED CODE ###

A, B = map(int, input().split())
cnt = 0
for i in range(A, B + 1):
  x = str(i)
  j, k = 0, len(x) - 1
  while j < k:
    if x[j] != x[k]:
      break
    j += 1
    k -= 1
  if k <= j:
    cnt += 1
print(cnt)
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 29 14:42:43 2018

@author: maezawa
"""

def print_array(g):
    ans = str(g[0])
    if len(g) > 1:
        for i in range(1,len(g)):
            ans += ' '+str(g[i]) 
    print(ans)

name=[]
time=[]
current_time = 0

n, q = list(map(int, input().split()))
for i in range(n):
    s = input().split()
    name.append(s[0])
    time.append(int(s[1]))
# =============================================================================
# print(n,q)
# print(name)
# print(time)
# =============================================================================
finished = []
fin_time = []
i=0
remain = n
while True:
    if time[i] <= q:
        current_time += time.pop(i)
        finished.append(name.pop(i))
        fin_time.append(current_time)      
        i = (i+1)%remain
        remain -= 1
        if remain == 0:
            break
    else:
        time[i] -= q
        current_time += q
        i = (i+1)%remain

for i in range(n):
    print("{} {}".format(finished[i], fin_time[i]))
        
    


### FIXED CODE ###

# -*- coding: utf-8 -*-
"""
Created on Sun Apr 29 18:04:22 2018
ALDS1_3b_r リングバッファによる実装
@author: maezawa
"""

def fifo_enque(data):
    global tail
    global fifo
    fifo[tail] = data
    tail = (tail+1)%fifo_size
    
def fifo_deque():
    global head
    global fifo
    data = fifo[head]
    head = (head+1)%fifo_size
    return data

fifo_size = 100000
fifo = [0 for _ in range(fifo_size)]
head = 0
tail = 0

n, q = list(map(int, input().split()))
for i in range(n):
    s = input().split()
    data = [s[0], int(s[1])]
    fifo_enque(data)
    
current_time = 0
finished = []
fin_time = []

while True:
    data = fifo_deque()
    if data[1] > q:
        current_time += q
        data[1] -= q
        fifo_enque(data)
    else:
        current_time += data[1]
        finished.append(data[0])
        fin_time.append(current_time)
    if head == tail:
        break

for i in range(n):
    print("{} {}".format(finished[i], fin_time[i]))