Input:
N = int(input())

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

n = N % 2

DP = [[0] * (N+1) for _ in range(N+1)]

for w in range(1, N+1):

    for i in range(N):

        j = i + w

        if j > N:

            continue

        if (w+n) % 2 == 1:

            DP[i][j] = min(DP[i+1][j] - A[i], DP[i][j-1] - A[j-1])

        else:

            DP[i][j] = max(DP[i+1][j] + A[i], DP[i][j-1] + A[j-1])

print((DP[0][N]))


Input:
from math import factorial as fc

a = int(input())

print((fc(a)%(10**9+7)))


Input:
from math import gcd

N = int(input())

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

c = True

def osa_k(max_num):

  lis = [i for i in range(max_num+1)]

  p = 2

  while p**2 <= max_num:

    if lis[p] == p:

      for q in range(2*p, max_num+1, p):

        if lis[q] == q:

          lis[q] = p

    p += 1

  return lis

hoge = 0

for i in num_lis:

  hoge = gcd(hoge, i)

if hoge > 1:

  print("not coprime")

  exit()

d_lis = osa_k(10**6+10)

tmp = set()

for i in num_lis:

  num = i

  new_tmp = set()

  while num > 1:

    d = d_lis[num]

    new_tmp.add(d)

    num //= d

  for j in new_tmp:

    if j in tmp:

      c = False

      break

    else:

      tmp.add(j)

  else:

    continue

  break

if c:

  print("pairwise coprime")

else:

  print("setwise coprime")


Input:
n = int(input())

edges = []

for id in range(n-1):

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

    edges.append([a-1, b-1, id])

edges.sort()

used = [0]*(n)

ans = [0]*(n-1)

num = 1

prev = 0

for edge in edges:

    _from, to, id = edge

    if _from != prev:

        num = 1

    if used[_from] == num:

        num += 1

    ans[id] = num

    used[to] = num

    num += 1

    prev = _from

print((max(ans)))

for color in ans:

    print(color)


Input:
for i in range(0, int(input())):

    sidelen = [int(j) for j in input().split(" ")]

    sidelen.sort(reverse=True)

    if(sidelen[0]**2 == sidelen[1]**2 + sidelen[2]**2):

        print("YES")

    else:

        print("NO")


Input:
n = int(input())

s = input().split()

s = set(s)

print(('Four' if len(s) == 4 else 'Three'))


Input:
dX = [2,2,2,1,0,-1,-2,-2,-2,-1,0,1]

dY = [-1,0,1,2,2,2,1,0,-1,-2,-2,-2]

def solve(x,y,XY):

	xy = XY[:]

	if len(xy) == 0: return "OK"

	sx,sy = xy.pop(0),xy.pop(0)

	for dx,dy in zip(dX,dY):

		if doa(x+dx,y+dy,sx,sy):

			r = solve(x+dx,y+dy,xy)

			if r: return r

def doa(x,y,sx,sy):

	if not (0 <= x <= 9 and 0 <= y <= 9): return False

	return True if abs(x-sx) < 2 and abs(y-sy) < 2 else False

while 1:

	x,y = list(map(int,input().split()))

	if x == y == 0: break

	n = input()

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

	ans = solve(x,y,xy)

	print(ans if ans else "NA")


Input:
import sys

input = sys.stdin.readline

sys.setrecursionlimit(1000000)

from collections import deque

def getN():

    return int(input())

def getList():

    return list(map(int, input().split()))

import math

n = getN()

nums = getList()

# nums.sort(reverse=True)

mx = max(nums)

mn = (mx+1) // 2

mndx = (mx % 2) + 1

from collections import Counter

cnt = Counter(nums)

for i in range(mn+1, mx+1):

    if cnt[i] < 2:

        print("Impossible")

        sys.exit()

if cnt[mn] != mndx:

    print("Impossible")

else:

    print("Possible")


Input:
import sys

if __name__ == '__main__':

    import doctest

    doctest.testmod()

    S = int(sys.stdin.read())

    h = S / 3600

    m = (S % 3600) / 60

    s = S % 60

    print("%d:%d:%d" % (h, m, s))


Input:
n = int(input())

s = []

for i in range(1, 10):

  for j in range(1, 10):

    s.append(i*j)

if n in s:

  print("Yes")

else:

  print("No")


Input:
import math

N=int(input())

ans=math.factorial(N)

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


Input:
from collections import deque

n,*t=list(map(int,open(0).read().split()))

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

dist_f_k=[0]*n

for a,b,c in zip(t[:3*n-3:3],t[1::3],t[2::3]):

	con[a-1].append((b-1,c))

	con[b-1].append((a-1,c))

k=t[3*n-2]

que=deque([k-1])

while que:

	cur=que.popleft()

	for nxt,dd in con[cur]:

		if dist_f_k[nxt]==0 and nxt!=k-1:

			que.append(nxt)

			dist_f_k[nxt]=dist_f_k[cur]+dd

for x,y in zip(t[3*n-1::2],t[3*n::2]):

	print((dist_f_k[x-1]+dist_f_k[y-1]))


Input:
S = input()

T = input()

N = len(S)

cnt = 0

for i in range(N):

    cnt += (S[i] != T[i])

print(cnt)


Input:
n, q = [ int( val ) for val in input( ).split( " " ) ]

ps = [0]*n

t = [0]*n

for i in range( n ):

	ps[i], t[i] = input( ).split( " " )

output = []

qsum = 0

while t:

	psi = ps.pop( 0 )

	ti = int( t.pop( 0 ) )

	if ti <= q:

		qsum += ti

		output.append( psi+" "+str( qsum ) )

	else:

		t.append( ti - q )

		ps.append( psi )

		qsum += q

print(( "\n".join( output ) ))


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

a=list(range(1,N+1))

for i in range(K):

  count=int(input())

  b=input().split(' ')

  for j in range(count):

    if int(b[j]) in a:

      a.remove(int(b[j]))

print((len(a)))


Input:
res = []

while True:

    s = input()

    if s == '0':

        break

    res.append(sum([int(x) for x in s]))

for e in res:

    print(e)


Input: import re;print((max(list(map(len,re.split('[^ACGT]',input()))))))


Input:
import math

n=int(input())

waru=10**9+7

li=[]

li2=[]

for i in range(2,n+1):

    tmp=math.factorial(n)

    flag=1

    for j in range(len(li)):

        if i%li[j]==0:

            flag=0

            break

    if flag==1:

        li.append(i)

        li2.append(0)

        while tmp%i==0:

            li2[len(li)-1]+=1

            tmp=tmp//i

ans=1

# print(li)

# print(li2)

for i in range(len(li2)):

    ans*=li2[i]+1

print((ans%waru))


Input:
N = int(input())

A = [int(i) for i in input().split()]

ans = 0

check = [0]*30

for i in range(1, 31):

    check[i-1] = 2**i

for i in A:

    if i % 2 == 0:

        j = 0

        while check[j] <= i:

            if i % check[j] == 0:

                add = check[j]

            else:

                break

            j += 1

        ans += j

print(ans)


Input:
N = int(input())

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

M = int(input())

p = []

x = []

ans = []

for i in range(M):

    p, x = list(map(int, input().split()))

    ans.append(sum(T[:p-1])+x+sum(T[p:]))

for _ in ans:

    print(_)


Input:
import math

N = int(input())

print((math.factorial(N)%(10**9+7)))


Input:
def main():

    import math

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

    a=[]

    for i in range(M):

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

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

        c=0

        for j in range(len(tmpc)):

            c+=2**(tmpc[j]-1)

        tmp[1]=c

        a+=[tmp]

    dp=[[10**10 for j in range(2**N)] for i in range(M)]

    dp[0][0]=0

    dp[0][a[0][1]]=a[0][0]

    for i in range(1,M):

        dp[i]=dp[i-1]

        for j in range(2**N):

            z,p=a[i][0],a[i][1]

            q=j|p

            x=dp[i-1][j]+z

            if dp[i][q]>x: dp[i][q]=x

    ans=dp[M-1][2**N-1]

    print((ans if ans<10**10 else -1))

if __name__ == '__main__':

    main()


Input:
#!/usr/bin/env pypy3

# N,M = map(int,sys.stdin.readline().split())

# a = tuple(map(int,sys.stdin.readline().split())) # single line with multi param

# a = tuple(int(sys.stdin.readline()) for _ in range(N)) # multi line with single param

# a = tuple(tuple(map(int,sys.stdin.readline().rstrip().split())) for _ in range(N)) # multi line with multi param

# s = sys.stdin.readline().rstrip()

# N = int(sys.stdin.readline())

# INF = float("inf")

import math,sys

n = int(sys.stdin.readline())

a = tuple(map(int,sys.stdin.readline().split()))

#n = int(input())

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

g = math.gcd(a[0],a[1])

for i in range(2,n):

    g = math.gcd(g,a[i])

M = max(a)

acc = a[0]

for i in range(n):

    acc = math.gcd(acc,a[i])

if acc != 1:

    print("not coprime")

    exit()

LIMIT=max(a)

minPrime = [0]*(LIMIT+1)

minPrime[1] = 1

def make():

    for i in range(2,LIMIT+1):

        if minPrime[i] == 0:

            minPrime[i] = i

            #print(i)

            for j in range(i+i,LIMIT+1,i):

                #print(i,j)

                if minPrime[j] == 0:

                    minPrime[j] = i

make()

def factrial(N):

     ret = []

     while minPrime[N] != N:

         ret.append(minPrime[N])

         N = N//minPrime[N]

     if N != 1:

         ret.append(N)

     return ret

judge = set([])

pc = True

pairwise = True

p = set() #all prime

for e in a:

    if not pairwise:

        break

    asf = set(factrial(e))

    if judge & asf != set():

        pc = False

    #     #pairwise = False

    #     print("setwise coprime")

    #     exit(0)

    judge |= asf

# for i in a:

#     if not pc:

#         break

#     asf = set(factrial(i))

#     if judge & asf != set():

#         pc = False

#     judge |= asf

if pc:

    print("pairwise coprime")

elif g == 1:

    print("setwise coprime")

else:

    print("not coprime")


Input:
alph = input()

if alph.isupper():

    print("A")

else:

    print("a")


Input:
n, m, k = list(map(int, input().split()))

mod = 1000000007

def pow(x, n):

    ret = 1

    while n > 0:

        if (n & 1) == 1:

            ret = (ret * x) % mod

        x = (x * x) % mod

        n >>= 1

    return ret

fac = [1]

inv = [1]

for i in range(1, n * m + 1):

    fac.append((fac[-1] * i) % mod)

    inv.append(pow(fac[i], mod - 2))

def cmb(n, k):

    return (fac[n] * inv[k] * inv[n - k]) % mod

def doit(n, m, k):

    ret = 0

    for d in range(m):

        ret = (ret + d * (m - d)) % mod

    return (ret * n * n * cmb(n * m - 2, k - 2)) % mod

print(((doit(n, m, k) + doit(m, n, k)) % mod))


Input:
s=int(input())

p=10**9+7

if s<=2:

    print((0))

    exit()

n=s//3

ans=0

def f(n,k,p):

    if k==0:

        return 1

    elif k%2==0:

        return (f(n,k//2,p)**2)%p

    else:

        return (n*f(n,k-1,p))%p

for k in range(1,n+1):

    a=1

    b=1

    for j in range(k-1):

        a=(a*(s-3*k+1+j))%p

        b=(b*(j+1))%p

    ans=(ans+a*f(b,p-2,p))%p

print((ans%p))


Input: a,b,c,x,y=list(map(int,input().split()));print((min(k*c*2+a*max(x-k,0)+b*max(y-k,0)for k in range(max(x,y)*3))))


Input:
import math

import string

import collections

from collections import Counter

def readints():

    return list(map(int, input().split()))

def nCr(n, r):

    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))

def has_duplicates2(seq):

    seen = []

    for item in seq:

        if not(item in seen):

            seen.append(item)

    return len(seq) != len(seen)

def divisor(n):

    divisor = []

    for i in range(1, n+1):

        if n % i == 0:

            divisor.append(i)

    return divisor

# coordinates

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

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

a, b, c, d = list(map(int, input()))

#print(a, b, c, d)

if a+b+c+d == 7:

    print((str(a)+'+'+str(b)+'+'+str(c)+'+'+str(d)+'=7'))

    exit()

if a+b-c+d == 7:

    print((str(a)+'+'+str(b)+'-'+str(c)+'+'+str(d)+'=7'))

    exit()

if a+b+c-d == 7:

    print((str(a)+'+'+str(b)+'+'+str(c)+'-'+str(d)+'=7'))

    exit()

if a+b-c-d == 7:

    print((str(a)+'+'+str(b)+'-'+str(c)+'-'+str(d)+'=7'))

    exit()

if a-b+c+d == 7:

    print((str(a)+'-'+str(b)+'+'+str(c)+'+'+str(d)+'=7'))

    exit()

if a-b-c+d == 7:

    print((str(a)+'-'+str(b)+'-'+str(c)+'+'+str(d)+'=7'))

    exit()

if a-b+c-d == 7:

    print((str(a)+'-'+str(b)+'+'+str(c)+'-'+str(d)+'=7'))

    exit()

if a-b-c-d == 7:

    print((str(a)+'-'+str(b)+'-'+str(c)+'-'+str(d)+'=7'))

    exit()


Input:
a,b,c=list(map(int, input().split()))

d=0

while d<10**5:

    if a%2==1 or b%2==1 or c%2==1:break

    a,b,c=b/2+c/2,a/2+c/2,a/2+b/2

    d+=1

else:d=-1

print(d)


Input:
import statistics

N = int(input())

a = [int(i) for i in input().split()]

ans = 0

for i in range(1,N+1):

  a[i-1] -= i

b = statistics.median(a)

for i in range(N):

  ans += abs(a[i]-b)

print((int(ans)))


Input:
n = int(input())

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

ans = 0

nodes_min = [0] * (n + 1)

nodes_max = [0] * (n + 1)

nodes_min[n] = leaves[n]

nodes_max[n] = leaves[n]

for depth in range(n, 0, -1):

    root_min = nodes_min[depth] // 2 + nodes_min[depth] % 2

    nodes_min[depth - 1] = leaves[depth - 1] + root_min

    root_max = nodes_max[depth]

    nodes_max[depth - 1] = leaves[depth - 1] + root_max

nodes = [0] * (n + 1)

nodes[0] = 1

if nodes[0] < nodes_min[0]:

    print((-1))

else:

    for depth in range(n):

        roots = nodes[depth]-leaves[depth]

        nodes[depth + 1] = min(roots * 2, nodes_max[depth + 1])

        if nodes[depth+1] < nodes_min[depth+1]:

            print((-1))

            break

    else:

        print((sum(nodes)))


Input:
N = int(input())

A = [int(i) for i in input().split()]

dp = [0] * 10 ** 7

ans = 0

for i in range(N):

    x = i + 1 - A[i]

    if x >= 0:

        ans += dp[x]

    y = i + 1 + A[i]

    if y < 10 ** 7:

        dp[y] += 1

print(ans)


Input:
a,b=2,1

for i in[0]*int(input()):a,b=b,a+b

print(a)


Input:
S = int(input())

mod = 10**9+7

dp = [0]*(S+1)

dp[0] = 1

for i in range(1,S+1):

    for j in range(0,(i-3)+1):

        dp[i] += dp[j]

        dp[i] %= mod

print((dp[S]))


Input:
# ABC 120 C

import re

S = input()

l0 = 0

l1 = 0

for i in range(len(S)):

    if S[i] == '0':

        l0 += 1

    elif S[i] == '1':

        l1 += 1

print((min(l0, l1) * 2))


Input:
def iput(): return int(input())

def mput(): return list(map(int, input().split()))

def lput(): return list(map(int, input().split()))

def solve():

    a, b = mput()

    print((a*b))

    return 0

if __name__ == "__main__":

    solve()


Input:
a, b, c = list(map(int, input().split()))

if (b - a) == (c - b):

    print("YES")

else:

    print("NO")


Input:
import math

print((math.factorial(int(input()))%1000000007))


Input:
a,b = list(map(int, input().split()))

count = 0

for i in range(a,b+1):

    j = str(i)

    if j==''.join(reversed(j)):

        count += 1

print(count)


Input:
#!/usr/bin/env pypy3

import math

print((math.factorial(int(input())) % 1000000007))


Input:
(N, K), *AB = [list(map(int, s.split())) for s in open(0)]

for a, b in sorted(AB):

    K -= b

    if K <= 0:

        print(a)

        break


Input:
mod = int(1e9+7)

n = int(input())

power = 1

for i in range(1, n+1):

    power = power * i % mod

print(power)


Input:
N, W = list(map(int, input().split()))

L = [list(map(int, input().split())) for i in range(N)]

V = 0

for i in range(N):

  V += L[i][1]

dp = [[10**9+1] * (V + 1) for i in range(N+1)]

dp[0][0] = 0

for i in range(N):

    for j in range(V+1):

        if j - L[i][1] >= 0:

          dp[i+1][j] = min(dp[i][j], dp[i][j-L[i][1]] + L[i][0])

        else:

          dp[i+1][j] = dp[i][j]

ans = 0

for j in range(V+1):

    if  W >= dp[-1][j]:

        ans = max(ans, j)

print(ans)


Input:
s = input()

start = -1

end = 0

for i in range(len(s)):

    if s[i] == 'A' and start < 0:

        start = i

    elif s[i] == 'Z':

        end = i

print((end - start + 1))


Input:
while 1:

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

 if n+x==0:break

 print((len([1 for i in range(3,n+1)for j in range(2,x-i)if x-i-j<j<i])))


Input:
def main():

    from sys import stdin

    input = stdin.readline

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

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

    for i in range(r):

        l[i] -= 1

    d = [[10**8] * n for _ in range(n)]

    for _ in range(m):

        i, j, k = list(map(int, input().split()))

        d[i-1][j-1] = k

        d[j-1][i-1] = k

    # Warshall-Floyd algorithm

    for k in range(n):

        for i in range(n):

            for j in range(n):

                if d[i][j] > d[i][k] + d[k][j]:

                    d[i][j] = d[i][k] + d[k][j]

    # full search

    # 8! = 40320

    from itertools import permutations

    answer = 10**8

    for i in permutations(l):

        ans = 0

        for j in range(r-1):

            ans += d[i[j]][i[j+1]]

        if ans < answer:

            answer = ans

    print(answer)

main()


Input:
from collections import Counter

S = input()[::-1]

memo = [0]

d = 0

mod = 2019

for s in S:

    tmp = int(s) * pow(10, d, mod) % mod

    tmp = memo[-1] + tmp

    memo.append(tmp % mod)

    d += 1

c = Counter(memo)

ans = sum(v * (v - 1) // 2 for v in list(c.values()))

print(ans)


Input:
a = input()

print(('A' if not a.islower() else 'a'))


Input:
N=int(input())

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

for i in range(N):

  for j in range(1,31):

    if a[i]%(2**j)!=0:

      a[i]=j-1

      break

print((sum(a)))


Input:
a, b = list(map(int, input().split()))

cnt = 0

for i in range(a, b+1):

    i = str(i)

    if i[0] == i[-1] and i[1] == i[-2]:

        cnt += 1

print(cnt)


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

cnt = 0

for i in range(k + 1):

    for j in range(k + 1):

        if i + j > s:

            break

        elif s - i - j <= k:

            cnt += 1

print(cnt)


Input:
from collections import Counter

s = input()

mod1 = 673

mod2 = 3

num1 = 0

num2 = 0

li1 = []

li2 = []

pw1 = 10

pw2 = 10

for i, e in enumerate(s, 1):

    num1 *= 10

    num1 += int(e)

    num1 %= mod1

    inv = pow(pw1, mod1 - 2, mod1)

    li1.append(num1 * inv % mod1)

    num2 *= 10

    num2 += int(e)

    num2 %= mod2

    inv = pow(pw2, mod2 - 2, mod2)

    li2.append(num2 * inv % mod2)

    pw1 *= 10

    pw1 %= mod1

    pw2 *= 10

    pw2 %= mod2

li = [0] + [10 * e1 + e2 for e1, e2 in zip(li1, li2)]

c = Counter(li)

ans = 0

for v in list(c.values()):

    ans += v * (v - 1) // 2

print(ans)


Input:
a, b, c, x, y = list(map(int, input().split(' ')))

c = int(c*2)

m = float('inf')

for i in range(10**5+1):

    m = min(m, a*max(0, x-i)+b*max(0,y-i)+c*i)

print(m)


Input:
l,r=list(map(int,input().split()))

s=list(range(l,r+1))[:2019]

print((min(i*j%2019for i in s for j in s if i<j)))


Input:
s = input()

cnt = 0

for i in range(len(s) - 1):

    if s[i] == 'W' and s[i + 1] == 'B':

        cnt += 1

    if s[i] == 'B' and s[i + 1] == 'W':

        cnt += 1

print(cnt)


Input:
import math

mod = 10 ** 9 + 7

n = int(input())

print((math.factorial(n) % mod))


Input:
A, B = list(map(int, input().split()))

count = 0

for i in range(A, B+1):

    if(str(i) == str(i)[::-1]):

        count += 1

print(count)


Input:
import math

import string

def readints():

    return list(map(int, input().split()))

def nCr(n, r):

    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))

def has_duplicates2(seq):

    seen = []

    for item in seq:

        if not(item in seen):

            seen.append(item)

    return len(seq) != len(seen)

def divisor(n):

    divisor = []

    for i in range(1, n+1):

        if n % i == 0:

            divisor.append(i)

    return divisor

# coordinates

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

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

n = int(input())

a = readints()

def func(l):

    if len(l) == n:

        for i in range(len(l)):

            if l[i] % 2 == 0:

                return 1

        return 0

    return func(l+[(a[len(l)])])+func(l+[(a[len(l)]-1)])+func(l+[(a[len(l)]+1)])

print((func([])))


Input:
import math

N = int(input())

num = math.factorial(N)

k, r = divmod(num, 10**9 + 7)

print(r)


Input:
def main():

    N, M = (int(i) for i in input().split())

    def trial_division(n):

        divs = []

        for i in range(1, int(n**0.5)+1):

            if n % i == 0:

                divs.append(i)

                if i != n//i:

                    divs.append(n//i)

        divs.sort(reverse=True)

        return divs

    divs = trial_division(M)

    for d in divs:

        if d*N <= M:

            return print(d)

if __name__ == '__main__':

    main()


Input:
import math

N = int(input())

C = 10**9 + 7

print((math.factorial(N) % C))


Input:
import sys

input = sys.stdin.readline

import bisect

n = int(input())

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

l = int(input())

U = 17

dp = [[0]*n for _ in range(U+1)]

for i, x in enumerate(X):

  t = bisect.bisect_left(X, x+l)

  dp[0][i] = bisect.bisect_right(X, x+l) - 1

for k in range(U):

  for i in range(n):

    dp[k+1][i] = dp[k][dp[k][i]]

def test(x, a, b):

  for i in range(U, -1, -1):

    if x >> i & 1:

      a = dp[i][a]

  return a >= b

def solve(a, b):

  if a > b:

    a, b = b, a

  ng = 0

  ok = n-1

  while ok - ng > 1:

    mid = (ng + ok) // 2

    if test(mid, a, b):

      ok = mid

    else:

      ng = mid

  print(ok)

q = int(input())

for _ in range(q):

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

  a -= 1

  b -= 1

  solve(a, b)


Input:
n, like = list(map(int, input().split()))

arr = [like + i for i in range(n)]

arr.sort(key=lambda x:abs(x))

print((sum(arr[1:])))


Input:
A, B, C, X, Y = list(map(int, input().split()))

ans = float('inf')

for i in range(max(X,Y)+1):

    cost = 2 * C * i + max(0,X-i)*A +max(0,Y-i)*B

    ans = min(ans,cost)

print(ans)


Input:
#coding: UTF-8

import math

N=int(input())

def prime_list(k):

    lis=[True for i in range(1,k+1)]

    lis[0]=False

    for i in range(2,k+1):

        j=2

        while j*j<=i:

            if i%j==0:

                lis[i-1]=False

                break

            else:

                j+=1

    return lis

#    print(lis)

def p_fact(k,N):

    plist=prime_list(k)

    lis=[0]*N

    for i in range(0,k):

        n=0

        if plist[i]:

            while k%(i+1)==0:

                n+=1

                k=k/(i+1)

        lis[i]=n

    return lis

out_lis=[0]*N

for i in range(1,N+1):

    input_lis=p_fact(i,N)

    out_lis=[out_lis[j]+input_lis[j] for j in range(0,N)]

#print(out_lis)

out=1

for i in range(0,N):

    out=out*(out_lis[i]+1)

print((out%1000000007))


Input:
import sys

import queue

N = 3

f= ''.join(sys.stdin.readline().split())

f+= ''.join(sys.stdin.readline().split())

f+= ''.join(sys.stdin.readline().split())

dd = [[-1, 0], [0, -1], [1, 0], [0, 1]]

Q = queue.Queue()

V = dict()

Q.put([f,f.index('0'),0])

V[f] = True

while not Q.empty():

    u = Q.get()

    if u[0] == '123456780':  break

    sx, sy = u[1]//N, u[1]%N

    for dx, dy in dd:

        tx, ty = sx+dx, sy+dy

        if tx<0 or ty<0 or tx>=N or ty>=N: continue

        v = u[:]

        n1, n2 = u[1], tx*N+ty

        v[1] = n2

        if n1>n2: n1, n2 = n2, n1

        v[0] = v[0][0:n1]+v[0][n2]+v[0][n1+1:n2]+v[0][n1]+v[0][n2+1:]

        if not V.get(v[0], False):

            V[v[0]] = True

            v[2] += 1

            Q.put(v)

print((u[2]))


Input:
def main():

    S = input()

    w1 = 'dream'[::-1]

    w2 = 'dreamer'[::-1]

    w3 = 'erase'[::-1]

    w4 = 'eraser'[::-1]

    # print(w1,w2,w3,w4)

    S = S[::-1]

    # print(S)

    # words = ['dream', 'dreamer', 'erase', 'eraser']

    words = [w1, w2, w3, w4]

    T = ''

    while S != T:

        cand = []

        for w in words:

            new_t = T + w

            cand.append(new_t)

        # print('cand', cand)

        ans = False

        for c in cand:

            if c == S[:len(c)]:

                T = c

                ans = True

        if not ans:

            break

    if S == T:

        print('YES')

    else:

        print("NO")

if __name__ == "__main__":

    main()


Input:
N,MA,MB = list(map(int,input().split()))

src = [tuple(map(int,input().split())) for i in range(N)]

INF = 10**9

dp = [[INF for j in range(401)] for i in range(401)]

dp[0][0] = 0

maxa = maxb = 0

for a,b,c in src:

    for ai in range(maxa,-1,-1):

        for bi in range(maxb,-1,-1):

            dp[ai+a][bi+b] = min(dp[ai+a][bi+b], dp[ai][bi] + c)

    maxa += a

    maxb += b

ans = INF

for i in range(1,401):

    if MA*i > 400 or MB*i > 400: break

    ans = min(ans, dp[MA*i][MB*i])

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


Input:
s = input()

a = s.find('A')

z = s[::-1].find('Z')

ans = len(s)-a-z

print(ans)

#print(*ans, sep='\n')


Input: _,t=open(0);n=eval(t.replace(' ','|'));print((len(bin(n&-n))-3))


Input:
import statistics

N = int(input())

A = [int(a)-i-1 for i,a in enumerate(input().split(),1)]

b = statistics.median_low(A)

c = statistics.median_high(A)

d = sum([abs(x-b) for x in A])

e = sum([abs(x-c) for x in A])

print((min(d,e)))


Input:
a,b = list(map(int,input().split()))

c = 0

for i in range(a,b+1):

  new = list(reversed(list(str(i))))

  if new == list(str(i)):

    c += 1

print(c)


Input:
import sys

input = sys.stdin.readline

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

hole = [0] * (N+1)

for i in range(M):

    hole[int(input())] = 1

if N == 1:

    print((1))

    exit()

if N == 2:

    if hole[1] == 1:

        print((1))

        exit()

    if hole[1] == 0:

        print((2))

        exit()

flg_one = False

flg_two = False

first = 1

second = 2

if hole[1] == 1:

    first = 0

    second = 1

    flg_one = True

if hole[2] == 1:

    first = 1

    second = 0

    flg_two = True

if flg_one and flg_two:

    print((0))

    exit()

for i in range(3,N):

    if hole[i] == 1:

        first = second

        second = 0

        continue

    tmp = first + second

    tmp %= 1000000007

    first = second

    second = tmp

tmp = first + second

tmp %= 1000000007

print(tmp)


Input:
#!usr/bin/env python3

from collections import defaultdict

from collections import deque

from heapq import heappush, heappop

import sys

import math

import bisect

import random

def LI(): return list(map(int, sys.stdin.readline().split()))

def I(): return int(sys.stdin.readline())

def LS():return list(map(list, sys.stdin.readline().split()))

def S(): return list(sys.stdin.readline())[:-1]

def IR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = I()

    return l

def LIR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = LI()

    return l

def SR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = S()

    return l

def LSR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = SR()

    return l

mod = 1000000007

#A

def A():

    s = S()

    t = S()

    if s+t == t[::-1]+s[::-1]:

        print("YES")

    else:

        print("NO")

#B

def B():

    n = I()

    for i in range(int(n**0.5)+2)[::-1]:

        if i*i <= n:

            print((i*i))

            quit()

#C

def C():

    n = I()

    a = LI()

    b = LI()

    c = LI()

    q = [0 for i in range(n)]

    ans = 0

    a.sort()

    b.sort()

    c.sort()

    for i in range(n):

        j = bisect.bisect_left(a,b[i])

        q[i] = j

    for i in range(n-1):

        q[i+1] += q[i]

    q.insert(0,0)

    for i in range(n):

        j = bisect.bisect_left(b,c[i])

        ans += q[j]

    print(ans)

#D

def D():

    def dijkstra():

        d = [float("inf") for i in range(k)]

        q = [[0,1]]

        d[1] = 0

        while q:

            dx,x = heappop(q)

            for y,dy in v[x]:

                if d[y] > dx+dy:

                    d[y] = dx+dy

                    heappush(q,[d[y],y])

        print((d[0]+1))

    k = I()

    if k == 1:

        print((1))

        quit()

    v = [[] for i in range(k)]

    for i in range(1,k):

        v[i].append([(i+1)%k,1])

        v[i].append([i*10%k,0])

    dijkstra()

#E

def E():

    return

#F

def F():

    return

#G

def G():

    return

#H

def H():

    return

#Solve

if __name__ == "__main__":

    D()


Input:
import math

x = int(input())

print((math.factorial(x) % (10**9 + 7)))


Input:
# coding: utf-8

def solve(*args: str) -> str:

    k = int(args[0])

    l = 9*(k//7 if k % 7 == 0 else k)

    if l % 2 == 0 or l % 5 == 0:

        return '-1'

    r = phi = l

    for i in range(2, int(-pow(l, 1/2))):

        if r % i == 0:

            phi = phi//i*(i-1)

            while r % i:

                r //= i

    a = 10 % l

    ret = 1

    while(a != 1):

        a = a*10 % l

        ret += 1

        if phi < ret:

            ret = -1

            break

    return str(ret)

if __name__ == "__main__":

    print((solve(*(open(0).read().splitlines()))))


Input:
import sys

sys.setrecursionlimit(100000)

def intinput(): return list(map(int,sys.stdin.readline().split()))

fib={0:0,1:1,2:1}

for i in range(3,100003):

    fib[i]=fib[i-1]+fib[i-2]

  #  fib.append(fib[i-1]+fib[i-2])

n,m=intinput()

mod=10**9+7

a=[int(sys.stdin.readline()) for _ in range(m)]

if m!=0:

    l=[a[0]]

    for i in range(len(a)-1):

        l.append(a[i+1]-a[i]-1)

    l.append(n-a[-1])

    k=1

    for i in l:

        k=fib[i]*k%mod

    print((k%mod))

else:

    print((fib[n+1]%mod))


Input:
N = int(input())

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

answer = 10 ** 9

for i in range(1,101):

    tmp = 0

    for a in A:

        tmp += abs(a-i) ** 2

    answer = min(answer,tmp)

print(answer)


Input:
"""

challenge case

in:

3

1 3

2 3

out:

1

1

1

"""

from collections import deque

def main():

	N, *AB = list(map(int, open(0).read().split()))

	B = AB[1::2]

	E = [[] for _ in range(N + 1)]

	for a, b in zip(*[iter(AB)] * 2):

		E[a] += b,

	Q = deque([1])

	C = [0] * (N + 1)

	while Q:

		v = Q.popleft()

		c = 0

		for u in E[v]:

			c += 1 + (c + 1 == C[v])

			C[u] = c

			Q += u,

	print((max(C)))

	for b in B:

		print((C[b]))

main()


Input:
import sys

input = sys.stdin.buffer.readline

from collections import defaultdict

import copy

def main():

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

    d = defaultdict(int)

    MOD = 10**9+7

    R = 10**5+100

    fac = [0 for _ in range(R+1)]

    fac[0],fac[1] = 1,1

    inv = copy.deepcopy(fac)

    invfac = copy.deepcopy(fac)

    for i in range(2,R+1):

        fac[i] = (fac[i-1]*i)%MOD

        inv[i] = MOD-(MOD//i)*inv[MOD%i]%MOD

        invfac[i] = (invfac[i-1]*inv[i])%MOD

    def coef(x,y):

        num = (((fac[x+y]*invfac[y])%MOD)*invfac[x]%MOD)

        return num

    while M%2 == 0:

        d[2] += 1

        M //= 2

    f = 3

    while f ** 2 <= M:

        if M % f == 0:

            d[f] += 1

            M //= f

        else:

            f += 2

    if M != 1:

        d[M] += 1

    l = list(d.values())

    ans = 1

    for num in l:

        ans *= coef(num,N-1)

        ans %= MOD

    print(ans)

if __name__ == "__main__":

    main()


Input:
from collections import Counter

d={chr(i):0 for i in range(2)}

s=Counter(input())

d=d.update(s)

print((min(s["0"],s["1"])*2))


Input:
from functools import reduce
def f(a,b): return [Card[a][0], Card[b][1]]

n = int(input())

Card = [[] for _ in [0]*n]

Cost = {}

for i in range(n):

  Card[i] = list(map(int, input().split()))

  Cost[(i, i)] = 0

for i in range(1, n):

  for j in range(0, n-i):

    a = j+i

    Cost[(j, a)] = min([reduce(lambda a,b: a*b, f(j, k) + f(k+1, a)) + Cost[(j, k)] + Cost[(k+1, a)] for k in range(j, j+i)])

print(Cost[0, n-1])


Input:
def cmb(n, r, mod):

    if ( r<0 or r>n ):

        return 0

    r = min(r, n-r)

    return g1[n] * g2[r] * g2[n-r] % mod

A=2*10**5

mod = 10**9+7

g1 = [1, 1]

g2 = [1, 1]

inverse = [0, 1]

for i in range( 2, A + 1 ):

    g1.append( ( g1[-1] * i ) % mod )

    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )

    g2.append( (g2[-1] * inverse[-1]) % mod )

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

keisuu=cmb(N*M-2,K-2,mod)

sum_=0

for i in range(N):

    a=min(abs(i),abs(N-i-1))

    b=max(abs(i),abs(N-i-1))

    sum_+=(M**2)*((a*(a+1)//2)+(b*(b+1)//2))

for i in range(M):

    a=min(abs(i),abs(M-i-1))

    b=max(abs(i),abs(M-i-1))

    sum_+=(N**2)*((a*(a+1)//2)+(b*(b+1)//2))

print(((keisuu * (sum_//2))%mod))


Input:
k,n = list(map(int,input().split()))

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

a.append(a[0]+k)

longd=0

for i in range(n):

    tmp=abs(a[i+1]-a[i])

    longd=max(longd,tmp)

print((k-longd))


Input:
def ii():return int(input())

def iim():return list(map(int,input().split()))

def iil():return list(map(int,input().split()))

def ism():return list(map(str,input().split()))

def isl():return list(map(str,input().split()))

n = ii()

D = iil()

ans = 0

for i in range(n):

    for j in range(i+1,n):

#        print(i,j)

        ans += D[i]*D[j]

print(ans)


Input:
S = input()

K = int(input())

a = list(set(S))

a.sort()

k = []

l = 0

while l < len(a):

    for i in range(len(S)):

        if S[i] == a[l]:

            for j in range(1,6):

                if i+j <= len(S):

                    k.append(S[i:i+j])

                else:

                    break

    l += 1

k = list(set(k))

k.sort()

print((k[K-1]))


Input:
s = input()[::-1]

l = len(s)

c = [0] * 2019

c[0] = 1

ten = 1

mod = 0

ans = 0

for i in range(l):

    mod = (mod + int(s[i]) * ten) % 2019

    ten = ten * 10 % 2019

    c[mod] += 1

for i in c:

    ans += i * (i - 1) // 2

print(ans)


Input:
S=input()

print((S.rfind('Z')-S.find('A')+1))


Input:
#!usr/bin/env python3

from collections import defaultdict

from collections import deque

from heapq import heappush, heappop

import sys

import math

import bisect

import random

def LI(): return list(map(int, sys.stdin.readline().split()))

def I(): return int(sys.stdin.readline())

def LS():return list(map(list, sys.stdin.readline().split()))

def S(): return list(sys.stdin.readline())[:-1]

def IR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = I()

    return l

def LIR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = LI()

    return l

def SR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = S()

    return l

def LSR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = LS()

    return l

sys.setrecursionlimit(1000000)

mod = 1000000007

#A

def A():

    return

#B

def B():

    return

#C

def C():

    return

#D

def D():

    return

#E

def E():

    return

#F

def F():

    a,b,c,X,Y = LI()

    ans = float("inf")

    for z in range(300001):

        if z%2 == 0:

            m = c*z

            x = z//2

            y = z//2

            m += a*max(0,X-x)

            m += b*max(0,Y-y)

            if m < ans:

                ans = m

    print(ans)

    return

#G

def G():

    n = I()

    x = LI()

    f = [(i,x[i]) for i in range(n)]

    f.sort(key = lambda x:x[1])

    g = [(f[i][0],i) for i in range(n)]

    g.sort(key = lambda x:x[0])

    for i in range(n):

        if g[i][1] < n//2:

            print((f[n//2][1]))

        else:

            print((f[n//2-1][1]))

    return

#H

def H():

    return

#I

def I_():

    return

#J

def J():

    return

#Solve

if __name__ == "__main__":

    F()


Input:
n = int(input()) % 1000

if n == 0:

    print((0))

else:

    print((1000 - n))


Input:
L ,R = list(map(int,input().split()))

ans =[]

if R - L <= 2019:

    for i in range(L,R+1):

        for j in range(i+1,R+1):

            ans.append(i*j%2019)

else:

    for i in range(R-L-1010,R-L+1010):

        for j in range(i,R-L+1010):

            ans.append(i*j%2019)

print((min(ans)))


Input:
from collections import Counter

import heapq

n = int(input())

ss = []

for _ in range(n):

    ss.append(input().rstrip())

ctr = Counter(ss)

heap = [(-v, k) for k, v in list(ctr.items())]

heapq.heapify(heap)

maxv = heap[0][0]

while heap:

    c = heapq.heappop(heap)

    if c[0] != maxv:

        break

    print((c[1]))


Input:
# -*- coding: utf-8 -*-

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

L, R = [None] * M, [None] * M

for i in range(M):

    L[i], R[i] = list(map(int, input().split()))

l = max(L)

r = min(R)

ans = max(r - l + 1, 0)

print(ans)


Input:
import math

input_num = int(input())

num_sqrt = int(math.sqrt(input_num))

minimam = input_num

for i in range(num_sqrt+1,0,-1):

    q, mod = divmod(input_num, i)

    distance = q + i -2

    if mod == 0 and minimam > distance:

        minimam = distance

print(minimam)


Input:
# C - Walk on Multiplication Table

# https://atcoder.jp/contests/abc144/tasks/abc144_c

n = int(input())

ans = 10 ** 12

for i in range(1, 10 ** 6 + 1):

  if n % i == 0:

    ans = min(ans, n // i + i)

print((ans - 2))


Input:
# AGC003B - Simplified mahjong

import sys

input = sys.stdin.readline

def main():

    # split in subsequences when 0 appears -> sum up cur // 2

    N = int(input())

    A = list(map(int, [input() for _ in range(N)])) + [0]

    ans, cur = 0, 0

    for i in A:

        if i:

            cur += i

        else:

            ans += cur // 2

            cur = 0

    print(ans)

if __name__ == "__main__":

    main()


Input:
while True:

    (n, x) = [int(i) for i in input().split()]

    if n == x == 0:

        break

    count = 0

    for a in range(1, n + 1):

        for b in range(a + 1, n + 1):

            for c in range(b + 1, n + 1):

                if sum([a,b,c]) == x:

                    count += 1

    print(count)


Input:
from collections import deque

N = 3

m = {8: {7, 5}, 7: {8, 6, 4}, 6: {7, 3}, 5: {8, 4, 2}, 4: {7, 5, 3, 1}, 3: {6, 4, 0}, 2: {5, 1}, 1: {4, 2, 0},

     0: {3, 1}}

goal = 123456780

def g(i, j, a):

    t = a // (10 ** j) % 10

    return a - t * (10 ** j) + t * (10 ** i)

def solve():

    MAP = "".join(input().replace(" ", "") for _ in range(N))

    start = 8 - MAP.find("0")

    MAP = int(MAP)

    if MAP == goal:

        return 0

    dp = deque([(0, start, MAP)])

    LOG = {MAP}

    while dp:

        cnt, yx, M = dp.popleft()

        if M == goal:

            return cnt

        cnt += 1

        for nyx in m[yx]:

            CM = g(yx, nyx, M)

            if not CM in LOG:

                dp.append((cnt, nyx, CM))

                LOG.add(CM)

def MAIN():

    print((solve()))

MAIN()


Input:
inf = (5000 * 10 ** 5) * 3

a, b, c, x, y = list(map(int, input().split()))

ret = inf

for cnt_ab in range(max(x, y) * 2 + 1):

    d = cnt_ab // 2

    cnt_a = max(0, x - d)

    cnt_b = max(0, y - d)

    t = cnt_ab * c + cnt_a * a + cnt_b * b

    ret = min(ret, t)

print(ret)


Input:
S = int(input())

f = [0]*(S+1)

f[0] = 1

if S >= 3: f[3] = 1

if S >= 4: f[4] = 1

if S >= 5: f[5] = 1

for i in range(6, S+1):

    f[i] = sum(f[i-j] for j in range(3, i+1))%(10**9+7)

print((f[S]))


Input:
import sys

input = sys.stdin.buffer.readline

N, Ma, Mb = list(map(int, input().split()))

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

INF = 10 ** 15

dp = [[INF for j in range(401)] for i in range(401)]

dp[0][0] = 0

for a, b, c in ABC:

  for i in range(400, -1, -1):

    for j in range(400, -1, -1):

      if dp[i][j] != INF:

        dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)

answer = INF

for i in range(1, 401):

  for j in range(1, 401):

    if dp[i][j] != INF and i / j == Ma / Mb:

      answer = min(answer, dp[i][j])

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


Input:
from sys import stdin, stdout, setrecursionlimit

from collections import deque, defaultdict, Counter

from heapq import heappush, heappop

from functools import lru_cache

import math

setrecursionlimit(10**6)

rl = lambda: stdin.readline()

rll = lambda: stdin.readline().split()

rli = lambda: list(map(int, stdin.readline().split()))

rlf = lambda: list(map(float, stdin.readline().split()))

INF, NINF = float('inf'), float('-inf')

MOD = 10**9 + 7

def main():

	s = int(rl())

	@lru_cache(None)

	def dp(currsum):

		if currsum == s: return 1

		cnt = 0

		for d in range(3, 2001):

			if currsum + d <= s:

				cnt += dp(currsum + d)

				cnt %= MOD

		return cnt

	print((dp(0)))

	stdout.close()

if __name__ == "__main__":

	main()


Input:
import math

n=int(input())

print(((2*n)//math.gcd(2,n)))


Input:
import sys

s = input()

ts = ['']

while True:

  nts= []

  for t in ts:

    for w in ['dreamer', 'eraser', 'dream', 'erase']:

      if s == t + w:

        print('YES')

        sys.exit()

      if s.startswith(t + w):

        nts.append(t + w)

  if len(nts) == 0:

    print('NO')

    sys.exit()

  ts = nts


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

a = [[0]*N]*K

a = [[0 for n in range(N)] for k in range(K)]

for k in range(K):

    d=int(input())

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

    for h in have_k:

        a[k][h-1]=1

an=[0]*N

for k in range(K):

  for n in range(N):

    an[n] = an[n]+a[k][n]

result=0

for i in an:

  if i == 0:

    result+=1

print(result)


Input:
import sys

sys.setrecursionlimit(1000000)

S = input()

T = ['dream', 'dreamer', 'erase', 'eraser']

lenT = [len(i) for i in T]

def dayDream(pin):

    global isDream               #;print('dayDream: ', pin, S[pin:])

    for i, d in enumerate(T):

        #print('d:', d)

        if S[pin:pin+lenT[i]] == d:

            pout = pin + lenT[i] #; print(d, pout)

            if pout == len(S):

                isDream = True   #; print('YES')

                return

            else:

                dayDream(pout)

    #print('dayDream return', pin)

    return

isDream = False

dayDream(0)

if isDream:

    print('YES')

else:

    print('NO')


Input:
n,m=list(map(int,input().split()))

c=[0]*(n+2)

for i in range(m):

  l,r=list(map(int,input().split()))

  c[l-1]+=1

  c[r]-=1

for i in range(1,n+2):

  c[i]=c[i]+c[i-1]

ans=0

for i in range(n+2):

  if c[i]==m:

    ans+=1

print(ans)


Input:
n,m=list(map(int,input().split()))

a=[int(x) for x in input().split()]

b=[0 for x in range(n+1)]

l=list([0])

for i in range(1,n+1):

  b[i]=b[i-1]+a[i-1]

for i in range(1,n+1):

  l.append(b[i]%m)

d=list()

l.sort()

t,c=0,1

for i in range(1,n+1):

  if l[i]==l[i-1]:

    c+=1

  else:

    if c!=1:

      t += c*(c-1)//2

      c=1

if c!=1:

  t += c*(c-1)//2

print(t)


Input:
import copy

n=int(input())

org_set=set([i for i in range(1,10**5)])

edges=[]

relations={}

nodes=[]

for i in range(n-1):

    a,b=list(map(int,input().split(" ")))

    edges.append([a,b])

    relations.setdefault(a,[]).append(b)

    nodes.append([])

nodes.append([])

nodes.append([])

max_color=0

for start in range(1,n+1):

    targets=relations.get(start,[])

    color=1

    for target in targets:

        if color in nodes[start]:

            color+=1

        else:

            pass

        nodes[target].append(color)

        if color>=max_color:

            max_color=color

        color+=1

print(max_color)

for edge in edges:

    print((nodes[edge[1]][0]))


Input:
n=int(input())

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

k=[]

ans=0

for i in range(400005):

  k.append(0)

for i in range(n):

  if i-a[i]>=0:

    ans+=k[i-a[i]]

  if a[i]+i<400005:

    k[a[i]+i]+=1

print(ans)


Input:
import sys

import os

import math

ii = lambda: int(sys.stdin.buffer.readline().rstrip())

il = lambda: list(map(int, sys.stdin.buffer.readline().split()))

iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()

isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

def main():

    if os.getenv("LOCAL"):

        sys.stdin = open("input.txt", "r")

    N = ii()

    ret = float('inf')

    I = int(math.sqrt(N)) * 10

    for i in range(1, I):

        if N%i == 0:

            j = N//i

            ret = min(ret, j + i - 2)

    print(ret)

if __name__ == '__main__':

    main()