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

ans = 5000 * 2 * 10 ** 5

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

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

    ans = min(ans, s)

print(ans)

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

if X > Y:

    v = A

else:

    v = B

ans = min(A*X+B*Y, C*max(X, Y)*2, C*min(X,Y)*2+(max(X,Y)-min(X,Y))*v)

print(ans)

A, B, C, X, Y = list(map(int, input().split()))
ans = 5000 * 2 * 10**5
for i in range(10**5 + 1):
    s = i * 2 * C + max(0, X - i) * A + max(0, Y - i) * B
    ans = min(ans, s)
print(ans)

A, B, C, X, Y = list(map(int, input().split()))
if X > Y:
    v = A
else:
    v = B
ans = min(
    A * X + B * Y, C * max(X, Y) * 2, C * min(X, Y) * 2 + (max(X, Y) - min(X, Y)) * v
)
print(ans)


Input:
import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

N = int(read())

x = N + (-N) % 1000

print((x - N))

import math

N = int(input())

x = math.ceil(N/1000) * 1000

print((x-N))

import math

N = int(input())
x = math.ceil(N / 1000) * 1000
print((x - N))

-import sys
+import math
-read = sys.stdin.buffer.read
-readline = sys.stdin.buffer.readline
-readlines = sys.stdin.buffer.readlines
-N = int(read())
-x = N + (-N) % 1000
+N = int(input())
+x = math.ceil(N / 1000) * 1000


Input:
N = int(input())

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

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

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

A.sort()

C.sort()

ans = 0

from bisect import bisect, bisect_left

for b in B:

    i = bisect_left(A, b)

    j = bisect(C, b)

    ans += i * (N-j)

print(ans)

N = int(input())

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

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

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

A.sort()

C.sort()

from bisect import bisect_left, bisect

ans = 0

for b in B:

    i = bisect_left(A,b)

    j = bisect(C,b)

    ans += i * (N-j)

print(ans)

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
C.sort()
ans = 0
from bisect import bisect, bisect_left

for b in B:
    i = bisect_left(A, b)
    j = bisect(C, b)
    ans += i * (N - j)
print(ans)

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort()
C.sort()
from bisect import bisect_left, bisect

ans = 0
for b in B:
    i = bisect_left(A, b)
    j = bisect(C, b)
    ans += i * (N - j)
print(ans)

+from bisect import bisect_left, bisect
+
-from bisect import bisect, bisect_left


Input:
n = int(input())

def countKeta(num):

    count = 1

    while num / 10 >= 1:

        count += 1

        num = num // 10

    return count

count = 0

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

    if(countKeta(i) % 2 == 1):

        count += 1

print(count)

n = int(input())

def countKeta(num):
    count = 1
    while num / 10 >= 1:
        count += 1
        num = num // 10
    return count

count = 0
for i in range(1, n + 1):
    if countKeta(i) % 2 == 1:
        count += 1
print(count)

-def countKeta(num):
-    count = 1
-    while num / 10 >= 1:
-        count += 1
-        num = num // 10
-    return count
-count = 0
-for i in range(1, n + 1):
-    if countKeta(i) % 2 == 1:
-        count += 1
-print(count)
+if n < 10:
+    print(n)
+elif n < 100:
+    print((9))
+elif n < 1000:
+    print((n - 90))
+elif n < 10000:
+    print((909))
+elif n < 100000:
+    print((n - 9090))
+elif n < 1000000:
+    print((90909))


Input:
n = int(input())

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

abn.sort()

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

#print(abn)

ts = 0 # time_stamp

enable = True

for abi in abn:

    a, b = abi

    if not ts + a <= b:

        enable = False

        break

    else:

        ts += a

print(('Yes' if enable else 'No'))

n = int(input())

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

#abn.sort()

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

#print(abn)

ts = 0 # time_stamp

enable = True

for abi in abn:

    a, b = abi

    if not ts + a <= b:

        enable = False

        break

    else:

        ts += a

print(('Yes' if enable else 'No'))

n = int(input())
abn = [list(map(int, input().split())) for _ in range(n)]
abn.sort()
abn.sort(key=lambda x: x[1])
# print(abn)
ts = 0  # time_stamp
enable = True
for abi in abn:
    a, b = abi
    if not ts + a <= b:
        enable = False
        break
    else:
        ts += a
print(("Yes" if enable else "No"))

n = int(input())
abn = [list(map(int, input().split())) for _ in range(n)]
# abn.sort()
abn.sort(key=lambda x: x[1])
# print(abn)
ts = 0  # time_stamp
enable = True
for abi in abn:
    a, b = abi
    if not ts + a <= b:
        enable = False
        break
    else:
        ts += a
print(("Yes" if enable else "No"))


Input:
def isPrime(x):

    if x == 2:

        return True

    if x < 2 or x % 2 == 0:

        return False

    i, root_x = 3, int(pow(x, 0.5))

    while i <= root_x:

        if x % i == 0:

            return False

        i += 2

    return True

primes = [2]

for i in range(3, 104730):

    if isPrime(i):

        primes.append(primes[-1]+i)

while True:

    n = int(input())

    if n == 0:

        break

    print((primes[n-1]))

primes = [0, 0] + [1] * 150000

for i in range(2, 388):

    if primes[i]:

        for j in range(i*i, 150001, i):

            primes[j] = 0

values = [i for i, v in enumerate(primes) if v]

while True:

    n = int(input())

    if n == 0:

        break

    print((sum(values[:n])))

def isPrime(x):
    if x == 2:
        return True
    if x < 2 or x % 2 == 0:
        return False
    i, root_x = 3, int(pow(x, 0.5))
    while i <= root_x:
        if x % i == 0:
            return False
        i += 2
    return True

primes = [2]
for i in range(3, 104730):
    if isPrime(i):
        primes.append(primes[-1] + i)
while True:
    n = int(input())
    if n == 0:
        break
    print((primes[n - 1]))

primes = [0, 0] + [1] * 150000
for i in range(2, 388):
    if primes[i]:
        for j in range(i * i, 150001, i):
            primes[j] = 0
values = [i for i, v in enumerate(primes) if v]
while True:
    n = int(input())
    if n == 0:
        break
    print((sum(values[:n])))

+        for j in range(i * i, 150001, i):

+values = [i for i, v in enumerate(primes) if v]


Input:
import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)

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

ans = 0

for z in range(K+1):

    for y in range(K+1):

        x = S - (z + y)

        if x >= 0 and x <= K:

            ans += 1

print(ans)

import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)

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

ans = 0

for z in range(K+1):

    for y in range(K+1):

        x = S - (z + y)

        if 0 <= x <= K:

            ans += 1

print(ans)

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
K, S = list(map(int, input().split()))
ans = 0
for z in range(K + 1):
    for y in range(K + 1):
        x = S - (z + y)
        if x >= 0 and x <= K:
            ans += 1
print(ans)

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
K, S = list(map(int, input().split()))
ans = 0
for z in range(K + 1):
    for y in range(K + 1):
        x = S - (z + y)
        if 0 <= x <= K:
            ans += 1
print(ans)


Input:
n=int(input())

task=[]

for _ in range(n):

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

    task.append((b, -a))

task.sort()

time=0

flag=True

for i in task:

    time-=i[1]

    if time>i[0]:

        flag=False

        break

if flag:

    print("Yes")

else:

    print("No")

import sys

from collections import defaultdict

from operator import itemgetter

input = sys.stdin.readline

N = int(input())

d = defaultdict(int)

for _ in range(N):

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

    d[b] += a

task = list(d.items())

task.sort()

now = 0

ans = "Yes"

for limit, time in task:

    now += time

    if now > limit:

        ans = "No"

        break

print(ans)

n = int(input())
task = []
for _ in range(n):
    a, b = list(map(int, input().split()))
    task.append((b, -a))
task.sort()
time = 0
flag = True
for i in task:
    time -= i[1]
    if time > i[0]:
        flag = False
        break
if flag:
    print("Yes")
else:
    print("No")

-n = int(input())
-task = []
-for _ in range(n):
+import sys
+from collections import defaultdict
+from operator import itemgetter
+
+input = sys.stdin.readline
+N = int(input())
+d = defaultdict(int)
+for _ in range(N):
-    task.append((b, -a))
+    d[b] += a
+task = list(d.items())
-time = 0
-flag = True
-for i in task:
-    time -= i[1]
-    if time > i[0]:
-        flag = False
+now = 0
+ans = "Yes"
+for limit, time in task:
+    now += time
+    if now > limit:
+        ans = "No"
-if flag:
-    print("Yes")
-else:
-    print("No")
+print(ans)


Input:
n,q=list(map(int,input().split()));s="_"+input()+"_";l,r=0,n+1

for(m,h)in[input().split()for i in range(q)][::-1]:L=h=="L";R=h=="R";l,r=[l,l-1,l+1][(m==s[l]and R)-(m==s[l+1]and L)],[r,r-1,r+1][(m==s[r-1]and R)-(m==s[r]and L)]

print((max(0,r-l-1)))

n, q = list(map(int, input().split()))
s = "_" + input() + "_"
l, r = 0, n + 1
for (m, h) in [input().split() for i in range(q)][::-1]:
    L = h == "L"
    R = h == "R"
    l += (m == s[l + 1] and L) - (m == s[l] and R)
    r += (m == s[r] and L) - (m == s[r - 1] and R)
print((max(0, r - l - 1)))


Input:
import math

n = int(input())

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

n = int(input())

p = 1

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

  p = p * i % (10 ** 9 + 7)

print(p)

import math

n = int(input())
print((math.factorial(n) % (10**9 + 7)))

n = int(input())
p = 1
for i in range(1, n + 1):
    p = p * i % (10**9 + 7)
print(p)

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


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

print((b if 13<=a else b//2 if 6<=a else 0))

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

print((b if 12<a else b//2 if 5<a else 0))

a, b = list(map(int, input().split()))
print((b if 13 <= a else b // 2 if 6 <= a else 0))

a, b = list(map(int, input().split()))
print((b if 12 < a else b // 2 if 5 < a else 0))

-print((b if 13 <= a else b // 2 if 6 <= a else 0))

+print((b if 12 < a else b // 2 if 5 < a else 0))


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

con = 0

ans = list(int() for _ in range(100000))

pre = 0

for _ in range(K):

    D = int(input())

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

    for j in range(D):

        ans[pre+j]=arr[j]

    pre += D

fin = (N+1) - len(set(ans))

print(fin)

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

con = 0

ans = list(int() for _ in range(10001))

pre = 0

for _ in range(K):

    D = int(input())

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

    for j in range(D):

        ans[pre+j]=arr[j]

    pre += D

fin = (N+1) - len(set(ans))

print(fin)

N, K = list(map(int, input().split()))
con = 0
ans = list(int() for _ in range(100000))
pre = 0
for _ in range(K):
    D = int(input())
    arr = list(map(int, input().split()))
    for j in range(D):
        ans[pre + j] = arr[j]
    pre += D
fin = (N + 1) - len(set(ans))
print(fin)

N, K = list(map(int, input().split()))
con = 0
ans = list(int() for _ in range(10001))
pre = 0
for _ in range(K):
    D = int(input())
    arr = list(map(int, input().split()))
    for j in range(D):
        ans[pre + j] = arr[j]
    pre += D
fin = (N + 1) - len(set(ans))
print(fin)

-ans = list(int() for _ in range(100000))

+ans = list(int() for _ in range(10001))


Input:
def main():

  n = int(input())

  a = [0]

  b = [0]

  c = [0]

  for i in range(n):

      aa,bb,cc = list(map(int,input().split()))

      a.append(max(b[i] + aa, c[i] + aa))

      b.append(max(a[i] + bb, c[i] + bb))

      c.append(max(a[i] + cc, b[i] + cc))

  print((max(a[-1], b[-1], c[-1])))

main()

def main():

    n = int(input())

    a = 0

    b = 0

    c = 0

    for i in range(n):

        aa,bb,cc = list(map(int,input().split()))

        a,b,c = max(b,c)+aa, max(a,c)+bb, max(a,b)+cc

    print((max(a,b,c)))

main()

def main():
    n = int(input())
    a = [0]
    b = [0]
    c = [0]
    for i in range(n):
        aa, bb, cc = list(map(int, input().split()))
        a.append(max(b[i] + aa, c[i] + aa))
        b.append(max(a[i] + bb, c[i] + bb))
        c.append(max(a[i] + cc, b[i] + cc))
    print((max(a[-1], b[-1], c[-1])))

main()

def main():
    n = int(input())
    a = 0
    b = 0
    c = 0
    for i in range(n):
        aa, bb, cc = list(map(int, input().split()))
        a, b, c = max(b, c) + aa, max(a, c) + bb, max(a, b) + cc
    print((max(a, b, c)))

main()

-        a.append(max(b[i] + aa, c[i] + aa))

-        b.append(max(a[i] + bb, c[i] + bb))

-        c.append(max(a[i] + cc, b[i] + cc))

+        a, b, c = max(b, c) + aa, max(a, c) + bb, max(a, b) + cc


Input:
import sys

e=[list(map(int,e.split()))for e in sys.stdin]

n=e[0][0]+1

for c in e[1:n]:

 t=''

 for l in zip(*e[n:]):t+=f'{sum(s*t for s,t in zip(c,l))} '

 print((t[:-1]))

import sys

e=[list(map(int,e.split()))for e in sys.stdin]

n=e[0][0]+1

t=''

for c in e[1:n]:

 for l in zip(*e[n:]):t+=f'{sum(s*t for s,t in zip(c,l))} '

 t=t[:-1]+'\n'

print((t[:-1]))

import sys

e = [list(map(int, e.split())) for e in sys.stdin]
n = e[0][0] + 1
for c in e[1:n]:
    t = ""
    for l in zip(*e[n:]):
        t += f"{sum(s*t for s,t in zip(c,l))} "
    print((t[:-1]))

import sys

e = [list(map(int, e.split())) for e in sys.stdin]
n = e[0][0] + 1
t = ""
for c in e[1:n]:
    for l in zip(*e[n:]):
        t += f"{sum(s*t for s,t in zip(c,l))} "
    t = t[:-1] + "\n"
print((t[:-1]))


Input:
x = int(input())

if x == 0:

  print((1))

else:

  print((0))


Input:
from sys import stdin,stdout

if __name__=="__main__":

    n=int(stdin.readline())

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

    b=[int(x) for x in stdin.readline().split()]

    cnt=0

    for i in range(n):

        if(a[i]>=b[i]):

            cnt+=b[i]

        else:

            cnt+=a[i]

            d=b[i]-a[i]

            cnt+=min(a[i+1],d)

            if(d>a[i+1]):

                a[i+1]=0

            else: a[i+1]-=d

    stdout.write(str(cnt)+"\n")

n = int(input())

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

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

a1 = sum(A)

for i in range(n):

  b = B[i]

  if A[i] >= b:A[i] -= b

  else:

    if A[i+1]+A[i] >= b:

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

      A[i] = 0

    else:A[i], A[i+1] = 0, 0

a2 = sum(A)

print((a1-a2))

from sys import stdin, stdout

if __name__ == "__main__":
    n = int(stdin.readline())
    a = [int(x) for x in stdin.readline().split()]
    b = [int(x) for x in stdin.readline().split()]
    cnt = 0
    for i in range(n):
        if a[i] >= b[i]:
            cnt += b[i]
        else:
            cnt += a[i]
            d = b[i] - a[i]
            cnt += min(a[i + 1], d)
            if d > a[i + 1]:
                a[i + 1] = 0
            else:
                a[i + 1] -= d
    stdout.write(str(cnt) + "\n")

n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a1 = sum(A)
for i in range(n):
    b = B[i]
    if A[i] >= b:
        A[i] -= b
    else:
        if A[i + 1] + A[i] >= b:
            A[i + 1] = A[i + 1] + A[i] - b
            A[i] = 0
        else:
            A[i], A[i + 1] = 0, 0
a2 = sum(A)
print((a1 - a2))

-    a = [int(x) for x in stdin.readline().split()]

-    b = [int(x) for x in stdin.readline().split()]


Input:
counter = 0

c = list(map(int, input().split(' ')))

for i in range(c[0], c[1] + 1):

  if str(i)[0] != str(i)[4]:

    continue

  if str(i)[1] != str(i)[3]:

    continue

  counter += 1

print(counter)

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

counter = 0

for i in range(a[0], a[1] + 1):

  stri = str(i)

  if (stri[0] == stri[4] and stri[1] == stri[3]):

    counter += 1

print(counter)

counter = 0
c = list(map(int, input().split(" ")))
for i in range(c[0], c[1] + 1):
    if str(i)[0] != str(i)[4]:
        continue
    if str(i)[1] != str(i)[3]:
        continue
    counter += 1
print(counter)

a = list(map(int, input().split()))
counter = 0
for i in range(a[0], a[1] + 1):
    stri = str(i)
    if stri[0] == stri[4] and stri[1] == stri[3]:
        counter += 1
print(counter)

+    if stri[0] == stri[4] and stri[1] == stri[3]:


Input:
I=lambda:list(map(int,input().split()))

n,m=I()

a=I()

a.sort(reverse=True)

qq=[I() for i in range(m)]

qq.sort(key=lambda x:x[1],reverse=True)

ans=0

for x,y in qq:

    for j in range(x):

        if not a:

            break

        aa=a.pop()

        ans+=max(aa,y)

ans+=sum(a)

print(ans)

I=lambda : list(map(int,input().split()))

n,m = I()

l=I();s=sum(l);l.sort();d=[]

for _ in range(m):

    b,c=I()

    d.append([c,b])

p=s=0

for i in sorted(d,reverse=True):

    while i[1]:

        if p>=n or l[p]>=i[0]:

            break

        i[1]-=1

        s+=i[0]

        p+=1

    if p>=n:

        break

s+=sum(l[p:])

print(s)

I = lambda: list(map(int, input().split()))
n, m = I()
a = I()
a.sort(reverse=True)
qq = [I() for i in range(m)]
qq.sort(key=lambda x: x[1], reverse=True)
ans = 0
for x, y in qq:
    for j in range(x):
        if not a:
            break
        aa = a.pop()
        ans += max(aa, y)
ans += sum(a)
print(ans)

I = lambda: list(map(int, input().split()))
n, m = I()
l = I()
s = sum(l)
l.sort()
d = []
for _ in range(m):
    b, c = I()
    d.append([c, b])
p = s = 0
for i in sorted(d, reverse=True):
    while i[1]:
        if p >= n or l[p] >= i[0]:
            break
        i[1] -= 1
        s += i[0]
        p += 1
    if p >= n:
        break
s += sum(l[p:])
print(s)

-qq.sort(key=lambda x: x[1], reverse=True)


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

e=(a-b)|(b-c)

print((bool(e|(a|b|c)%2)*(e^~-e).bit_length()-1))

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

e=(a-b)|(b-c)

print((bool(e|b%2)*(e^~-e).bit_length()-1))

a, b, c = list(map(int, input().split()))
e = (a - b) | (b - c)
print((bool(e | (a | b | c) % 2) * (e ^ ~-e).bit_length() - 1))

a, b, c = list(map(int, input().split()))
e = (a - b) | (b - c)
print((bool(e | b % 2) * (e ^ ~-e).bit_length() - 1))

+print((bool(e | b % 2) * (e ^ ~-e).bit_length() - 1))


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

L = [0 for i in range(N+1)]

R = [0 for i in range(N+1)]

for i in range(M):

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

    L[l] += 1

    R[r] += 1

cnt = 0

tmp = 0

for i in range(N+1):

    tmp += L[i]

    if tmp == M:

        cnt += 1

    tmp -= R[i]

print(cnt)

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

ll = 1

rr = n

for i in range(m):

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

    if l > ll:

        ll = l

    if r < rr:

        rr = r

print((max(rr - ll + 1, 0)))

N, M = list(map(int, input().split()))
L = [0 for i in range(N + 1)]
R = [0 for i in range(N + 1)]
for i in range(M):
    l, r = list(map(int, input().split()))
    L[l] += 1
    R[r] += 1
cnt = 0
tmp = 0
for i in range(N + 1):
    tmp += L[i]
    if tmp == M:
        cnt += 1
    tmp -= R[i]
print(cnt)

n, m = list(map(int, input().split()))
ll = 1
rr = n
for i in range(m):
    l, r = list(map(int, input().split()))
    if l > ll:
        ll = l
    if r < rr:
        rr = r
print((max(rr - ll + 1, 0)))

-N, M = list(map(int, input().split()))
-L = [0 for i in range(N + 1)]
-R = [0 for i in range(N + 1)]
-for i in range(M):
+n, m = list(map(int, input().split()))
+ll = 1
+rr = n
+for i in range(m):
-    L[l] += 1
-    R[r] += 1
-cnt = 0
-tmp = 0
-for i in range(N + 1):
-    tmp += L[i]
-    if tmp == M:
-        cnt += 1
-    tmp -= R[i]
-print(cnt)
+    if l > ll:
+        ll = l
+    if r < rr:
+        rr = r
+print((max(rr - ll + 1, 0)))


Input:
import math

def lcm(x, y):

    return (x * y) // math.gcd(x, y)

N = int(input())

print((lcm(N, 2)))

N = int(input())

if N % 2 == 0:

    print(N)

else:

    print((2 * N))

-import math
-def lcm(x, y):
-    return (x * y) // math.gcd(x, y)
-print((lcm(N, 2)))
+if N % 2 == 0:
+    print(N)
+else:
+    print((2 * N))


Input:
import sys

n, m, *lr = list(map(int, sys.stdin.read().split()))

lr = list(zip(*[iter(lr)] * 2))

def main():

    cnt = [0] * (n + 1)

    for l, r in lr:

        cnt[l-1] += 1

        cnt[r] -= 1

    for i in range(n):

        cnt[i+1] += cnt[i]

    res = 0

    for i in range(n):

        res += (cnt[i] == m) & 1

    return res

if __name__ == '__main__':

    ans = main()

    print(ans)

import sys

n, m, *lr = list(map(int, sys.stdin.read().split()))

l, r = lr[::2], lr[1::2]

def main():

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

    return ans

if __name__ == '__main__':

    ans = main()

    print(ans)

import sys

n, m, *lr = list(map(int, sys.stdin.read().split()))
lr = list(zip(*[iter(lr)] * 2))

def main():
    cnt = [0] * (n + 1)
    for l, r in lr:
        cnt[l - 1] += 1
        cnt[r] -= 1
    for i in range(n):
        cnt[i + 1] += cnt[i]
    res = 0
    for i in range(n):
        res += (cnt[i] == m) & 1
    return res

if __name__ == "__main__":
    ans = main()
    print(ans)

import sys

n, m, *lr = list(map(int, sys.stdin.read().split()))
l, r = lr[::2], lr[1::2]

def main():
    ans = max(min(r) - max(l) + 1, 0)
    return ans

if __name__ == "__main__":
    ans = main()
    print(ans)

-lr = list(zip(*[iter(lr)] * 2))
+l, r = lr[::2], lr[1::2]
-    cnt = [0] * (n + 1)
-    for l, r in lr:
-        cnt[l - 1] += 1
-        cnt[r] -= 1
-    for i in range(n):
-        cnt[i + 1] += cnt[i]
-    res = 0
-    for i in range(n):
-        res += (cnt[i] == m) & 1
-    return res
+    ans = max(min(r) - max(l) + 1, 0)
+    return ans


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

height = []

for _ in range(N) :

    height.append(int(input()))

height.sort()

ans = float('inf')

for i in range(N-K+1) :

    diff = height[i + K - 1] - height[i]

    ans = min(ans, diff)

print(ans)

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

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

H.sort()

ans = 10**18

for i in range(N - K + 1):

    ans = min(ans, H[i + K - 1] - H[i])

print(ans)

N, K = list(map(int, input().split()))
height = []
for _ in range(N):
    height.append(int(input()))
height.sort()
ans = float("inf")
for i in range(N - K + 1):
    diff = height[i + K - 1] - height[i]
    ans = min(ans, diff)
print(ans)

-    diff = height[i + K - 1] - height[i]

+    ans = min(ans, H[i + K - 1] - H[i])


Input:
a, b, c, d = int(input()), int(input()), int(input()), int(input()),

print((min(a, b)+min(c, d)))

print((min(int(input()), int(input())) + min(int(input()), int(input()))))

a, b, c, d = (
    int(input()),
    int(input()),
    int(input()),
    int(input()),
)
print((min(a, b) + min(c, d)))

print(
    (
        min(int(input()), int(input()))
        + min(int(input()), int(input()))
    )
)

+        min(int(input()), int(input()))

+        + min(int(input()), int(input()))


Input:
def popcount(x):

  r = 0

  while x:

    if x & 1:

      r += 1

    x >>= 1

  return r

n = int(input())

r = list(range(n))

a = [[0, 0] for _ in r]

for i in r:

  for _ in range(int(input())):

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

    a[i][y] |= 1 << (x - 1)

m = 0

l = (1 << n) - 1

for x in range(1, l + 1):

  if all(a[i][0] & x == a[i][1] & l - x == 0 for i in r if x >> i & 1):

    m = max(m, popcount(x))

print(m)

from itertools import combinations

n = int(input())

r = list(range(n))

a = [(set(), set()) for _ in r]

for i in r:

  for _ in range(int(input())):

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

    a[i][y].add(x - 1)

fail = lambda x: not all(a[i][0].isdisjoint(x) and a[i][1] < x for i in x)

while all(map(fail, list(map(set, combinations(r, n))))):

  n -= 1

print(n)

+fail = lambda x: not all(a[i][0].isdisjoint(x) and a[i][1] < x for i in x)

+while all(map(fail, list(map(set, combinations(r, n))))):


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

if N==1:

    nmax=M

else:

    nmax = 1

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

        if M%i==0 and M//i>=N:

            nmax = max(nmax,i)

        if M%i==0 and i>=N:

            nmax = max(nmax,M//i)

print(nmax)

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

amax = 1

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

    if M%i==0:

        a = i

        b = M//i

        if b>=N:

            amax = max(amax,a)

        a,b = b,a

        if b>=N:

            amax = max(amax,a)

print(amax)

N, M = list(map(int, input().split()))
if N == 1:
    nmax = M
else:
    nmax = 1
    for i in range(2, int(M**0.5) + 1):
        if M % i == 0 and M // i >= N:
            nmax = max(nmax, i)
        if M % i == 0 and i >= N:
            nmax = max(nmax, M // i)
print(nmax)

N, M = list(map(int, input().split()))
amax = 1
for i in range(1, int(M**0.5) + 1):
    if M % i == 0:
        a = i
        b = M // i
        if b >= N:
            amax = max(amax, a)
        a, b = b, a
        if b >= N:
            amax = max(amax, a)
print(amax)

-    for i in range(2, int(M**0.5) + 1):


Input:
N = int(input())

txy = [(0, 0, 0)] + [tuple(map(int, input().split())) for _ in range(N)]

for i in range(N):

    t1, x1, y1 = txy[i]

    t2, x2, y2 = txy[i+1]

    if abs(x2-x1) + abs(y2-y1) <= t2 - t1 and (x1+y1+x2+y2) % 2 == (t2-t1) % 2:

        continue

    else:

        print('No')

        exit()

print('Yes')

N = int(input())

t0 = x0 = y0 = 0

for _ in range(N):

    t1, x1, y1 = list(map(int, input().split()))

    d = abs(x1 - x0) + abs(y1 - y0)

    if d > t1 - t0 or d % 2 != (t1 - t0) % 2:

        print('No')

        exit()

    t0, x0, y0 = t1, x1, y1

print('Yes')

N = int(input())
txy = [(0, 0, 0)] + [tuple(map(int, input().split())) for _ in range(N)]
for i in range(N):
    t1, x1, y1 = txy[i]
    t2, x2, y2 = txy[i + 1]
    if (
        abs(x2 - x1) + abs(y2 - y1) <= t2 - t1
        and (x1 + y1 + x2 + y2) % 2 == (t2 - t1) % 2
    ):
        continue
    else:
        print("No")
        exit()
print("Yes")

N = int(input())
t0 = x0 = y0 = 0
for _ in range(N):
    t1, x1, y1 = list(map(int, input().split()))
    d = abs(x1 - x0) + abs(y1 - y0)
    if d > t1 - t0 or d % 2 != (t1 - t0) % 2:
        print("No")
        exit()
    t0, x0, y0 = t1, x1, y1
print("Yes")

-txy = [(0, 0, 0)] + [tuple(map(int, input().split())) for _ in range(N)]

+    t1, x1, y1 = list(map(int, input().split()))


Input:
d=[0]*6**8

n,k,*a=list(map(int,open(0).read().split()+d))

for i in range(n):d[i]=max(d[j]for j in range(i-99,i+1)if abs(a[i]-a[j])<=k)+1

print((max(d)))

d=[0]*6**8

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

a=d+a

while n:d[n]=max(d[j]for j in range(n,n+99)if abs(a[-n]-a[-j])<=k)+1;n-=1

print((max(d)))

d = [0] * 6**8
n, k, *a = list(map(int, open(0).read().split() + d))
for i in range(n):
    d[i] = max(d[j] for j in range(i - 99, i + 1) if abs(a[i] - a[j]) <= k) + 1
print((max(d)))

d = [0] * 6**8
n, k, *a = list(map(int, open(0).read().split()))
a = d + a
while n:
    d[n] = max(d[j] for j in range(n, n + 99) if abs(a[-n] - a[-j]) <= k) + 1
    n -= 1
print((max(d)))

-n, k, *a = list(map(int, open(0).read().split() + d))

-    d[i] = max(d[j] for j in range(i - 99, i + 1) if abs(a[i] - a[j]) <= k) + 1

+n, k, *a = list(map(int, open(0).read().split()))

+    d[n] = max(d[j] for j in range(n, n + 99) if abs(a[-n] - a[-j]) <= k) + 1


Input:
a , b = input().split()

a = int(a)

b = int(b)

count=0

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

    test = list(str(i))

    if (test[0] == test[4]) and (test[1]==test[3]):

        count=count+1

print(count)

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

ct=0

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

    s=str(i)

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

        ct+=1

print(ct)

a, b = input().split()
a = int(a)
b = int(b)
count = 0
for i in range(a, b + 1):
    test = list(str(i))
    if (test[0] == test[4]) and (test[1] == test[3]):
        count = count + 1
print(count)

A, B = list(map(int, input().split()))
ct = 0
for i in range(A, B + 1):
    s = str(i)
    if s == s[::-1]:
        ct += 1
print(ct)


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

O=0

for X in range(K+1):

  for Y in range(K+1):

    Z=S-X-Y

    if 0<=Z<=K:

      O+=1

print(O)

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

ans=0

for X in range(K+1):

    for Y in range(K+1):

        Z=S-X-Y

        if 0<=Z<=K:

            ans+=1

print(ans)

K, S = list(map(int, input().split()))
O = 0
for X in range(K + 1):
    for Y in range(K + 1):
        Z = S - X - Y
        if 0 <= Z <= K:
            O += 1
print(O)

K, S = list(map(int, input().split()))
ans = 0
for X in range(K + 1):
    for Y in range(K + 1):
        Z = S - X - Y
        if 0 <= Z <= K:
            ans += 1
print(ans)

-O = 0
+ans = 0
-            O += 1
-print(O)
+            ans += 1
+print(ans)


Input:
n = int(input())

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

aa = []

for i in range(n):

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

b = (sorted(aa)[n//2])

ans = 0

for i in range(n):

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

print(ans)

N= int(input())

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

B = []

for i in range(N):

    B.append(A[i]-(i+1))

#print(B)

b= (sorted(B)[N//2])

#print(b)

ans = 0

for i in range(N):

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

print(ans)

n = int(input())
a = list(map(int, input().split()))
aa = []
for i in range(n):
    aa.append(a[i] - (i + 1))
b = sorted(aa)[n // 2]
ans = 0
for i in range(n):
    ans += abs(a[i] - (b + i + 1))
print(ans)

N = int(input())
A = list(map(int, input().split()))
B = []
for i in range(N):
    B.append(A[i] - (i + 1))
# print(B)
b = sorted(B)[N // 2]
# print(b)
ans = 0
for i in range(N):
    ans += abs(B[i] - b)
print(ans)

-n = int(input())
-a = list(map(int, input().split()))
-aa = []
-for i in range(n):
-    aa.append(a[i] - (i + 1))
-b = sorted(aa)[n // 2]
+N = int(input())
+A = list(map(int, input().split()))
+B = []
+for i in range(N):
+    B.append(A[i] - (i + 1))
+# print(B)
+b = sorted(B)[N // 2]
+# print(b)
-for i in range(n):
-    ans += abs(a[i] - (b + i + 1))
+for i in range(N):
+    ans += abs(B[i] - b)


Input:
import collections

n = int(input())

a=[input() for _ in range(n)]

anslist=[]

b=collections.Counter(a)

c=max(list(b.values()))

for i in b:

    if b[i]==c:

        anslist.append(i)

d=sorted(anslist)

for i in tuple(d):

    print(i)

from collections import Counter

n=int(input())

a=[]

for i in range(n):

  a.append(input())

ac=Counter(a)

acs=sorted(list(ac.items()),key=lambda x:x[1],reverse=1)

z=len(acs)

i=0

temp=[]

while i<=z-1 and acs[i][1]==acs[0][1] :

  temp.append(acs[i][0])

  i+=1

temp.sort()

for i in temp:

  print(i)

import collections

n = int(input())
a = [input() for _ in range(n)]
anslist = []
b = collections.Counter(a)
c = max(list(b.values()))
for i in b:
    if b[i] == c:
        anslist.append(i)
d = sorted(anslist)
for i in tuple(d):
    print(i)

from collections import Counter

n = int(input())
a = []
for i in range(n):
    a.append(input())
ac = Counter(a)
acs = sorted(list(ac.items()), key=lambda x: x[1], reverse=1)
z = len(acs)
i = 0
temp = []
while i <= z - 1 and acs[i][1] == acs[0][1]:
    temp.append(acs[i][0])
    i += 1
temp.sort()
for i in temp:
    print(i)

+acs = sorted(list(ac.items()), key=lambda x: x[1], reverse=1)


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

xy = max(x,y)*2+1

print((min([c*i + max(0,a*(x-i//2)) +max(0,b*(y-i//2)) for i in range(0,xy,2)])))

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

if x < y:

    a, b, x, y = b, a, y, x

print((min(a*x+b*y,2*c*y + a*(x-y),2*c*x)))

a, b, c, x, y = list(map(int, input().split()))
xy = max(x, y) * 2 + 1
print(
    (
        min(
            [
                c * i + max(0, a * (x - i // 2)) + max(0, b * (y - i // 2))
                for i in range(0, xy, 2)
            ]
        )
    )
)

a, b, c, x, y = list(map(int, input().split()))
if x < y:
    a, b, x, y = b, a, y, x
print((min(a * x + b * y, 2 * c * y + a * (x - y), 2 * c * x)))

-                for i in range(0, xy, 2)


Input:
while True:

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

    if n == x == 0:

        break

    count = 0

    for s in range(1, n - 1):

        for m in range(s + 1, n):

            for e in range(m + 1, n + 1):

                if x == sum([s, m, e]):

                    count += 1

    print(count)

while True:

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

    if n == x == 0:

        break

    count = 0

    for s in range(1, n - 1):

        for m in range(s + 1, n):

            for e in range(m + 1, n + 1):

                if x == s+m+e:

                    count += 1

    print(count)

while True:
    n, x = [int(i) for i in input().split()]
    if n == x == 0:
        break
    count = 0
    for s in range(1, n - 1):
        for m in range(s + 1, n):
            for e in range(m + 1, n + 1):
                if x == s + m + e:
                    count += 1
    print(count)


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

L = [0] + [int(x) for x in input().split()]

D = [0] * (N+2)

answer = 1

for n in range(2,N+2):

  D[n] = D[n-1] + L[n-1]

  if D[n] <= X:

    answer += 1

print(answer)

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

import itertools

N,X,*L = list(map(int,read().split()))

answer = sum(x <= X for x in itertools.accumulate([0]+L))

print(answer)

N, X = list(map(int, input().split()))
L = [0] + [int(x) for x in input().split()]
D = [0] * (N + 2)
answer = 1
for n in range(2, N + 2):
    D[n] = D[n - 1] + L[n - 1]
    if D[n] <= X:
        answer += 1
print(answer)

import sys

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

N, X, *L = list(map(int, read().split()))
answer = sum(x <= X for x in itertools.accumulate([0] + L))
print(answer)

-L = [0] + [int(x) for x in input().split()]

+N, X, *L = list(map(int, read().split()))

+answer = sum(x <= X for x in itertools.accumulate([0] + L))


Input:
mod = 2019

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

if r//mod - l//mod > 0:

  print((0))

else:

  l %= mod

  r %= mod

  ans = mod

  for i in range(l+1, r+1):

    for j in range(l, i):

      ans = min(i*j%mod, ans)

    if ans == 0:

      break

  print(ans)

mod = 2019

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

if r//mod - l//mod > 0:

  print((0))

else:

  l %= mod

  r %= mod

  ans = mod

  for i in range(l, r):

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

      ans = min(i*j%mod, ans)

    if ans == 0:

      break

  print(ans)

mod = 2019
l, r = list(map(int, input().split()))
if r // mod - l // mod > 0:
    print((0))
else:
    l %= mod
    r %= mod
    ans = mod
    for i in range(l + 1, r + 1):
        for j in range(l, i):
            ans = min(i * j % mod, ans)
        if ans == 0:
            break
    print(ans)

mod = 2019
l, r = list(map(int, input().split()))
if r // mod - l // mod > 0:
    print((0))
else:
    l %= mod
    r %= mod
    ans = mod
    for i in range(l, r):
        for j in range(i + 1, r + 1):
            ans = min(i * j % mod, ans)
        if ans == 0:
            break
    print(ans)

-    for i in range(l + 1, r + 1):
-        for j in range(l, i):
+    for i in range(l, r):
+        for j in range(i + 1, r + 1):


Input:
prim=[True]*1000000

prim[0],prim[1]=False,False

for i in range(2,1000):

    if prim[i]:

        for j in range(i*2,1000000,i):

            prim[j]=False

prime=[i for i,j in enumerate(prim) if j==True]

while True:

    n=int(input())

    if n==0:

        break

    print((sum(prime[:n])))

prim=[True]*1000000

prim[0]=prim[1]=False

for i in range(2,350):

    if prim[i]:

        for j in range(i*2,110000,i):

            prim[j]=False

prime=[i for i,j in enumerate(prim) if j==True]

while True:

    n=int(input())

    if n==0:

        break

    print((sum(prime[:n])))

prim = [True] * 1000000
prim[0], prim[1] = False, False
for i in range(2, 1000):
    if prim[i]:
        for j in range(i * 2, 1000000, i):
            prim[j] = False
prime = [i for i, j in enumerate(prim) if j == True]
while True:
    n = int(input())
    if n == 0:
        break
    print((sum(prime[:n])))

prim = [True] * 1000000
prim[0] = prim[1] = False
for i in range(2, 350):
    if prim[i]:
        for j in range(i * 2, 110000, i):
            prim[j] = False
prime = [i for i, j in enumerate(prim) if j == True]
while True:
    n = int(input())
    if n == 0:
        break
    print((sum(prime[:n])))


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

gate = []

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

for i in range(1, m):

    m, s = list(map(int, input().split()))

    if l < m:

        l = m

    if s < r:

        r = s

print((len([i for i in range(l, r + 1)])))

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

l, r = [0] * m, [0] * m

for i in range(m):

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

print((max(min(r) - max(l) + 1, 0)))

-    m, s = list(map(int, input().split()))

-print((len([i for i in range(l, r + 1)])))

+    l[i], r[i] = list(map(int, input().split()))


Input:
K=int(input())

if K%2==0:

    print((-1))

    exit(0)

ans=[0]*(pow(10,6)+1)

ans[0]=0

for i in range(1,pow(10,6)):

    ans[i]=(ans[i-1]*10+7)%K

    if ans[i]==0:

        print(i)

        exit(0)

print((-1))

import functools

K=int(input())

if K%2==0:

    print((-1))

    exit(0)

mod=0

for i in range(1,pow(10,6)):

    mod=(mod*10+7)%K

    if mod==0:

        print(i)

        exit(0)

print((-1))

import functools

K = int(input())
if K % 2 == 0:
    print((-1))
    exit(0)
mod = 0
for i in range(1, pow(10, 6)):
    mod = (mod * 10 + 7) % K
    if mod == 0:
        print(i)
        exit(0)
print((-1))


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

L = []

R = []

for _ in range(M):

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

  L.append(l)

  R.append(r)

ans = min(R)-max(L)+1

if ans < 0:

  print((0))

else:

  print(ans)

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

max_l = float("-inf")

min_r = float("inf")

for _ in range(m):

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

  max_l = max(max_l, l)

  min_r = min(min_r, r)

ans = min_r - max_l + 1

if ans > 0:

  print(ans)

else:

  print((0))

N, M = list(map(int, input().split()))
L = []
R = []
for _ in range(M):
    l, r = list(map(int, input().split()))
    L.append(l)
    R.append(r)
ans = min(R) - max(L) + 1
if ans < 0:
    print((0))
else:
    print(ans)

n, m = list(map(int, input().split()))
max_l = float("-inf")
min_r = float("inf")
for _ in range(m):
    l, r = list(map(int, input().split()))
    max_l = max(max_l, l)
    min_r = min(min_r, r)
ans = min_r - max_l + 1
if ans > 0:
    print(ans)
else:
    print((0))

-N, M = list(map(int, input().split()))
-L = []
-R = []
-for _ in range(M):
+n, m = list(map(int, input().split()))
+max_l = float("-inf")
+min_r = float("inf")
+for _ in range(m):
-    L.append(l)
-    R.append(r)
-ans = min(R) - max(L) + 1
-if ans < 0:
+    max_l = max(max_l, l)
+    min_r = min(min_r, r)
+ans = min_r - max_l + 1
+if ans > 0:
+    print(ans)
+else:
-else:
-    print(ans)


Input:
N = int(input())

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

cnt = 0

for num in seq:

    while num % 2 == 0:

        cnt += 1

        num /= 2

print(cnt)

n = int(input())

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

ans = 0

for num in a:

    ans += format(num, 'b')[::-1].find('1')

print(ans)

n = int(input())
a = list(map(int, input().split()))
ans = 0
for num in a:
    ans += format(num, "b")[::-1].find("1")
print(ans)


Input:
import math

n = int(input())

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

n = int(input())

ans = 1

mod = 10**9+7

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

    ans *= i

    ans %= mod

ans %= mod

print(ans)

import math

n = int(input())
print((math.factorial(n) % (10**9 + 7)))

n = int(input())
ans = 1
mod = 10**9 + 7
for i in range(1, n + 1):
    ans *= i
    ans %= mod
ans %= mod
print(ans)

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


Input:
n,t=int(input()),list(map(int,input().split()));T=sum(t)

print(*[T-t[i-1]+j for i,j in [list(map(int,input().split())) for _ in range(int(input()))]],sep='\n')

input();t=list(map(int,input().split()));T=sum(t)

print(*[T-t[i-1]+j for i,j in [list(map(int,input().split())) for _ in range(int(input()))]],sep='\n')

n, t = int(input()), list(map(int, input().split()))
T = sum(t)
print(
    *[
        T - t[i - 1] + j
        for i, j in [list(map(int, input().split())) for _ in range(int(input()))]
    ],
    sep="\n"
)

input()
t = list(map(int, input().split()))
T = sum(t)
print(
    *[
        T - t[i - 1] + j
        for i, j in [list(map(int, input().split())) for _ in range(int(input()))]
    ],
    sep="\n"
)

-n, t = int(input()), list(map(int, input().split()))


Input:
n = int(input())

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

S = [0]*(n+1)

num={}

for i in range(n):

    S[i+1] = S[i] + A[i]

for i in S:

    if str(i) in list(num.keys()):

        num[str(i)] += 1

    else:

        num[str(i)] = 1

out = [i*(i-1)//2 for i in list(num.values()) if i>1]

print((sum(out)))

n = int(input())

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

S = [0]*(n+1)

num = {}

for i in range(n):

    S[i+1] = S[i] + A[i]

for i in S:

    s = str(i)

    num[s] = 1 if s not in list(num.keys()) else num[s]+1

out = [i*(i-1)//2 for i in list(num.values()) if i > 1]

print((sum(out)))

n = int(input())
A = [int(i) for i in input().split()]
S = [0] * (n + 1)
num = {}
for i in range(n):
    S[i + 1] = S[i] + A[i]
for i in S:
    if str(i) in list(num.keys()):
        num[str(i)] += 1
    else:
        num[str(i)] = 1
out = [i * (i - 1) // 2 for i in list(num.values()) if i > 1]
print((sum(out)))

n = int(input())
A = [int(i) for i in input().split()]
S = [0] * (n + 1)
num = {}
for i in range(n):
    S[i + 1] = S[i] + A[i]
for i in S:
    s = str(i)
    num[s] = 1 if s not in list(num.keys()) else num[s] + 1
out = [i * (i - 1) // 2 for i in list(num.values()) if i > 1]
print((sum(out)))

+    num[s] = 1 if s not in list(num.keys()) else num[s] + 1


Input:
def resolve():

  base = 10**9+7

  S = int(input())

  dp = [0] * (S+1)

  dp[0] = 1

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

    for u in range(i-2):

      dp[i] += dp[u]

      dp[i] %= base

  # print(dp)

  print((dp[S]%base))

if __name__ == "__main__":

  resolve()

def resolve():

  base = 10**9+7

  S = int(input())

  dp = [0] * (S+1)

  dp[0] = 1

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

    dp[i] = dp[i-1] + dp[i-3]

  # print(dp)

  print((dp[S]%base))

if __name__ == "__main__":

  resolve()

def resolve():
    base = 10**9 + 7
    S = int(input())
    dp = [0] * (S + 1)
    dp[0] = 1
    for i in range(1, S + 1):
        for u in range(i - 2):
            dp[i] += dp[u]
            dp[i] %= base
    # print(dp)
    print((dp[S] % base))

if __name__ == "__main__":
    resolve()

def resolve():
    base = 10**9 + 7
    S = int(input())
    dp = [0] * (S + 1)
    dp[0] = 1
    for i in range(3, S + 1):
        dp[i] = dp[i - 1] + dp[i - 3]
    # print(dp)
    print((dp[S] % base))

if __name__ == "__main__":
    resolve()

-    for i in range(1, S + 1):
-        for u in range(i - 2):
-            dp[i] += dp[u]
-            dp[i] %= base
+    for i in range(3, S + 1):
+        dp[i] = dp[i - 1] + dp[i - 3]


Input:
n = int(input())

res = 1

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

  res = res * num

  res = res % (pow(10,9) + 7)

print(res)

n = int(input())

res = 1

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

    res = res * num

    res = res % (10**9 + 7)

print(res)

n = int(input())
res = 1
for num in range(1, n + 1):
    res = res * num
    res = res % (pow(10, 9) + 7)
print(res)

n = int(input())
res = 1
for num in range(1, n + 1):
    res = res * num
    res = res % (10**9 + 7)
print(res)


Input:
N = int(input())

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

A=[0]*N

A[0]=B[0]

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

    A[i]=min(B[i],B[i-1])

A[-1]=B[-1]

print((sum(A)))

import sys

input = sys.stdin.readline

def I(): return int(input())

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

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

def main():

    mod=10**9+7

    N=I()

    B=LI()

    A=[0]*N

    A[0]=B[0]

    A[-1]=B[-1]

    for i in range(N-2):

        A[i+1]=min(B[i],B[i+1])

    print((sum(A)))

main()

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

import sys

input = sys.stdin.readline

def I():
    return int(input())

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

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

def main():
    mod = 10**9 + 7
    N = I()
    B = LI()
    A = [0] * N
    A[0] = B[0]
    A[-1] = B[-1]
    for i in range(N - 2):
        A[i + 1] = min(B[i], B[i + 1])
    print((sum(A)))

main()

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


Input:
import sys

input = sys.stdin.readline

from collections import *

A, B, C, D = list(map(int, input().split()))

while True:

    C -= B

    if C<=0:

        print('Yes')

        exit()

    A -= D

    if A<=0:

        print('No')

        exit()

A, B, C, D = list(map(int, input().split()))

c1 = (C+B-1)//B

c2 = (A+D-1)//D

if c1<=c2:

    print('Yes')

else:

    print('No')

import sys

input = sys.stdin.readline
from collections import *

A, B, C, D = list(map(int, input().split()))
while True:
    C -= B
    if C <= 0:
        print("Yes")
        exit()
    A -= D
    if A <= 0:
        print("No")
        exit()

A, B, C, D = list(map(int, input().split()))
c1 = (C + B - 1) // B
c2 = (A + D - 1) // D
if c1 <= c2:
    print("Yes")
else:
    print("No")

-import sys
-input = sys.stdin.readline
-from collections import *
-while True:
-    C -= B
-    if C <= 0:
-        print("Yes")
-        exit()
-    A -= D
-    if A <= 0:
-        print("No")
-        exit()
+c1 = (C + B - 1) // B
+c2 = (A + D - 1) // D
+if c1 <= c2:
+    print("Yes")
+else:
+    print("No")


Input:
#!/usr/bin/env python3

import sys

from math import gcd

import math

def main():

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

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

    tmp = 1

    count = None

    for i in range(N):

      tmp = (tmp * a[i]//2) // gcd(tmp,a[i]//2)

      c = 0

      n = a[i]//2

      while n % 2 == 0:

        c += 1

        n //= 2

      if i == 0:

        count = c

      elif count != c:

        print((0))

        exit()

    print(((M//tmp +1)//2))

if __name__ == '__main__':

    main()

from math import gcd

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

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

B = [a//2 for a in A]

LCM = 1

for i in range(N):

    LCM = LCM * B[i] // gcd(LCM, B[i])

for i in range(N):

    if LCM // B[i] % 2 == 0:

        print((0))

        exit()

print(((M//LCM + 1)//2))

#!/usr/bin/env python3
import sys
from math import gcd
import math

def main():
    N, M = list(map(int, input().split()))
    a = list(map(int, input().split()))
    tmp = 1
    count = None
    for i in range(N):
        tmp = (tmp * a[i] // 2) // gcd(tmp, a[i] // 2)
        c = 0
        n = a[i] // 2
        while n % 2 == 0:
            c += 1
            n //= 2
        if i == 0:
            count = c
        elif count != c:
            print((0))
            exit()
    print(((M // tmp + 1) // 2))

if __name__ == "__main__":
    main()

from math import gcd

N, M = list(map(int, input().split()))
A = list(map(int, input().split()))
B = [a // 2 for a in A]
LCM = 1
for i in range(N):
    LCM = LCM * B[i] // gcd(LCM, B[i])
for i in range(N):
    if LCM // B[i] % 2 == 0:
        print((0))
        exit()
print(((M // LCM + 1) // 2))

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

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


Input:
a=input().split()

b=list(map(int,a))

W=b[0]

H=b[1]

x=b[2]

y=b[3]

r=b[4]

if (r <= x <= (W - r)) and (r <= y <= (H - r)):

  print('Yes')

else:

  print('No')

W,H,x,y,r = [int(i) for i in input().split()]

if (r <= x <= (W - r)) and (r <= y <= (H - r)):

  print('Yes')

else:

  print('No')

a = input().split()
b = list(map(int, a))
W = b[0]
H = b[1]
x = b[2]
y = b[3]
r = b[4]
if (r <= x <= (W - r)) and (r <= y <= (H - r)):
    print("Yes")
else:
    print("No")

W, H, x, y, r = [int(i) for i in input().split()]
if (r <= x <= (W - r)) and (r <= y <= (H - r)):
    print("Yes")
else:
    print("No")

+W, H, x, y, r = [int(i) for i in input().split()]


Input:
ia = [int(i) for i in input().split(" ")]

W=ia[0]

H=ia[1]

x=ia[2]

y=ia[3]

r=ia[4]

print(("Yes" if 0<=x-r and x+r<=W and 0<=y-r and y+r<=H else "No"))

# encoding:utf-8

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

W, H, x, y, r = input

if x - r < 0 or x + r > W:

	print("No")

elif y - r < 0 or y + r > H:

	print("No")

else:

	print("Yes")

ia = [int(i) for i in input().split(" ")]
W = ia[0]
H = ia[1]
x = ia[2]
y = ia[3]
r = ia[4]
print(("Yes" if 0 <= x - r and x + r <= W and 0 <= y - r and y + r <= H else "No"))

-ia = [int(i) for i in input().split(" ")]

-print(("Yes" if 0 <= x - r and x + r <= W and 0 <= y - r and y + r <= H else "No"))

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


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

yenmin=a*x+b*y

for zz in range(2*10**5+1):

    xx=max(x-zz//2,0)

    yy=max(y-zz//2,0)

    yen=a*xx+b*yy+c*zz

    if yen<yenmin:

        yenmin=yen

print(yenmin)

#print(xx,yy,zz)

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

cmax=max(x,y)*2

zmin=(a+b)*10**5

for nc in range(0,cmax+1,2):

    na=max(x-nc//2,0)

    nb=max(y-nc//2,0)

    z=a*na+b*nb+c*nc

    zmin=min(zmin,z)

print(zmin)

a, b, c, x, y = list(map(int, input().split()))
yenmin = a * x + b * y
for zz in range(2 * 10**5 + 1):
    xx = max(x - zz // 2, 0)
    yy = max(y - zz // 2, 0)
    yen = a * xx + b * yy + c * zz
    if yen < yenmin:
        yenmin = yen
print(yenmin)
# print(xx,yy,zz)

a, b, c, x, y = list(map(int, input().split()))
cmax = max(x, y) * 2
zmin = (a + b) * 10**5
for nc in range(0, cmax + 1, 2):
    na = max(x - nc // 2, 0)
    nb = max(y - nc // 2, 0)
    z = a * na + b * nb + c * nc
    zmin = min(zmin, z)
print(zmin)

-yenmin = a * x + b * y
-for zz in range(2 * 10**5 + 1):
-    xx = max(x - zz // 2, 0)
-    yy = max(y - zz // 2, 0)
-    yen = a * xx + b * yy + c * zz
-    if yen < yenmin:
-        yenmin = yen
-print(yenmin)
-# print(xx,yy,zz)
+cmax = max(x, y) * 2
+zmin = (a + b) * 10**5
+for nc in range(0, cmax + 1, 2):
+    na = max(x - nc // 2, 0)
+    nb = max(y - nc // 2, 0)
+    z = a * na + b * nb + c * nc
+    zmin = min(zmin, z)
+print(zmin)


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

count = 0

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

    s = str(i)

    l = len(s)

    if all(s[j] == s[l-j-1] for j in range(l)):

            count += 1

print(count)

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

count = 0

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

    s = str(i)

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

         count += 1

print(count)

a, b = list(map(int, input().split()))
count = 0
for i in range(a, b + 1):
    s = str(i)
    l = len(s)
    if all(s[j] == s[l - j - 1] for j in range(l)):
        count += 1
print(count)

a, b = list(map(int, input().split()))
count = 0
for i in range(a, b + 1):
    s = str(i)
    if s == s[::-1]:
        count += 1
print(count)

-    if all(s[j] == s[l - j - 1] for j in range(l)):


Input:
import itertools

import math

def main():

  s = input()

  t = input()

  cnt = 0

  for i in range(3):

    if s[i] == t[i]:

      cnt += 1

  print(cnt)

if __name__ == '__main__':

  main()

s = input()

t = input()

ans = 0

for i in range(3):

    if s[i] == t[i]:

        ans += 1

print(ans)

import itertools
import math

def main():
    s = input()
    t = input()
    cnt = 0
    for i in range(3):
        if s[i] == t[i]:
            cnt += 1
    print(cnt)

if __name__ == "__main__":
    main()

-import itertools
-import math
-def main():
-    s = input()
-    t = input()
-    cnt = 0
-    for i in range(3):
-        if s[i] == t[i]:
-            cnt += 1
-    print(cnt)
-if __name__ == "__main__":
-    main()
+s = input()
+t = input()
+ans = 0
+for i in range(3):
+    if s[i] == t[i]:
+        ans += 1
+print(ans)


Input:
N = int(input())

ans = N - 1

p = 1

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

    if p % 2 == 1 and i > (N ** 0.5):

        break

    if N % i == 0:

        p = i

        j = N // i

        m = i + j - 2

        if m < ans:

            ans = m

        else:

            continue

    else:

        continue

print(ans)

N = int(input())

ans = N - 1

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

    if N % i == 0:

        j = N // i

        m = i + j - 2

        ans = min(ans, m)

print(ans)

N = int(input())
ans = N - 1
p = 1
for i in range(2, int((N**0.5) + 1)):
    if p % 2 == 1 and i > (N**0.5):
        break
    if N % i == 0:
        p = i
        j = N // i
        m = i + j - 2
        if m < ans:
            ans = m
        else:
            continue
    else:
        continue
print(ans)

N = int(input())
ans = N - 1
for i in range(2, int((N**0.5) + 1)):
    if N % i == 0:
        j = N // i
        m = i + j - 2
        ans = min(ans, m)
print(ans)

-p = 1
-    if p % 2 == 1 and i > (N**0.5):
-        break
-        p = i
-        if m < ans:
-            ans = m
-        else:
-            continue
-    else:
-        continue
+        ans = min(ans, m)


Input:
def two_count(x):

	if int(str(x)[-1]) % 2 == 0:

		return 1 + two_count(x//2)

	else:

		return 0

n = int(input())

n_list = [int(n) for n in input().split(" ")]

n_two_count_list = [two_count(n) for n in n_list]

print((min(n_two_count_list)))

def two_count(x):

	if x % 2 == 0:

		return 1 + two_count(x//2)

	else:

		return 0

n = int(input())

n_list = [int(n) for n in input().split(" ")]

n_two_count_list = [two_count(n) for n in n_list]

print((min(n_two_count_list)))

def two_count(x):
    if int(str(x)[-1]) % 2 == 0:
        return 1 + two_count(x // 2)
    else:
        return 0

n = int(input())
n_list = [int(n) for n in input().split(" ")]
n_two_count_list = [two_count(n) for n in n_list]
print((min(n_two_count_list)))

def two_count(x):
    if x % 2 == 0:
        return 1 + two_count(x // 2)
    else:
        return 0

n = int(input())
n_list = [int(n) for n in input().split(" ")]
n_two_count_list = [two_count(n) for n in n_list]
print((min(n_two_count_list)))


Input:
from math import gcd

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

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

x = 1

for i in range(n):

    x *= a[i] // gcd(x, a[i])

for i in a:

    if x // i % 2 == 0:

        print((0))

        exit()

print(((m // x + 1) // 2))

from math import gcd, ceil

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

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

a = [i // 2 for i in a]

l = 1

for i in a:

    l *= i // gcd(l, i)

for i in a:

    if l // i % 2 == 0:

        print((0))

        exit()

print((ceil((m // l) / 2)))

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


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

n = 0

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

    f = True

    s = str(i)

    l = len(s)

    for j in range(l//2 + 1):

        if s[j] != s[- j - 1]:

            f = False

            break

    if f:

        n += 1

print(n)

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

n = 0

for i in range(1, 10):

    for j in range(10):

        for k in range(10):

            s = 10000 * i + 1000 * j + 100 * k + 10 * j + i

            if a <= s <= b:

                n += 1

print(n)

a, b = list(map(int, input().split()))
n = 0
for i in range(a, b + 1):
    f = True
    s = str(i)
    l = len(s)
    for j in range(l // 2 + 1):
        if s[j] != s[-j - 1]:
            f = False
            break
    if f:
        n += 1
print(n)

a, b = list(map(int, input().split()))
n = 0
for i in range(1, 10):
    for j in range(10):
        for k in range(10):
            s = 10000 * i + 1000 * j + 100 * k + 10 * j + i
            if a <= s <= b:
                n += 1
print(n)


Input:
import math

n = int(input())

cnt = 0

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

    if math.floor(math.log10(i) + 1) % 2:

        cnt += 1

print(cnt)

n = int(input())

cnt = 0

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

    if len(str(i)) % 2:

        cnt += 1

print(cnt)

-    if math.floor(math.log10(i) + 1) % 2:


Input:
from collections import defaultdict

n = int(input())

d = defaultdict(int)

for _ in range(n):

    key = input()

    d[key] += 1

dict = sorted(list(d.items()), key=lambda x:x[1], reverse=True)

max_v = max(d.values())

dict_li = []

for a in dict:

    if a[1]==max_v:

        dict_li.append(a[0])

dict_li.sort()

for s in dict_li:

    print(s)

from collections import defaultdict

n = int(input())

d = defaultdict(int)

for _ in range(n):

    key = input()

    d[key] += 1

dict = sorted(d.items(), key=lambda x:x[1], reverse=True)

max_v = max(d.values())

dict_li = []

for a in dict:

    if a[1]==max_v:

        dict_li.append(a[0])

dict_li.sort()

print(*dict_li, sep='\n')

from collections import defaultdict

n = int(input())
d = defaultdict(int)
for _ in range(n):
    key = input()
    d[key] += 1
dict = sorted(list(d.items()), key=lambda x: x[1], reverse=True)
max_v = max(d.values())
dict_li = []
for a in dict:
    if a[1] == max_v:
        dict_li.append(a[0])
dict_li.sort()
for s in dict_li:
    print(s)

from collections import defaultdict

n = int(input())
d = defaultdict(int)
for _ in range(n):
    key = input()
    d[key] += 1
dict = sorted(d.items(), key=lambda x: x[1], reverse=True)
max_v = max(d.values())
dict_li = []
for a in dict:
    if a[1] == max_v:
        dict_li.append(a[0])
dict_li.sort()
print(*dict_li, sep="\n")

-dict = sorted(list(d.items()), key=lambda x: x[1], reverse=True)

+dict = sorted(d.items(), key=lambda x: x[1], reverse=True)


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

l = [0]*(10**5+1)

for i in range(n):

    ai,bi = list(map(int,input().split()))

    l[ai]+=bi

for index,i in enumerate(l):

    if i:

        k-=i

        if k<=0:

            break

print(index)

from collections import defaultdict

from sys import stdin

input = stdin.readline

d = defaultdict(int)

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

for _ in range(n):

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

    d[a]+=b

for key in sorted(d.keys()):

    v = d[key]

    k-=v

    if k <= 0:

        break

print(key)

n, k = list(map(int, input().split()))
l = [0] * (10**5 + 1)
for i in range(n):
    ai, bi = list(map(int, input().split()))
    l[ai] += bi
for index, i in enumerate(l):
    if i:
        k -= i
        if k <= 0:
            break
print(index)

from collections import defaultdict
from sys import stdin

input = stdin.readline
d = defaultdict(int)
n, k = list(map(int, input().split()))
for _ in range(n):
    a, b = list(map(int, input().split()))
    d[a] += b
for key in sorted(d.keys()):
    v = d[key]
    k -= v
    if k <= 0:
        break
print(key)

-    ai, bi = list(map(int, input().split()))

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


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

cnt = 0

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

    l = list(str(i))

    if l == list(reversed(l)):

        cnt += 1

print(cnt)

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

cnt = 0

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

    l = str(i)

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

        cnt += 1

print(cnt)

A, B = list(map(int, input().split()))
cnt = 0
for i in range(A, B + 1):
    l = list(str(i))
    if l == list(reversed(l)):
        cnt += 1
print(cnt)

A, B = list(map(int, input().split()))
cnt = 0
for i in range(A, B + 1):
    l = str(i)
    if l == l[::-1]:
        cnt += 1
print(cnt)

-    l = list(str(i))
-    if l == list(reversed(l)):
+    l = str(i)
+    if l == l[::-1]:


Input:
N = int(input())

mem = [[0]*10 for i in range(10)]

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

    a = int(str(i)[0])

    b = int(str(i)[-1])

    mem[a][b] += 1

ans = 0

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

    a = int(str(i)[0])

    b = int(str(i)[-1])

    ans += mem[b][a]

print(ans)

N = int(input())

d = [[0]*10 for i in range(10)]

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

    s = str(n)

    d[int(s[0])][int(s[-1])] += 1

ans = 0

for i in range(10):

    for j in range(10):

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

print(ans)

N = int(input())
mem = [[0] * 10 for i in range(10)]
for i in range(1, N + 1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    mem[a][b] += 1
ans = 0
for i in range(1, N + 1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    ans += mem[b][a]
print(ans)

N = int(input())
d = [[0] * 10 for i in range(10)]
for n in range(1, N + 1):
    s = str(n)
    d[int(s[0])][int(s[-1])] += 1
ans = 0
for i in range(10):
    for j in range(10):
        ans += d[i][j] * d[j][i]
print(ans)

-mem = [[0] * 10 for i in range(10)]
-for i in range(1, N + 1):
-    a = int(str(i)[0])
-    b = int(str(i)[-1])
-    mem[a][b] += 1
+d = [[0] * 10 for i in range(10)]
+for n in range(1, N + 1):
+    s = str(n)
+    d[int(s[0])][int(s[-1])] += 1
-for i in range(1, N + 1):
-    a = int(str(i)[0])
-    b = int(str(i)[-1])
-    ans += mem[b][a]
+for i in range(10):
+    for j in range(10):
+        ans += d[i][j] * d[j][i]


Input:
N = int(input())

p = 1

for i in range(N):

    p *= (i + 1)

    p %= (1e9 + 7)

print((int(p)))

N = int(input())

p = 1

for i in range(N):

    p = p * (i + 1) % (1e9 + 7)

print((int(p)))

N = int(input())
p = 1
for i in range(N):
    p *= i + 1
    p %= 1e9 + 7
print((int(p)))


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

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

diff = []

for i in range(n - 1):

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

diff.append(k - a[-1] + a[0])

print((k - max(diff)))

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

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

a.append(k + a[0])

diff = [a1 - a0 for a0, a1 in zip(a, a[1:])]

print((k - max(diff)))

+diff = [a1 - a0 for a0, a1 in zip(a, a[1:])]


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

val = float('inf')

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

    val = min(val, 2 * c * k + max(x - k, 0) * a + max(y - k, 0) * b)

print(val)

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

ans = 0

ans += min(A + B, 2 * C) * min(X, Y)

if X > Y:

    ans += min(A, 2 * C) * (X - Y)

elif X < Y:

    ans += min(B, 2 * C) * (Y - X)

print(ans)

a, b, c, x, y = list(map(int, input().split()))
val = float("inf")
for k in range(10**5 + 1):
    val = min(val, 2 * c * k + max(x - k, 0) * a + max(y - k, 0) * b)
print(val)

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

-    val = min(val, 2 * c * k + max(x - k, 0) * a + max(y - k, 0) * b)

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


Input:
from math import factorial as f

n = int(input())

print((f(n) % (10 ** 9 + 7)))

n = int(input())

p = 1

for i in range(n):

    p = (p * (i+1)) % (10 ** 9 + 7)

print(p)

from math import factorial as f

n = int(input())
print((f(n) % (10**9 + 7)))

n = int(input())
p = 1
for i in range(n):
    p = (p * (i + 1)) % (10**9 + 7)
print(p)

-from math import factorial as f
-print((f(n) % (10**9 + 7)))
+p = 1
+for i in range(n):
+    p = (p * (i + 1)) % (10**9 + 7)
+print(p)


Input:
import math

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

ans=1

n=int(input())

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

  ans*=i

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

print(ans)

import math

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

ans = 1
n = int(input())
for i in range(1, n + 1):
    ans *= i
    ans = ans % (10**9 + 7)
print(ans)

-print((math.factorial(int(input())) % (10**9 + 7)))


Input:
A, B, C, X, Y = [int(_) for _ in input().split()]

ans = A * X + B * Y

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

    cost = 2 * C * i

    if X > i:

        cost += A * (X - i)

    if Y > i:

        cost += B * (Y - i)

    ans = min(ans, cost)

print(ans)

A, B, C, X, Y = [int(_) for _ in input().split()]

M = max(X, Y)

print((min(2 * C * M, 2 * C * (X + Y - M) + A *

          (M - Y) + B * (M - X), A * X + B * Y)))


Input:
s = input()

len_s = len(s)

cnt = [0, 0]

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

    memo_0 = s[i - 1]

    memo_1 = s[len_s - i]

    if s[i] != memo_0:

        cnt[0] += 1

    if s[len_s - i - 1] != memo_1:

        cnt[1] += 1

print((min(cnt)))

s = input()

len_s = len(s)

cnt = 0

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

    memo_0 = s[i - 1]

    if s[i] != memo_0:

        cnt += 1

print(cnt)


Input:
import heapq

K=int(input())

dist=[10**15 for i in range(K)]

dist[1]=1

q=[]

heapq.heappush(q,(0,1))

while (len(q)!=0):

    prc,src=heapq.heappop(q)

    if dist[src]<prc:

        continue

    if dist[(src+1)%K]>dist[src]+1:

        dist[(src+1)%K]=dist[src]+1

        heapq.heappush(q,(dist[(src+1)%K],(src+1)%K))

    if dist[(10*src)%K]>dist[src]:

        dist[(10*src)%K]=dist[src]

        heapq.heappush(q,(dist[(src*10)%K],(src*10)%K))

print((dist[0]))

from collections import deque

K=int(input())

dist=[10**15 for i in range(K)]

dist[1]=1

q=deque([1])

while(len(q)>0):

    r=q.popleft()

    s=(r+1)%K

    if dist[r]+1<dist[s]:

        dist[s]=dist[r]+1

        q.append(s)

    t=(r*10)%K

    if dist[r]<dist[t]:

        dist[t]=dist[r]

        q.appendleft(t)

print((dist[0]))

import heapq

K = int(input())
dist = [10**15 for i in range(K)]
dist[1] = 1
q = []
heapq.heappush(q, (0, 1))
while len(q) != 0:
    prc, src = heapq.heappop(q)
    if dist[src] < prc:
        continue
    if dist[(src + 1) % K] > dist[src] + 1:
        dist[(src + 1) % K] = dist[src] + 1
        heapq.heappush(q, (dist[(src + 1) % K], (src + 1) % K))
    if dist[(10 * src) % K] > dist[src]:
        dist[(10 * src) % K] = dist[src]
        heapq.heappush(q, (dist[(src * 10) % K], (src * 10) % K))
print((dist[0]))

from collections import deque

K = int(input())
dist = [10**15 for i in range(K)]
dist[1] = 1
q = deque([1])
while len(q) > 0:
    r = q.popleft()
    s = (r + 1) % K
    if dist[r] + 1 < dist[s]:
        dist[s] = dist[r] + 1
        q.append(s)
    t = (r * 10) % K
    if dist[r] < dist[t]:
        dist[t] = dist[r]
        q.appendleft(t)
print((dist[0]))

-    if dist[(src + 1) % K] > dist[src] + 1:

-        dist[(src + 1) % K] = dist[src] + 1

-        heapq.heappush(q, (dist[(src + 1) % K], (src + 1) % K))

-    if dist[(10 * src) % K] > dist[src]:

-        dist[(10 * src) % K] = dist[src]

-        heapq.heappush(q, (dist[(src * 10) % K], (src * 10) % K))


Input:
n = int(input())

cnt = 0

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

    i_str = str(i)

    if len(i_str) == 1:

        cnt += 1

        continue

    if len(i_str) == 2:

        if int(i_str[0]) == int(i_str[1]):

            cnt += 3

        elif int(i_str[1]) != 0 and int(i_str[0]) > int(i_str[1]):

            cnt += 2

        continue

    if len(i_str) == 3:

        if int(i_str[0]) == int(i_str[2]):

            cnt += 4 + (2 * (int(i_str[1])+1) - 1)

        elif int(i_str[2]) != 0 and int(i_str[0]) > int(i_str[2]):

            cnt += 22

        elif int(i_str[0]) < int(i_str[2]):

            cnt += 2

        continue

    if len(i_str) == 4:

        if int(i_str[0]) == int(i_str[3]):

            cnt += 24 + 20 * int(i_str[1]) + 2 * (int(i_str[2])+1) - 1

        elif int(i_str[3]) != 0 and int(i_str[0]) > int(i_str[3]):

            cnt += 222

        elif int(i_str[0]) < int(i_str[3]):

            cnt += 22

        continue

    if len(i_str) == 5:

        if int(i_str[0]) == int(i_str[4]):

            cnt += 224 + 200 * int(i_str[1]) + 20 * int(i_str[2]) + 2 * (int(i_str[3])+1) - 1

        elif int(i_str[4]) != 0 and int(i_str[0]) > int(i_str[4]):

            cnt += 2222

        elif int(i_str[0]) < int(i_str[4]):

            cnt += 222

        continue

    if len(i_str) == 6:

        if int(i_str[0]) == int(i_str[5]):

            cnt += 2224 + 2000 * int(i_str[1]) + 200 * int(i_str[2]) + 20 * int(i_str[3]) + 2 * (int(i_str[4])+1) - 1

        elif int(i_str[5]) != 0 and int(i_str[0]) > int(i_str[5]):

            cnt += 22222

        elif int(i_str[0]) < int(i_str[5]):

            cnt += 2222

        continue

print(cnt)

n = int(input())

c = [[0 for _ in range(10)] for __ in range(10)]

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

    str_i = str(i)

    head = int(str_i[0])

    tail = int(str_i[-1])

    c[head][tail] += 1

cnt = 0

for i in range(10):

    for j in range(10):

        cnt += c[i][j] * c[j][i]

print(cnt)

n = int(input())
cnt = 0
for i in range(1, n + 1):
    i_str = str(i)
    if len(i_str) == 1:
        cnt += 1
        continue
    if len(i_str) == 2:
        if int(i_str[0]) == int(i_str[1]):
            cnt += 3
        elif int(i_str[1]) != 0 and int(i_str[0]) > int(i_str[1]):
            cnt += 2
        continue
    if len(i_str) == 3:
        if int(i_str[0]) == int(i_str[2]):
            cnt += 4 + (2 * (int(i_str[1]) + 1) - 1)
        elif int(i_str[2]) != 0 and int(i_str[0]) > int(i_str[2]):
            cnt += 22
        elif int(i_str[0]) < int(i_str[2]):
            cnt += 2
        continue
    if len(i_str) == 4:
        if int(i_str[0]) == int(i_str[3]):
            cnt += 24 + 20 * int(i_str[1]) + 2 * (int(i_str[2]) + 1) - 1
        elif int(i_str[3]) != 0 and int(i_str[0]) > int(i_str[3]):
            cnt += 222
        elif int(i_str[0]) < int(i_str[3]):
            cnt += 22
        continue
    if len(i_str) == 5:
        if int(i_str[0]) == int(i_str[4]):
            cnt += (
                224
                + 200 * int(i_str[1])
                + 20 * int(i_str[2])
                + 2 * (int(i_str[3]) + 1)
                - 1
            )
        elif int(i_str[4]) != 0 and int(i_str[0]) > int(i_str[4]):
            cnt += 2222
        elif int(i_str[0]) < int(i_str[4]):
            cnt += 222
        continue
    if len(i_str) == 6:
        if int(i_str[0]) == int(i_str[5]):
            cnt += (
                2224
                + 2000 * int(i_str[1])
                + 200 * int(i_str[2])
                + 20 * int(i_str[3])
                + 2 * (int(i_str[4]) + 1)
                - 1
            )
        elif int(i_str[5]) != 0 and int(i_str[0]) > int(i_str[5]):
            cnt += 22222
        elif int(i_str[0]) < int(i_str[5]):
            cnt += 2222
        continue
print(cnt)

n = int(input())
c = [[0 for _ in range(10)] for __ in range(10)]
for i in range(1, n + 1):
    str_i = str(i)
    head = int(str_i[0])
    tail = int(str_i[-1])
    c[head][tail] += 1
cnt = 0
for i in range(10):
    for j in range(10):
        cnt += c[i][j] * c[j][i]
print(cnt)

+c = [[0 for _ in range(10)] for __ in range(10)]

-        if int(i_str[0]) == int(i_str[1]):

-        elif int(i_str[1]) != 0 and int(i_str[0]) > int(i_str[1]):

-        if int(i_str[0]) == int(i_str[2]):

-        elif int(i_str[2]) != 0 and int(i_str[0]) > int(i_str[2]):

-        elif int(i_str[0]) < int(i_str[2]):

-        if int(i_str[0]) == int(i_str[3]):

-        elif int(i_str[3]) != 0 and int(i_str[0]) > int(i_str[3]):

-        elif int(i_str[0]) < int(i_str[3]):

-        if int(i_str[0]) == int(i_str[4]):

-        elif int(i_str[4]) != 0 and int(i_str[0]) > int(i_str[4]):

-        elif int(i_str[0]) < int(i_str[4]):

-        if int(i_str[0]) == int(i_str[5]):

-        elif int(i_str[5]) != 0 and int(i_str[0]) > int(i_str[5]):

-        elif int(i_str[0]) < int(i_str[5]):


Input:
import math

N = int(input())

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

print(ans)

N = int(input())

mod = 10**9 + 7

ans = 1

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

    ans *= i

    ans %= mod

print(ans)

-import math
-ans = math.factorial(N) % (10**9 + 7)
+mod = 10**9 + 7
+ans = 1
+for i in range(1, N + 1):
+    ans *= i
+    ans %= mod


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

l = []

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

    T = i * C * 2

    if i < X:

        T += (X-i)*A

    if i < Y:

        T += (Y-i)*B

    l.append(T)

l.sort()

print((l[0]))

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

ans = 0

if 2*C <= A+B:

    Z = min(X,Y)

    ans = 2*C*Z

    X -= Z

    Y -= Z

ans += min(2*C*X,A*X)

ans += min(2*C*Y,B*Y)

print(ans)

A, B, C, X, Y = list(map(int, input().split()))
l = []
for i in range(max(X, Y) + 1):
    T = i * C * 2
    if i < X:
        T += (X - i) * A
    if i < Y:
        T += (Y - i) * B
    l.append(T)
l.sort()
print((l[0]))

A, B, C, X, Y = list(map(int, input().split()))
ans = 0
if 2 * C <= A + B:
    Z = min(X, Y)
    ans = 2 * C * Z
    X -= Z
    Y -= Z
ans += min(2 * C * X, A * X)
ans += min(2 * C * Y, B * Y)
print(ans)

-l = []
-for i in range(max(X, Y) + 1):
-    T = i * C * 2
-    if i < X:
-        T += (X - i) * A
-    if i < Y:
-        T += (Y - i) * B
-    l.append(T)
-l.sort()
-print((l[0]))
+ans = 0
+if 2 * C <= A + B:
+    Z = min(X, Y)
+    ans = 2 * C * Z
+    X -= Z
+    Y -= Z
+ans += min(2 * C * X, A * X)
+ans += min(2 * C * Y, B * Y)
+print(ans)


Input:
import collections

import sys

a=input()

b=[]

for i in a:

  b.append(i)

b.sort()

c=collections.Counter(b)

c=list(c.values())

for i in range(len(c)):

  if int(c[i])%2==0:

    q=0

  else:

    q=1

    print('No')

    sys.exit()

print('Yes')

w = list(input())

s = set(w)

for i in s:

  if w.count(i)%2 != 0:

    print("No")

    break

else:

  print("Yes")

import collections
import sys

a = input()
b = []
for i in a:
    b.append(i)
b.sort()
c = collections.Counter(b)
c = list(c.values())
for i in range(len(c)):
    if int(c[i]) % 2 == 0:
        q = 0
    else:
        q = 1
        print("No")
        sys.exit()
print("Yes")

w = list(input())
s = set(w)
for i in s:
    if w.count(i) % 2 != 0:
        print("No")
        break
else:
    print("Yes")

-import collections
-import sys
-a = input()
-b = []
-for i in a:
-    b.append(i)
-b.sort()
-c = collections.Counter(b)
-c = list(c.values())
-for i in range(len(c)):
-    if int(c[i]) % 2 == 0:
-        q = 0
-    else:
-        q = 1
+w = list(input())
+s = set(w)
+for i in s:
+    if w.count(i) % 2 != 0:
-        sys.exit()
-print("Yes")
+        break
+else:
+    print("Yes")


Input:
#ABC951B/Sum_of_Three_Integers

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

ans = 0

for x in range(0, K+1):

    for y in range(0, K+1):

        z = S - x - y

        if 0 <= z and z <= K:

            ans += 1

print(ans)

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

ans = 0

for X in range(K+1):

    for Y in range(K+1):

        Z = S - X - Y

        if Z >= 0 and Z <= K:

            ans += 1

print(ans)

# ABC951B/Sum_of_Three_Integers
K, S = list(map(int, input().split()))
ans = 0
for x in range(0, K + 1):
    for y in range(0, K + 1):
        z = S - x - y
        if 0 <= z and z <= K:
            ans += 1
print(ans)

K, S = list(map(int, input().split()))
ans = 0
for X in range(K + 1):
    for Y in range(K + 1):
        Z = S - X - Y
        if Z >= 0 and Z <= K:
            ans += 1
print(ans)

-# ABC951B/Sum_of_Three_Integers
-for x in range(0, K + 1):
-    for y in range(0, K + 1):
-        z = S - x - y
-        if 0 <= z and z <= K:
+for X in range(K + 1):
+    for Y in range(K + 1):
+        Z = S - X - Y
+        if Z >= 0 and Z <= K:


Input:
n=int(input())-1

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

i=0

for j in range(n):

 if A[j]<=A[-1]:A[i],A[j]=A[j],A[i];i+=1

A[i],A[n]=A[n],A[i]

print((" ".join(map(str,A[:i]))+" ["+str(A[i])+"] "+" ".join(map(str,A[i+1:]))))

n = int(input()) - 1
A = list(map(int, input().split()))
i = 0
for j in range(n):
    if A[j] <= A[-1]:
        A[i], A[j] = A[j], A[i]
        i += 1
A[i], A[n] = A[n], A[i]
print(
    (
        " ".join(map(str, A[:i]))
        + " ["
        + str(A[i])
        + "] "
        + " ".join(map(str, A[i + 1 :]))
    )
)

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


Input:
N = int(input())

mem = [[0]*10 for _ in range(10)]

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

    a = int(str(i)[0])

    b = int(str(i)[-1])

    mem[a][b] += 1

ans = 0

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

    a = int(str(i)[0])

    b = int(str(i)[-1])

    ans += mem[b][a]

print(ans)

N = int(input())

mem=[[0] * 10 for _ in range(10)]

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

    s = str(i)

    mem[int(s[0])][int(s[-1])] += 1

ans = 0

for i in range(1, 10):

    for j in range(1, 10):

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

print(ans)

N = int(input())
mem = [[0] * 10 for _ in range(10)]
for i in range(1, N + 1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    mem[a][b] += 1
ans = 0
for i in range(1, N + 1):
    a = int(str(i)[0])
    b = int(str(i)[-1])
    ans += mem[b][a]
print(ans)

N = int(input())
mem = [[0] * 10 for _ in range(10)]
for i in range(1, N + 1):
    s = str(i)
    mem[int(s[0])][int(s[-1])] += 1
ans = 0
for i in range(1, 10):
    for j in range(1, 10):
        ans += mem[i][j] * mem[j][i]
print(ans)


Input:
w = input()

for l in w:

	if w.count(l) % 2 == 1:

		print("No")

		break

else:

	print("Yes")

from collections import Counter

s = input()

c = list(Counter(s).values())

for n in c:

	if n & 1 == 1:

		print("No")

		break

else:

	print("Yes")

-w = input()
-for l in w:
-    if w.count(l) % 2 == 1:
+from collections import Counter
+
+s = input()
+c = list(Counter(s).values())
+for n in c:
+    if n & 1 == 1:


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

tmp=str(a)

c=0

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

    tmp=str(i)

    tmp2=""

    for s in range(len(tmp)):

        tmp2=tmp2+tmp[len(tmp)-1-s]

    if tmp==tmp2:

        c=c+1

print(c)

def main():

    #1. input

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

    ans = 0

    #2. from A to B

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

        #2.1. judge if the number is palindromic

        if (str(i)[0]==str(i)[4]) and (str(i)[1]==str(i)[3]):

            ans = ans+1

    #3. output

    print(ans)

if __name__ == '__main__':

    main()

a, b = list(map(int, input().split()))
tmp = str(a)
c = 0
for i in range(a, b + 1):
    tmp = str(i)
    tmp2 = ""
    for s in range(len(tmp)):
        tmp2 = tmp2 + tmp[len(tmp) - 1 - s]
    if tmp == tmp2:
        c = c + 1
print(c)

def main():
    # 1. input
    a, b = list(map(int, input().split()))
    ans = 0
    # 2. from A to B
    for i in range(a, b + 1):
        # 2.1. judge if the number is palindromic
        if (str(i)[0] == str(i)[4]) and (str(i)[1] == str(i)[3]):
            ans = ans + 1
    # 3. output
    print(ans)

if __name__ == "__main__":
    main()

-        tmp2 = tmp2 + tmp[len(tmp) - 1 - s]

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

+        # 2.1. judge if the number is palindromic


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

ans=0

for i in range(k+1):

    for j in range(k+1):

        z=s-i-j

        if 0<=z<=k:

            ans+=1

print(ans)

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

ans=0

for i in range(k+1):

    for j in range(k+1):

        if 0<=s-i-j<=k:

            ans+=1

print(ans)

k, s = list(map(int, input().split()))
ans = 0
for i in range(k + 1):
    for j in range(k + 1):
        z = s - i - j
        if 0 <= z <= k:
            ans += 1
print(ans)

k, s = list(map(int, input().split()))
ans = 0
for i in range(k + 1):
    for j in range(k + 1):
        if 0 <= s - i - j <= k:
            ans += 1
print(ans)


Input:
from bisect import *

n = int(input())

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

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

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

a = sorted(a)

c = sorted(c)

rec = 0

for i in range(n):

    temp = b[i]

    l = bisect_left(a, temp)

    r = bisect_right(c, temp)

    rec += l * (n - r)

print(rec)

from bisect import *

n = int(input())

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

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

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

a.sort()

c.sort()

res = 0

for i in b:

    temp_a = bisect_left(a, i)

    temp_c = n - bisect_right(c, i)

    res += temp_a*temp_c

print(res)

from bisect import *

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a = sorted(a)
c = sorted(c)
rec = 0
for i in range(n):
    temp = b[i]
    l = bisect_left(a, temp)
    r = bisect_right(c, temp)
    rec += l * (n - r)
print(rec)

from bisect import *

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort()
c.sort()
res = 0
for i in b:
    temp_a = bisect_left(a, i)
    temp_c = n - bisect_right(c, i)
    res += temp_a * temp_c
print(res)

-a = sorted(a)
-c = sorted(c)
-rec = 0
-for i in range(n):
-    temp = b[i]
-    l = bisect_left(a, temp)
-    r = bisect_right(c, temp)
-    rec += l * (n - r)
-print(rec)
+a.sort()
+c.sort()
+res = 0
+for i in b:
+    temp_a = bisect_left(a, i)
+    temp_c = n - bisect_right(c, i)
+    res += temp_a * temp_c
+print(res)


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

for i in range(N+1):

    for j in range(M+1):

        t = i*M + j*N - i*j*2

        if t==K:

            print('Yes')

            exit()

print('No')

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

for i in range(N+1):

    for j in range(M+1):

        if i*(M-j) + j*(N-i) == K:

            print('Yes')

            exit()

print('No')

N, M, K = list(map(int, input().split()))
for i in range(N + 1):
    for j in range(M + 1):
        t = i * M + j * N - i * j * 2
        if t == K:
            print("Yes")
            exit()
print("No")

N, M, K = list(map(int, input().split()))
for i in range(N + 1):
    for j in range(M + 1):
        if i * (M - j) + j * (N - i) == K:
            print("Yes")
            exit()
print("No")


Input:
a,b,c,d,e,f=[int(input()) for _ in range(6)]

print((sum([a,b,c,d,max(e,f)])-min([a,b,c,d])))

print((sum(sorted(int(input()) for _ in range(4))[1:])+max([int(input()) for _ in range(2)])))

a, b, c, d, e, f = [int(input()) for _ in range(6)]
print((sum([a, b, c, d, max(e, f)]) - min([a, b, c, d])))

print(
    (
        sum(sorted(int(input()) for _ in range(4))[1:])
        + max([int(input()) for _ in range(2)])
    )
)

-a, b, c, d, e, f = [int(input()) for _ in range(6)]

-print((sum([a, b, c, d, max(e, f)]) - min([a, b, c, d])))

+        sum(sorted(int(input()) for _ in range(4))[1:])

+        + max([int(input()) for _ in range(2)])


Input:
from collections import defaultdict

N = int(input())

d = defaultdict(int)

for i in range(N):

    S = input()

    d[S] += 1

m = 0

for k in list(d.keys()):

    m = max(d[k],m)

li = []

for k in list(d.keys()):

    if d[k] == m:

        li.append(k)

li.sort()

for l in li:

    print(l)

from collections import defaultdict

N = int(input())

Ss = [input() for i in range(N)]

d = defaultdict(int)

for S in Ss:

    d[S] += 1

mx = max([d[k] for k in list(d.keys())])

ans = []

for k in list(d.keys()):

    if d[k] == mx:

       ans.append(k)

for a in sorted(ans):

    print(a)

from collections import defaultdict

N = int(input())
d = defaultdict(int)
for i in range(N):
    S = input()
    d[S] += 1
m = 0
for k in list(d.keys()):
    m = max(d[k], m)
li = []
for k in list(d.keys()):
    if d[k] == m:
        li.append(k)
li.sort()
for l in li:
    print(l)

+mx = max([d[k] for k in list(d.keys())])


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

L,R = [],[]

for i in range(M):

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

    L.append(l)

    R.append(r)

ans = min(R)-max(L)+1

print((ans if ans >= 0 else 0))

# coding: utf-8

import sys

sr = lambda: sys.stdin.readline().rstrip()

ir = lambda: int(sr())

lr = lambda: list(map(int, sr().split()))

N, M = lr()

L = 0; R = 10 ** 6

for _ in range(M):

    l, r = lr()

    if l > L:

        L = l

    if r < R:

        R = r

answer = max(0, R - L + 1)

print(answer)

N, M = list(map(int, input().split()))
L, R = [], []
for i in range(M):
    l, r = list(map(int, input().split()))
    L.append(l)
    R.append(r)
ans = min(R) - max(L) + 1
print((ans if ans >= 0 else 0))

# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
N, M = lr()
L = 0
R = 10**6
for _ in range(M):
    l, r = lr()
    if l > L:
        L = l
    if r < R:
        R = r
answer = max(0, R - L + 1)
print(answer)

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

+sr = lambda: sys.stdin.readline().rstrip()

+lr = lambda: list(map(int, sr().split()))


Input:
n = int(input())

l = []

for i in range(1,1000001):

    a = n / i

    if a.is_integer():

        l.append(i+n/i)

print((int(min(l)-2)))

n = int(input())
l = []
for i in range(1, 1000001):
    a = n / i
    if a.is_integer():
        l.append(i + n / i)
print((int(min(l) - 2)))


Input:
n = int(input())

import math

m = int(math.sqrt(n))+1

anss = []

for x in range(1, m+1):

  y = n//x

  z = n%x

  if z == 0:

    anss.append(abs(x-1)+abs(y-1))

print((min(anss)))

n = int(input())

def make_divisors(n):

    divisors = []

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

        if n % i == 0:

            divisors.append(i)

            if i != n // i:

                divisors.append(n//i)

    #divisors.sort(reverse=True)

    return divisors

l = make_divisors(n)

min_ = float('inf')

for i in range(len(l)):

    min_ = min(min_, l[i]-1+n//l[i]-1)

print(min_)

n = int(input())
import math

m = int(math.sqrt(n)) + 1
anss = []
for x in range(1, m + 1):
    y = n // x
    z = n % x
    if z == 0:
        anss.append(abs(x - 1) + abs(y - 1))
print((min(anss)))

n = int(input())

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5) + 1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n // i)
    # divisors.sort(reverse=True)
    return divisors

l = make_divisors(n)
min_ = float("inf")
for i in range(len(l)):
    min_ = min(min_, l[i] - 1 + n // l[i] - 1)
print(min_)

-        anss.append(abs(x - 1) + abs(y - 1))

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

+                divisors.append(n // i)


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

if a*b%2 == 1:

    print("Odd")

else:

    print("Even")

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

print(('Odd'if a&b&1 else 'Even'))

a, b = list(map(int, input().split()))
if a * b % 2 == 1:
    print("Odd")
else:
    print("Even")

a, b = list(map(int, input().split()))
print(("Odd" if a & b & 1 else "Even"))

+print(("Odd" if a & b & 1 else "Even"))


Input:
import sys

input = sys.stdin.readline

read = sys.stdin.read

n = int(input())

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

L = sorted([(j, i) for i, j in enumerate(A)])

count = 0

for i in range(n):

  if (L[i][1]-i)%2:

    count += 1

ans = (count+1) // 2

print(ans)

import sys

input = sys.stdin.readline

read = sys.stdin.read

n = int(input())

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

L = sorted([(j, i) for i, j in enumerate(A)])

count = 0

for i in range(n):

  count += (L[i][1]-i)%2

ans = (count+1) // 2

print(ans)

import sys

input = sys.stdin.readline
read = sys.stdin.read
n = int(input())
A = list(map(int, read().split()))
L = sorted([(j, i) for i, j in enumerate(A)])
count = 0
for i in range(n):
    if (L[i][1] - i) % 2:
        count += 1
ans = (count + 1) // 2
print(ans)

import sys

input = sys.stdin.readline
read = sys.stdin.read
n = int(input())
A = list(map(int, read().split()))
L = sorted([(j, i) for i, j in enumerate(A)])
count = 0
for i in range(n):
    count += (L[i][1] - i) % 2
ans = (count + 1) // 2
print(ans)


Input:
w = input()

y = list(set(w))

for i in range(len(y)):

    if w.count(y[i]) % 2 != 0:

        print('No')

        exit(0)

print('Yes')

w = list(input())

x = list(set(w))

for i in x:

    y = w.count(i)

    if y % 2 != 0:

        print('No')

        exit(0)

print('Yes')

w = input()
y = list(set(w))
for i in range(len(y)):
    if w.count(y[i]) % 2 != 0:
        print("No")
        exit(0)
print("Yes")

w = list(input())
x = list(set(w))
for i in x:
    y = w.count(i)
    if y % 2 != 0:
        print("No")
        exit(0)
print("Yes")

-w = input()
-y = list(set(w))
-for i in range(len(y)):
-    if w.count(y[i]) % 2 != 0:
+w = list(input())
+x = list(set(w))
+for i in x:
+    y = w.count(i)
+    if y % 2 != 0:


Input:
n=int(input())

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

x.sort()

for p in range(x[0],x[len(x)-1]+1):

    psum=0

    for i in range(len(x)):

        psum=psum+(x[0]-x[i])**2

#    print(psum)

    pmin=psum

    for j in range(x[0]+1,x[len(x)-1]):

        psum=0

        for i in range(len(x)):

            psum=psum+(j-x[i])**2

#        print(psum)

        if pmin>psum:

            pmin=psum

print(pmin)

n=int(input())

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

x.sort()

psum=0

for i in range(len(x)):

    psum=psum+(x[0]-x[i])**2

#    print(psum)

pmin=psum

for j in range(x[0]+1,x[len(x)-1]):

    psum=0

    for i in range(len(x)):

        psum=psum+(j-x[i])**2

#        print(psum)

    if pmin>psum:

        pmin=psum

print(pmin)

n = int(input())
x = list(map(int, input().split()))
x.sort()
for p in range(x[0], x[len(x) - 1] + 1):
    psum = 0
    for i in range(len(x)):
        psum = psum + (x[0] - x[i]) ** 2
    #    print(psum)
    pmin = psum
    for j in range(x[0] + 1, x[len(x) - 1]):
        psum = 0
        for i in range(len(x)):
            psum = psum + (j - x[i]) ** 2
        #        print(psum)
        if pmin > psum:
            pmin = psum
print(pmin)

n = int(input())
x = list(map(int, input().split()))
x.sort()
psum = 0
for i in range(len(x)):
    psum = psum + (x[0] - x[i]) ** 2
#    print(psum)
pmin = psum
for j in range(x[0] + 1, x[len(x) - 1]):
    psum = 0
    for i in range(len(x)):
        psum = psum + (j - x[i]) ** 2
    #        print(psum)
    if pmin > psum:
        pmin = psum
print(pmin)

-for p in range(x[0], x[len(x) - 1] + 1):

+for j in range(x[0] + 1, x[len(x) - 1]):

-    for j in range(x[0] + 1, x[len(x) - 1]):

-            psum = psum + (j - x[i]) ** 2


Input:
import re

S = input()

max = 0

for i in range(len(S)) :

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

        pattern = re.compile(r'[A|T|C|G]{%d}' % int(j - i))

        subStr = S[i:j]

        if pattern.match(subStr) and j - i > max :

            max = j - i

print(max)

S = input()

ans = 0

for i in range(len(S)) :

    for j in range(i, len(S)) :

        if all('ACGT'.count(c) == 1 for c in S[i : j + 1]) :

            ans = max(ans, j - i + 1)

print(ans)

import re

S = input()
max = 0
for i in range(len(S)):
    for j in range(i + 1, len(S) + 1):
        pattern = re.compile(r"[A|T|C|G]{%d}" % int(j - i))
        subStr = S[i:j]
        if pattern.match(subStr) and j - i > max:
            max = j - i
print(max)

S = input()
ans = 0
for i in range(len(S)):
    for j in range(i, len(S)):
        if all("ACGT".count(c) == 1 for c in S[i : j + 1]):
            ans = max(ans, j - i + 1)
print(ans)

-        pattern = re.compile(r"[A|T|C|G]{%d}" % int(j - i))

-        if pattern.match(subStr) and j - i > max:

+        if all("ACGT".count(c) == 1 for c in S[i : j + 1]):


Input:
s=input()

t=input()

ans=0

if s[0]==t[0]:

    ans+=1

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

    ans+=1

if s[2]==t[2]:

    ans+=1

print(ans)

s=input()

t=input()

ans=0

for i in range(0,3):

    if s[i]==t[i]:

        ans+=1

print(ans)

s = input()
t = input()
ans = 0
for i in range(0, 3):
    if s[i] == t[i]:
        ans += 1
print(ans)


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

cnt=0

for i in range(k+1):

    for j in range(k+1):

        if 0 <= s-i-j <= k:

            cnt+=1

print(cnt)

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

a=0

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

    for j in range(0,k+1):

        if 0<=s-i-j<=k:

            a+=1

print(a)

k, s = list(map(int, input().split()))
cnt = 0
for i in range(k + 1):
    for j in range(k + 1):
        if 0 <= s - i - j <= k:
            cnt += 1
print(cnt)

k, s = list(map(int, input().split()))
a = 0
for i in range(0, k + 1):
    for j in range(0, k + 1):
        if 0 <= s - i - j <= k:
            a += 1
print(a)

-cnt = 0
-for i in range(k + 1):
-    for j in range(k + 1):
+a = 0
+for i in range(0, k + 1):
+    for j in range(0, k + 1):
-            cnt += 1
-print(cnt)
+            a += 1
+print(a)


Input:
from heapq import *

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

PAIRS = [[-a, -1] for a in map(int, input().split())]

heapify(PAIRS)

for _ in range(m):

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

    heappush(PAIRS, [-c, -b])

answer = 0

for _ in range(n):

    pair = heappop(PAIRS)

    answer += -pair[0]

    pair[1] += 1

    if pair[1] != 0:

        heappush(PAIRS, pair)

print(answer)

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

PAIRS = [(a, 1) for a in map(int, input().split())]

for _ in range(m):

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

    PAIRS.append((c, b))

PAIRS.sort(reverse = True)

answer, count = 0, 0

for pair in PAIRS:

    answer += pair[0] * pair[1]

    count += pair[1]

    if count > n:

        answer -= pair[0] * (count - n)

        break

print(answer)

n, m = list(map(int, input().split()))
PAIRS = [(a, 1) for a in map(int, input().split())]
for _ in range(m):
    b, c = list(map(int, input().split()))
    PAIRS.append((c, b))
PAIRS.sort(reverse=True)
answer, count = 0, 0
for pair in PAIRS:
    answer += pair[0] * pair[1]
    count += pair[1]
    if count > n:
        answer -= pair[0] * (count - n)
        break
print(answer)

-PAIRS = [[-a, -1] for a in map(int, input().split())]

+PAIRS = [(a, 1) for a in map(int, input().split())]

+        answer -= pair[0] * (count - n)


Input:
import math

n = int(input())

ans = math.factorial(n) % (pow(10, 9) + 7)

print(ans)

n = int(input())

ans = 1

mod = 10 ** 9 + 7

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

    ans = (ans * i % mod) % mod

print(ans)

n = int(input())
ans = 1
mod = 10**9 + 7
for i in range(1, n + 1):
    ans = (ans * i % mod) % mod
print(ans)

-ans = math.factorial(n) % (pow(10, 9) + 7)


Input:
k = int(input())

x = 1

if k%7 != 0:

    mod = 9*k

else:

    mod = 9*k//7

for i in range(10**6):

    x = x*10%mod

    if x == 1:

        print((i + 1))

        exit()

print((-1))

k = int(input())

a = 0

for i in range(k + 10):

    a = (a*10 + 7)%k

    if a == 0:

        print((i + 1))

        exit()

print((-1))

k = int(input())
x = 1
if k % 7 != 0:
    mod = 9 * k
else:
    mod = 9 * k // 7
for i in range(10**6):
    x = x * 10 % mod
    if x == 1:
        print((i + 1))
        exit()
print((-1))

k = int(input())
a = 0
for i in range(k + 10):
    a = (a * 10 + 7) % k
    if a == 0:
        print((i + 1))
        exit()
print((-1))


Input:
s = input()

cnt = 0

ans = 0

for c in s:

    if c == 'B':

        cnt += 1

    elif c == 'W':

        ans += cnt

print(ans)

s = input()

ans = 0

b = 0

for ss in s:

    if ss == 'W':

        ans += b

    else:

        b += 1

print(ans)

s = input()
cnt = 0
ans = 0
for c in s:
    if c == "B":
        cnt += 1
    elif c == "W":
        ans += cnt
print(ans)

s = input()
ans = 0
b = 0
for ss in s:
    if ss == "W":
        ans += b
    else:
        b += 1
print(ans)

-cnt = 0
-for c in s:
-    if c == "B":
-        cnt += 1
-    elif c == "W":
-        ans += cnt
+b = 0
+for ss in s:
+    if ss == "W":
+        ans += b
+    else:
+        b += 1


Input:
import math

def pri(n):

    if n < 2:

        return False

    elif n == 2:

        return True

    elif n % 2 == 0:

        return False

    i = 3

    while i <= math.sqrt(n):

        if n % i == 0:

            return False

        i += 2

    return True

l=[i for i in range(110000)if pri(i) ]

while True:

    n = int(input())

    if n == 0: break

    print((sum(l[:n])))

import math

def pri(n):

    l =[True] * n

    l[0] = l[1] = False

    for i in range(2,n):

        if l[i]:

            for j in range(i*2, n, i):

                l[j] = False

    return l

n=105000

s=pri(n)

l=[i for i in range(n) if s[i]]

while True:

    n = int(input())

    if n == 0: break

    print((sum(l[:n])))

import math

def pri(n):
    if n < 2:
        return False
    elif n == 2:
        return True
    elif n % 2 == 0:
        return False
    i = 3
    while i <= math.sqrt(n):
        if n % i == 0:
            return False
        i += 2
    return True

l = [i for i in range(110000) if pri(i)]
while True:
    n = int(input())
    if n == 0:
        break
    print((sum(l[:n])))

import math

def pri(n):
    l = [True] * n
    l[0] = l[1] = False
    for i in range(2, n):
        if l[i]:
            for j in range(i * 2, n, i):
                l[j] = False
    return l

n = 105000
s = pri(n)
l = [i for i in range(n) if s[i]]
while True:
    n = int(input())
    if n == 0:
        break
    print((sum(l[:n])))

+            for j in range(i * 2, n, i):

-l = [i for i in range(110000) if pri(i)]


Input:
n = int(input())

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

print((sum(l[i]<l[i+1]<l[i+2] or l[i]>l[i+1]>l[i+2] for i in range(n-2))))

n=int(input())

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

a=0

for i in range(n-2):

  p,q,r=l[i:i+3]

  if p<q<r or r<q<p: a+=1

print(a)

n = int(input())
l = list(map(int, input().split()))
print(
    (
        sum(
            l[i] < l[i + 1] < l[i + 2] or l[i] > l[i + 1] > l[i + 2]
            for i in range(n - 2)
        )
    )
)

n = int(input())
l = list(map(int, input().split()))
a = 0
for i in range(n - 2):
    p, q, r = l[i : i + 3]
    if p < q < r or r < q < p:
        a += 1
print(a)


Input:
N = int(input())

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

ans, s = 0, 0

d = {0:1}

for i in A:

    s += i

    ans += d.get(s, 0)

    d[s] = d.get(s, 0) + 1

print(ans)

#!/usr/bin/env python3

from collections import defaultdict

N = int(input())

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

d = defaultdict(int)

d[0] = 1

s, ans = 0, 0

for a in A:

    s += a

    ans += d[s]

    d[s] += 1

print (ans)

N = int(input())
A = list(map(int, input().split()))
ans, s = 0, 0
d = {0: 1}
for i in A:
    s += i
    ans += d.get(s, 0)
    d[s] = d.get(s, 0) + 1
print(ans)

#!/usr/bin/env python3
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
d = defaultdict(int)
d[0] = 1
s, ans = 0, 0
for a in A:
    s += a
    ans += d[s]
    d[s] += 1
print(ans)

-N = int(input())
+#!/usr/bin/env python3
+from collections import defaultdict
+
+N = int(input())
-ans, s = 0, 0
-d = {0: 1}
-for i in A:
-    s += i
-    ans += d.get(s, 0)
-    d[s] = d.get(s, 0) + 1
+d = defaultdict(int)
+d[0] = 1
+s, ans = 0, 0
+for a in A:
+    s += a
+    ans += d[s]
+    d[s] += 1


Input:
N = int(input())

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

dp = [[0 for i in range(3)] for j in range(N+1)]

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

    for j in range(3):

        for k in range(3):

            if j != k:

                dp[i][j] = max(dp[i][j], dp[i-1][k]+l[i-1][k])

print((max(dp[N][0], dp[N][1], dp[N][2])))

N = int(input())

dp = [[0 for i in range(3)] for j in range(N+1)]

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

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

    for j in range(3):

        for k in range(3):

            if j != k:

                dp[i][j] = max(dp[i][j], dp[i-1][k] + hapiness[k])

print((max(dp[N][0], dp[N][1], dp[N][2])))

N = int(input())
l = [list(map(int, input().split())) for _ in range(N)]
dp = [[0 for i in range(3)] for j in range(N + 1)]
for i in range(1, N + 1):
    for j in range(3):
        for k in range(3):
            if j != k:
                dp[i][j] = max(dp[i][j], dp[i - 1][k] + l[i - 1][k])
print((max(dp[N][0], dp[N][1], dp[N][2])))

N = int(input())
dp = [[0 for i in range(3)] for j in range(N + 1)]
for i in range(1, N + 1):
    hapiness = list(map(int, input().split()))
    for j in range(3):
        for k in range(3):
            if j != k:
                dp[i][j] = max(dp[i][j], dp[i - 1][k] + hapiness[k])
print((max(dp[N][0], dp[N][1], dp[N][2])))

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

+    hapiness = list(map(int, input().split()))

+                dp[i][j] = max(dp[i][j], dp[i - 1][k] + hapiness[k])