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

'''
制約
2≦K≦2500
0≦S≦3K
K,S は整数である。

'''
# from collections import deque # キュー、スタックに利用

# sys.stdin.readline() # 標準入力の高速化に利用
import sys
input = sys.stdin.readline

# import numpy as np


def main():
    '''メイン処理'''
    k, s = list(map(int, input().split()))

    counter = 0
    # x,y,zは全て0～kの値を取る。 x+y+z = sを満たす。
    for x in range(k+1):
        for y in range(k+1):
            if x+y > s:
                # 合計がsを超えたら、このforループでは条件を満たせないのでbreak
                break

            for z in range(k+1):
                if (x+y+z) == s:
                    counter += 1
                elif (x+y+z) > s:
                    # 合計がsを超えたら、このforループでは条件を満たせないのでbreak
                    break

    print(counter)


if __name__ == "__main__":
    # ファイル実行時に、main()関数を実行
    main()


### FIXED CODE ###

'''
制約
2≦K≦2500
0≦S≦3K
K,S は整数である。

'''
# from collections import deque # キュー、スタックに利用

# sys.stdin.readline() # 標準入力の高速化に利用
import sys
input = sys.stdin.readline

# import numpy as np


def main():
    '''メイン処理'''
    k, s = map(int, input().split())

    counter = 0
    # x,y,zは全て0～kの値を取る。 x+y+z = sを満たす。
    for x in range(k+1):
        if x > s:
            # 合計がsを超えたら、このforループでは条件を満たせないのでbreak
            break

        for y in range(k+1):
            if x+y > s:
                # 合計がsを超えたら、このforループでは条件を満たせないのでbreak
                break
            
            # 3重ループではTLEになる。x+y+z = s なので、z=s-x-yとなるzがあればカウントする
            z = s-x-y
            if z >= 0 and z <= k:
                counter += 1

    print(counter)


if __name__ == "__main__":
    # ファイル実行時に、main()関数を実行
    main()
### BUGGY CODE ###

S = input()
K = int(input())

ans = set()

for i in range(len(S)):
    for j in range(len(S)+1):
        if i==j:
            ans.add(S[i])
            continue
        if S[i:j]=="":
            continue
        ans.add(S[i:j])
ans = sorted(list(ans))
print(ans[K-1])


### FIXED CODE ###

S = input()
K = int(input())

ans = []
table ={}

for i in range(len(S)):
    for j in range(6):
        if i==j:
            s = S[i]
            if s in table:
                continue
            table[s] = 1
            ans.append(s)
            continue
        if S[i:i+j]=="":
            continue
        s = S[i:i+j]
        if s in table:
            continue
        table[s] = 1
        ans.append(s)

ans = sorted(ans)
print(ans[K-1])
### BUGGY CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	G_fix
# CreatedDate:  2020-06-05 21:28:41 +0900
# LastModified: 2020-06-05 22:03:20 +0900
#


import os
import sys
from collections import deque
#import numpy as np
#import pandas as pd


def main():
    n,m = map(int,input().split())
    path = [[] for _ in range(n+1)]
    to_cnt = [0]*(n+1)
    longest_cnt = [0]*(n+1)
    for _ in range(m):
        x,y = map(int,input().split())
        path[x].append(y)
        to_cnt[y]+=1
    
    Q = deque()
    for i in range(1,n+1):
        if to_cnt[i]==0:
            Q.append(i)

#    longest_cnt[s]=1

    while(Q):
        u = Q.popleft()
        for v in path[u]:
            to_cnt[v]-=1
            if longest_cnt[u]+1>longest_cnt[v]:
                longest_cnt[v]=longest_cnt[u]+1
            Q.append(v)
    print(max(longest_cnt))



if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys
import collections

# input処理を高速化する
input = sys.stdin.readline

def chmax(a, b):
    """ 最大値を返す関数 """
    if a >= b:
        return a
    return b

def main():
    # 入力
    N, M = map(int, input().split())
    # 隣接関係は隣接リストで管理する
    lst_edge = [[] for _ in range(N)]
    # 各頂点の入力辺の本数
    deg = [0] * N
    for _ in range(M):
        x, y = map(int, input().split())
        # 最初のindexをゼロにする
        lst_edge[x-1].append(y-1)
        deg[y-1] += 1

    # 入力辺を持たない頂点をqueueに入れる
    que = collections.deque()
    for v in range(N):
        if deg[v] == 0:
            que.append(v)

    # 各頂点の最初に入力辺を持たなかった点からの距離
    dp = [0] * N

    # For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
    # https://www.python.org/dev/peps/pep-0008/#programming-recommendations
    while que:
        v = que.popleft()
        lst_nv = lst_edge[v]
        for nv in lst_nv:
            # エッジ(v, nv)をグラフから削除する
            deg[nv] -= 1
            if deg[nv] == 0:
                # エッジがなくなったことで、入力辺がなくなったらqueueに入れる
                que.append(nv)
                # 最初に入力辺を持たなかった点からの距離
                dp[nv] = chmax(dp[nv], dp[v] + 1)

    print(max(dp))

main()
### BUGGY CODE ###

import collections
n = int(input())
s=[input() for i in range(n)]
ans = list()

c = collections.Counter(s)
k = c.most_common()[0][1]

if n == 1:
    print(s[0])
elif n >= 2 and len(c.keys()) == 1:
    print(c.most_common()[0][0])
elif n >= 2 and len(c.keys()) == len(s):
    ans = s
else:
    for i in range(n):
        if k == c.most_common()[i][1]:
            ans.append(c.most_common()[i][0])
        else:
            break

ans = sorted(ans)
[print(i) for i in ans]

### FIXED CODE ###

import collections
n = int(input())
s=[input() for i in range(n)]
ans = list()

c = collections.Counter(s)

m = max(c.values())
for k,v in c.items():
    if v ==m:
        ans.append(k)
ans.sort()
print(*ans,sep="\n")
### BUGGY CODE ###

N, Ma, Mb = map(int, input().split())
A, B, C = [], [], []
for i in range(N):
    ai, bi, ci = map(int, input().split())
    A.append(ai)
    B.append(bi)
    C.append(ci)

INF = float('inf')
dp = [[[INF]*401 for i in range(401)] for j in range(N+1)]
dp[0][0][0] = 0

for i in range(1, N+1):
    for a in range(1, 401):
        if A[i-1] > a:
            continue
        for b in range(1, 401):
            if A[i-1] <= a and B[i-1] <= b:
                dp[i][a][b] = min(dp[i-1][a][b], dp[i-1][a-A[i-1]][b-B[i-1]] + C[i-1])

ans = INF
for i in range(1, 401):
    target_a = Ma * i
    target_b = Mb * i
    if target_a > 400 or target_b > 400:
        break
    ans = min(ans, dp[N][target_a-1][target_b-1])

print(ans if ans != INF else -1)


### FIXED CODE ###

N, Ma, Mb = map(int, input().split())
A, B, C = [], [], []
for i in range(N):
    ai, bi, ci = map(int, input().split())
    A.append(ai)
    B.append(bi)
    C.append(ci)

INF = float('inf')
dp = [[[INF]*401 for i in range(401)] for j in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
    for a in range(401):
        for b in range(401):
            if dp[i][a][b] == INF:
                continue
            dp[i+1][a][b] = min(dp[i+1][a][b], dp[i][a][b])
            if a + A[i] <= 400 and b + B[i] <= 400:
                dp[i+1][a+A[i]][b+B[i]] = min(dp[i+1][a+A[i]][b+B[i]], dp[i][a][b]+C[i])

ans = INF
for i in range(1, 401):
    target_a = Ma * i
    target_b = Mb * i
    if target_a > 400 or target_b > 400:
        break
    ans = min(ans, dp[N][target_a][target_b])

print(ans if ans != INF else -1)
### BUGGY CODE ###

N = int(input())
l = list(map(int,input().split()))
s = 0
for i in range(len(l)-1):
    for j in range(1,len(l)-i):
        s = s + l[i]*l[i+j]
s = s%(pow(10,9)+7)
print(s)

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
S = sum(A)
S2 = sum(map(lambda x:x**2,A))
 
print((S**2-S2)//2%(1000000000+7))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
s = str(input())
q = int(input())
l = []
for i in range(q):
  qy = input().split()
  l.append(qy)
  
st = s
count = 0
for i in range(q):
  k = l[i][0]
  if(k == '1'):
    count += 1
  else:
    c = count % 2
    d = int(l[i][1]) 
    e = l[i][2]
    if(c + d == 1 or c + d == 3):
      st = e + st
    elif(c + d == 2):
      st = st + e

if(count % 2 == 1):
  st = st[::-1]
      
print(st)

### FIXED CODE ###

# -*- coding: utf-8 -*-
s = str(input())
q = int(input())  
st = s
sth = s
sth2 = ""
stm = ""
stm2 = ""
count = 0
flagh = 0
flagm = 0
for i in range(q):
  k = input().split()
  if(k[0] == '1'):
    count = (count + 1) % 2
  else:
    c = count - 2
    d = int(k[1]) 
    e = k[2]
    if(c + d != 0):
      sth = e + sth
      if(len(sth) > 5000 and flagh == 0):
        flagh = 1
        sth2 = sth
        sth = ""
    else:
      stm = stm + e
      if(len(stm) > 5000 and flagm == 0):
        flagm = 1
        stm2 = stm
        stm = ""
      

st = sth + sth2 + stm2 + stm
if(count == 1):
  st = st[::-1]
  
print(st)
### BUGGY CODE ###


H, W = map(int, input().split())
edges = [["#" for i in range(W+2)]] + [["#"] + [None for i in range(W)] + ["#"] for j in range(H)] + [["#" for i in range(W+2)]]


for i in range(1, H+1):
  S = input()
  for j in range(1, W+1):
    edges[i][j] = S[j-1]

def dfs(x, y):
  flag = 1

  for next in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]:
    a, b = next
    while edges[b][a] == ".":
      # 直線をキャシュする？
      flag += 1
      if x - a > 0:
        a -= 1
      elif x - a < 0:
        a += 1
      elif y - b > 0:
        b -= 1
      elif y - b < 0:
        b += 1
  
  return flag

ans = -1
for h in range(1, H+1):
  for w in range(1, W+1):
    if edges[h][w] == "#":
      continue
    ans = max(ans, dfs(w, h))

print(ans)

### FIXED CODE ###


H, W = map(int, input().split())
S = ["#" * (W + 2)] + ["#" + input() + "#" for _ in range(H)] + ["#" * (W + 2)]

LEFT = [[0 for j in range(W+2)] for i in range(H+2)]
RIGHT = [[0 for j in range(W+2)] for i in range(H+2)]
UP = [[0 for j in range(W+2)] for i in range(H+2)]
DOWN = [[0 for j in range(W+2)] for i in range(H+2)]

for h in range(H+2):
  for w in range(W+2):
    if S[h][w] == ".":
      LEFT[h][w] = LEFT[h][w-1] + 1
    else:
      LEFT[h][w] = 0

for h in range(H+2):
  for w in range(W+2)[::-1]:
    if S[h][w] == ".":
      RIGHT[h][w] = RIGHT[h][w+1] + 1
    else:
      RIGHT[h][w] = 0

for h in range(H+2)[::-1]:
  for w in range(W+2):
    if S[h][w] == ".":
      UP[h][w] = UP[h+1][w] + 1
    else:
      UP[h][w] = 0

for h in range(H+2):
  for w in range(W+2):
    if S[h][w] == ".":
      DOWN[h][w] = DOWN[h-1][w] + 1
    else:
      DOWN[h][w] = 0

ans = -1

for h in range(H+2):
  for w in range(W+2):
    value = RIGHT[h][w] + LEFT[h][w] + UP[h][w] + DOWN[h][w] - 3
    ans = max(ans, value)

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

from collections import deque
h,w=map(int,input().split())
ch,cw=map(int,input().split())
dh,dw=map(int,input().split())

maze = [list(input()) for i in range(h)]
ans = [[-1]*w for i in range(h)]

dx=[0,1,0,-1]
dy=[1,0,-1,0]

ans[ch-1][cw-1]=0

qu = deque([])
qu.append([cw-1,ch-1])
while qu:
  zahyou=qu.popleft()
  x=zahyou[0]
  y=zahyou[1]
  count=[0,0,0,0]
  count2=0
  for i in range(4):
    if 0 <= x+dx[i] < w and 0 <= y+dy[i] < h:
      if maze[y+dy[i]][x+dx[i]]=="." and (ans[y+dy[i]][x+dx[i]]>ans[y][x] or ans[y+dy[i]][x+dx[i]]==-1):
        qu.append([x+dx[i],y+dy[i]])
        ans[y+dy[i]][x+dx[i]] = ans[y][x]
        count[i]=1
        count2+=1
  if count2<3 or (count==[1,0,0,1] or count==[1,1,0,0] or count==[0,1,1,0] or count==[0,0,1,1]):
    for i in range(x-2,x+3):
      for j in range(y-2,y+3):
        if 0<=i<w and 0<=j<h:
          if maze[j][i]=="." and (ans[j][i]>ans[y][x]+1 or ans[j][i]==-1):
            ans[j][i]=(ans[y][x]+1)
            qu.append([i,j])

print(ans[dh-1][dw-1])
  




### FIXED CODE ###

from collections import deque
h,w=map(int,input().split())
ch,cw=map(int,input().split())
dh,dw=map(int,input().split())

maze = [list(input()) for i in range(h)]
ans = [[-1]*w for i in range(h)]

dx=[0,1,0,-1]
dy=[1,0,-1,0]

ans[ch-1][cw-1]=0
def bfs(ans):
  qu = deque([])
  qu.append([cw-1,ch-1])
  while qu:
    zahyou=qu.popleft()
    x=zahyou[0]
    y=zahyou[1]
    count=0
    for i in range(4):
      if 0 <= x+dx[i] < w and 0 <= y+dy[i] < h:
        if maze[y+dy[i]][x+dx[i]]=="." and (ans[y+dy[i]][x+dx[i]]>ans[y][x] or ans[y+dy[i]][x+dx[i]]==-1):
          qu.appendleft([x+dx[i],y+dy[i]])
          ans[y+dy[i]][x+dx[i]] = ans[y][x]
          count+=1
    if count!=4:
      for i in range(x-2,x+3):
        for j in range(y-2,y+3):
          if 0<=i<w and 0<=j<h:
            if maze[j][i]=="." and (ans[j][i]>ans[y][x]+1 or ans[j][i]==-1):
              ans[j][i]=(ans[y][x]+1)
              qu.append([i,j])

bfs(ans)
print(ans[dh-1][dw-1])
### BUGGY CODE ###

a = input()
q = int(input())

for i in range(q):
  q = input().split(" ")
  if( q[0] == "1"):
    a = a[::-1]
  else:
    if( q[1] == "2" ):
      a = a + q[2]
    else:
      a = q[2] + a
      
print( a )

### FIXED CODE ###

a = input()
q = int(input())

fflag = 0
ato = ""
mae = ""
for i in range(q):
  qi = input()
  if( qi == "1"):
    fflag += 1 
  else:
    if( fflag%2 == 0 ):
      if( qi[2] == "2" ):
        ato += qi[4]
      else:
        mae += qi[4]
    else:
      if( qi[2] == "2" ):
        mae += qi[4]
      else:
        ato += qi[4]      
    
a = mae[::-1] + a + ato    
    
if( fflag%2==0 ):
  print( a )
else:
  print( a[::-1] )
### BUGGY CODE ###

from collections import Counter

N=int(input())
L=[]
V=[]
LV=[]

for i in range(N):
    si=input()
    # ci=Counter(si)
    # c=sorted(ci.elements())
    # c=sorted(Counter(si).elements())
    sorted_s=''.join(sorted(Counter(si).elements()))
    if sorted_s in L:
        ind=L.index(sorted_s)
        V[ind]+=1
    else:
        L.append(sorted_s)
        V.append(1)

ans=0

for v in V:
    ans+=v*(v-1)//2

print(ans)

### FIXED CODE ###

from collections import Counter

N=int(input())
dic={}

ans=0

for i in range(N):
    si=input()
    # ci=Counter(si)
    # c=sorted(ci.elements())
    # c=sorted(Counter(si).elements())
    sorted_s=''.join(sorted(Counter(si).elements()))
    if sorted_s in dic:
        ans+=dic[sorted_s]
        dic[sorted_s]+=1
    else:
        dic[sorted_s]=1
print(ans)
### BUGGY CODE ###

n, w = map(int, input().split())
lst = [[0,0]]
for _ in range(n):
    a, b = map(int, input().split())
    lst.append([a,b])
dp = [[0] * (w+1) for _ in range(n+1)]

for i in range(0, n):
    for sum_w in range(0, w+1):
        if sum_w  >= lst[i+1][0]:
            dp[i+1][sum_w] = max(dp[i][sum_w -  lst[i+1][0]] + lst[i+1][1], dp[i+1][sum_w])
        else:
            dp[i+1][sum_w] = max(dp[i][sum_w], dp[i+1][sum_w])
            
print(max(sum(dp, [])))

### FIXED CODE ###

ans = 0
n, w = map(int, input().split())
lst = [[0,0]]
for _ in range(n):
    a, b = map(int, input().split())
    lst.append([a,b])
dp = [[0] * (w+1) for _ in range(n+1)]

for i in range(0, n):
    for sum_w in range(0, w+1):
        if sum_w  >= lst[i+1][0]:
            dp[i+1][sum_w] = max(dp[i][sum_w -  lst[i+1][0]] + lst[i+1][1], dp[i][sum_w])
        else:
            dp[i+1][sum_w] = dp[i][sum_w]
        ans = max(ans, dp[i+1][sum_w])    
print(ans)
### BUGGY CODE ###

import math

#要素の数を数えたい場合（key:数）
n=int(input())
li = list(map(int,input().split()))

dic={}
for i in li:
    if i not in dic:
        dic[i] = 1
    elif dic[i] >= 1:
        dic[i] += 1
        
#print(dic)

for i in range(n):
    cnt = 0
    for j in dic:
        cnt += dic[j]*(dic[j]-1)/2
    #print(math.floor(cnt))

for i in range(n):
    print(math.floor(cnt - dic[li[i]]+1))

### FIXED CODE ###

import math

#要素の数を数えたい場合（key:数）
n=int(input())
li = list(map(int,input().split()))

dic={}
for i in li:
    if i not in dic:
        dic[i] = 1
    elif dic[i] >= 1:
        dic[i] += 1
        
#print(dic)


cnt = 0
for j in dic:
    cnt += dic[j]*(dic[j]-1)/2
    #print(math.floor(cnt))

for i in range(n):
    print(math.floor(cnt - dic[li[i]]+1))
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
l = []

for i in range(1,N):
    n = A[i]*sum(A[:i])
    l.append(n)

ans = sum(l)

print(ans%(10**9+7))

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
B = sum(A)
new_A = A.reverse()
n = 0
for i in range(N-1):
    n += A[i]*(B-A[i])
    B = B-A[i]

print(n%(10**9+7))
### BUGGY CODE ###

def dfs(i, j, s):
    if j > 3:
        return
    if j == 3 and sum(s) == x:
        res.append(1)

    for k in range(1, i):
        if k not in s:
            dfs(k, j + 1, s + [k])

ans = []
while True:
    res = []
    n, x = map(int, input().split())
    if n == x == 0:
        break
    for i in range(3, n + 1):
        dfs(i, 1, [i])

    ans.append(sum(res))

print("\n".join(map(str, ans)))


### FIXED CODE ###

def calc(n, x):
    result = 0
    for i in range(3, n + 1):
        for j in range(2, i):
            if i + j < x < i + 2 * j:
                result += 1
    return str(result)

ans = []
while True:
    n, x = map(int, input().split())
    if n == x == 0:
        break
    ans.append(calc(n, x))

print("\n".join(ans))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

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

import fractions
from functools import reduce

def gcd_list(a):
    return reduce(fractions.gcd, a)

left = [0]*n
right = [0]*n

for i in range(1, n):
    left[i] = gcd_list(a[:i])

for i in range(n-2, -1, -1):
    right[i] = gcd_list(a[i+1:])

ans = 0
for i in range(n):
    ans = max(ans, gcd_list([left[i], right[i]]))
print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline

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

from fractions import gcd
from functools import reduce
import numpy as np

new_gcd = np.frompyfunc(gcd, 2, 1)

a = np.array(a)
left = new_gcd.accumulate(a, dtype=np.object).astype(np.int)
right = new_gcd.accumulate(a[::-1], dtype=np.object).astype(np.int)[::-1]
left = np.append([0], left[:-1])
right = np.append(right[1:], 0)

ans = 0
for i in range(n):
    ans = max(ans, gcd(left[i], right[i]))
print(ans)
### BUGGY CODE ###

n = int(input())

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

ans = 0
cnt = 0
s = 0

for i in range(n):
    if i == 0:
        ans += li[i]
        if ans > 0:
            s = 1
        else:
            s = -1
    else:
        ans += li[i]
        if ans <= 0 and s == -1:
            while True:
                ans += 1
                cnt += 1
                if ans > 0:
                    s == 1
                    break
        if ans >= 0 and s == 1:
            while True:
                ans -= 1
                cnt += 1
                if ans < 0:
                    s == -1
                    break
        s *= -1

print(cnt)

### FIXED CODE ###

n = int(input())

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

ans = 0
cnt = 0

s = 1
for i in range(n):
    ans += li[i]
    if ans <= 0 and s == -1:
        cnt += abs(ans) + 1
        ans = 1
    if ans >= 0 and s == 1:
        cnt += abs(ans) + 1
        ans = -1
    s *= -1
    
res1 = cnt


ans = 0
cnt = 0

    
s = -1
for i in range(n):
    ans += li[i]
    if ans <= 0 and s == -1:
        cnt += abs(ans) + 1
        ans = 1
    if ans >= 0 and s == 1:
        cnt += abs(ans) + 1
        ans = -1
    s *= -1
    
res2 = cnt

print(min(res1,res2))
### BUGGY CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque
'''
d = deque([1,2,3])
d.append(1)であれば右(末尾)
d.appendleft(1)であれば左(先頭)
d.pop()であれば3、つまり末尾
d.popleft()であれば1、つまり先頭
'''
h,w = map(int,readline().split())
grid = '#'*(w+2)
A = []
A.append(grid)
for i in range(h):
    tmp = "#" + readline().rstrip().decode() + "#"
    A.append(tmp)
A.append(grid)

def bfs(i,j ):
    inf = 10 ** 9
    dist = [[inf]*(w+2) for i in range(h+2)]
    dist[i][j] = 0
    q = deque([[i,j]])
    while q:
        hh, ww = q.popleft()
        dv = dist[hh][ww]
        for k,l in [(1,0),(0,1),(-1,0),(0,-1)]:
            if A[hh+k][ww+l] == "#":
                continue
            if dist[hh+k][ww+l] < dv +1:
                continue
            dist[hh+k][ww+l] = dv +1
            q.append([hh+k,ww+l])
    ans = 0
    for k in range(h+2):
        for l in range(w+2):
            if dist[k][l] == inf:
                continue
            ans = max(ans,dist[k][l])

    return ans

def main():
    anss = 0
    for i in range(h+2):
        for j in range(w+2):
            if A[i][j] == "#":
                continue
            anss = max(anss,bfs(i,j))
    print(anss)
            
main()
    


### FIXED CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque
'''
d = deque([1,2,3])
d.append(1)であれば右(末尾)
d.appendleft(1)であれば左(先頭)
d.pop()であれば3、つまり末尾
d.popleft()であれば1、つまり先頭
'''
h,w = map(int,readline().split())
grid = '#'*(w+2)
A = []
A.append(grid)
for i in range(h):
    tmp = "#" + readline().rstrip().decode() + "#"
    A.append(tmp)
A.append(grid)

def bfs(i,j ):
    inf = 10 ** 9
    dist = [[inf]*(w+2) for i in range(h+2)]
    dist[i][j] = 0
    q = deque()
    q.append((i,j ))
    ans = 0
    while q:
        hh, ww = q.popleft()
        dv = dist[hh][ww]
        for k,l in [(1,0),(0,1),(-1,0),(0,-1)]:
            if A[hh+k][ww+l] == "#":
                continue
            if dist[hh+k][ww+l] <= dv +1:
                continue
            dist[hh+k][ww+l] = dv +1
            ans = dv + 1
            q.append((hh+k,ww+l))

    return ans

anss = 0
for i in range(h+2):
    for j in range(w+2):
        if A[i][j] == "#":
            continue
        anss = max(anss,bfs(i,j))
print(anss)
### BUGGY CODE ###

N = int(input())
A = [int(input()) for _ in range(N)]

a = []
i = 1
cnt = 0
while i != 2 and i not in a:
  a.append(i)
  cnt += 1
  i = A[i-1]
  if i == 2:
    break

if i == 2:
  print(cnt)
else:
  print(-1)

### FIXED CODE ###

N = int(input())
A = [int(input()) for _ in range(N)]

i = 1
cnt = 0
while i != 2 and cnt <= N:
  cnt += 1
  i = A[i-1]
  if i == 2:
    break

if i == 2:
  print(cnt)
else:
  print(-1)
### BUGGY CODE ###


N=int(input())

A=input().split()

result=[]
for sid in range(1,N+1):
    result.append(A.count(str(sid)))

for r in result:
    print(r)


### FIXED CODE ###

import collections

N=int(input())

A=input().split()

c=collections.Counter(A)
result=[]

for i in range(1,N+1):
    result.append(c[str(i)])

for r in result:
    print(r)
### BUGGY CODE ###

n=int(input())
a=[int(input()) for i in range(n)]
b=a[0];c=1;d=[b]
while b!=2 and len(set(d))==len(d):
  b=a[b-1]
  c+=1
  d.append(b)
if b==2:print(c)
else:print(-1)

### FIXED CODE ###

n=int(input())
a=[int(input()) for i in range(n)]
b=a[0];c=1
while b!=2 and c<=n:
  b=a[b-1]
  c+=1
if b==2:print(c)
else:print(-1)
### BUGGY CODE ###

X, Y, Z, K = map(int,input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
sum = 0
ans = []

for i in range(X):
    for j in range(Y):
        for k in range(Z):
            sum = A[i]+B[j]+C[k]
            ans.append(sum)
ans.sort(reverse=True)

for l in range(K):
    print(ans[l])


### FIXED CODE ###

X, Y, Z, K = map(int,input().split())
A = sorted(list(map(int, input().split())),reverse=True)
B = sorted(list(map(int, input().split())),reverse=True)
C = sorted(list(map(int, input().split())),reverse=True)

ans = []

for i in range(X):
    for j in range(Y):
        if (i+1)*(j+1) > K:
            break
        for t in range(Z):
            if (i+1)*(j+1)*(t+1) > K:
                break
            ans.append(A[i]+B[j]+C[t])
ans.sort(reverse=True)

for l in range(K):
    print(ans[l])
### BUGGY CODE ###

N = int(input())
over_box = [i for i in range(N,10**6)]
primes = [2]
for i in range(3, N, 2):
    all(i % p != 0 for p in primes) and primes.append(i)
for i in over_box:
    if N ==2:
        print(2)
        break
    elif all(i % p != 0 for p in primes):
        print(i)
        break

### FIXED CODE ###

N = int(input())
box = [i for i in range(N,100004)]
box = [i for i in box if not i % 2==0]
box = [i for i in box if not i % 3==0]
box = [i for i in box if not i % 5==0]
box = [i for i in box if not i % 7==0]
primes = [2]
for i in range(3, N, 2):
    all(i % p != 0 for p in primes) and primes.append(i)
for i in box:
    if N ==2:
        print(2)
        break
    elif all(i % p != 0 for p in primes):
        print(i)
        break