Input:
import sys

input = sys.stdin.readline

from collections import *

def dfs(x, y):

    if x==0 and y==0:

        t[x][y] = False

        return False

    if x>=1:

        t[x][y] |= not dfs(x-1, y)

    if y>=1:

        t[x][y] |= not dfs(x, y-1)

    if min(x, y)>=1:

        t[x][y] |= not dfs(x-1, y-1)

    return t[x][y]

t = [[False]*7 for _ in range(7)]

for i in range(7):

    for j in range(7):

        dfs(i, j)

"""

for ti in t:

    print(*ti)

"""

N = int(input())

flag = True

for _ in range(N):

    a = int(input())

    if a%2==1:

        flag = False

if flag:

    print('second')

else:

    print('first')

import sys

input = sys.stdin.readline

"""

def dfs(x, y):

    if x==0 and y==0:

        return False

    res = False

    if x>0:

        res |= not dfs(x-1, y)

    if y>0:

        res |= not dfs(x, y-1)

    if min(x, y)>0:

        res |= not dfs(x-1, y-1)

    return res

t = [[False]*5 for _ in range(5)]

for i in range(5):

    for j in range(5):

        t[i][j] = dfs(i, j)

for ti in t:

    print(*ti)

"""

N = int(input())

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

for ai in a:

    if ai%2==1:

        print('first')

        exit()

print('second')

import sys

input = sys.stdin.readline
from collections import *

def dfs(x, y):
    if x == 0 and y == 0:
        t[x][y] = False
        return False
    if x >= 1:
        t[x][y] |= not dfs(x - 1, y)
    if y >= 1:
        t[x][y] |= not dfs(x, y - 1)
    if min(x, y) >= 1:
        t[x][y] |= not dfs(x - 1, y - 1)
    return t[x][y]

t = [[False] * 7 for _ in range(7)]
for i in range(7):
    for j in range(7):
        dfs(i, j)
"""
for ti in t:
    print(*ti)
"""
N = int(input())
flag = True
for _ in range(N):
    a = int(input())
    if a % 2 == 1:
        flag = False
if flag:
    print("second")
else:
    print("first")

import sys

input = sys.stdin.readline
"""
def dfs(x, y):
    if x==0 and y==0:
        return False
    res = False
    if x>0:
        res |= not dfs(x-1, y)
    if y>0:
        res |= not dfs(x, y-1)
    if min(x, y)>0:
        res |= not dfs(x-1, y-1)
    return res
t = [[False]*5 for _ in range(5)]
for i in range(5):
    for j in range(5):
        t[i][j] = dfs(i, j)
for ti in t:
    print(*ti)
"""
N = int(input())
a = [int(input()) for _ in range(N)]
for ai in a:
    if ai % 2 == 1:
        print("first")
        exit()
print("second")


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

cnt=0

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

    n=list(str(i))

    if n==list(reversed(n)):

        cnt+=1

print(cnt)

from sys import stdin

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

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

a,b=nii()

ans=0

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

  s=str(i)

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

    ans+=1

print(ans)

a, b = list(map(int, input().split()))
cnt = 0
for i in range(a, b + 1):
    n = list(str(i))
    if n == list(reversed(n)):
        cnt += 1
print(cnt)

from sys import stdin

nii = lambda: list(map(int, stdin.readline().split()))
lnii = lambda: list(map(int, stdin.readline().split()))
a, b = nii()
ans = 0
for i in range(a, b + 1):
    s = str(i)
    if s == s[::-1]:
        ans += 1
print(ans)

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

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


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

count = 0

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

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

        count += 1

print(count)

def solve(string):

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

    count = 0

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

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

            count += 1

    return str(count)

if __name__ == '__main__':

    print((solve(input())))

def solve(string):
    a, b = list(map(int, string.split()))
    count = 0
    for i in range(a, b + 1):
        if str(i) == str(i)[::-1]:
            count += 1
    return str(count)

if __name__ == "__main__":
    print((solve(input())))

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

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


Input:
import math

N = int(input())

p = math.factorial(N)

print((p % 1000000007))

N = int(input())

p = 1

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

    p *= i

    p %= 1000000007

print(p)

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

-import math
-p = math.factorial(N)
-print((p % 1000000007))
+p = 1
+for i in range(1, N + 1):
+    p *= i
+    p %= 1000000007
+print(p)


Input:
import sys

input = sys.stdin.readline

s = input().rstrip()

k = int(input())

def check_count(s):

    cnt_list = [1]

    cnt = 1

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

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

            cnt_list[-1] += 1

        else:

            cnt_list.append(1)

    return cnt_list

def calc_ans(cnt_list, k):

    _ans = 0

    for c in cnt_list:

        _ans += c//2

    return(_ans * k)

if len(s)==1:

    ans = k//2

else:

    cnt_list = check_count(s)

    if len(cnt_list)==1:

        ans = (len(s)*k)//2

    else:

        ans = calc_ans(cnt_list, k)

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

            if cnt_list[0]%2==1 and cnt_list[-1]%2==1:

                ans += k - 1

print(ans)

import sys

input = sys.stdin.readline

s = input().rstrip()

k = int(input())

def check_count(s):

    cnt_list = []

    cnt = 1

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

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

            cnt += 1

        else:

            cnt_list.append(cnt)

            cnt = 1

    if cnt>1:

        cnt_list.append(cnt)

    return cnt_list

def calc_ans(cnt_list, k):

    _ans = 0

    for c in cnt_list:

        _ans += c//2

    return(_ans * k)

if len(s)==1:

    ans = k//2

else:

    cnt_list = check_count(s)

    if len(cnt_list)==1:

        ans = (len(s)*k)//2

    else:

        ans = calc_ans(cnt_list, k)

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

            if cnt_list[0]%2==1 and cnt_list[-1]%2==1:

                ans += k - 1

print(ans)

import sys

input = sys.stdin.readline
s = input().rstrip()
k = int(input())

def check_count(s):
    cnt_list = [1]
    cnt = 1
    for i in range(1, len(s)):
        if s[i - 1] == s[i]:
            cnt_list[-1] += 1
        else:
            cnt_list.append(1)
    return cnt_list

def calc_ans(cnt_list, k):
    _ans = 0
    for c in cnt_list:
        _ans += c // 2
    return _ans * k

if len(s) == 1:
    ans = k // 2
else:
    cnt_list = check_count(s)
    if len(cnt_list) == 1:
        ans = (len(s) * k) // 2
    else:
        ans = calc_ans(cnt_list, k)
        if s[0] == s[-1]:
            if cnt_list[0] % 2 == 1 and cnt_list[-1] % 2 == 1:
                ans += k - 1
print(ans)

import sys

input = sys.stdin.readline
s = input().rstrip()
k = int(input())

def check_count(s):
    cnt_list = []
    cnt = 1
    for i in range(1, len(s)):
        if s[i - 1] == s[i]:
            cnt += 1
        else:
            cnt_list.append(cnt)
            cnt = 1
    if cnt > 1:
        cnt_list.append(cnt)
    return cnt_list

def calc_ans(cnt_list, k):
    _ans = 0
    for c in cnt_list:
        _ans += c // 2
    return _ans * k

if len(s) == 1:
    ans = k // 2
else:
    cnt_list = check_count(s)
    if len(cnt_list) == 1:
        ans = (len(s) * k) // 2
    else:
        ans = calc_ans(cnt_list, k)
        if s[0] == s[-1]:
            if cnt_list[0] % 2 == 1 and cnt_list[-1] % 2 == 1:
                ans += k - 1
print(ans)

-    cnt_list = [1]
+    cnt_list = []
-            cnt_list[-1] += 1
+            cnt += 1
-            cnt_list.append(1)
+            cnt_list.append(cnt)
+            cnt = 1
+    if cnt > 1:
+        cnt_list.append(cnt)


Input:
N = int(input())

def dist(i, j):

    return i + j - 2

min = 10 ** 24

i = 1

while (i ** 2) <= N:

    if 0 == N % i:

        j = N // i

        d = dist(i, j)

        if min > d:

            min = d

    i += 1

print(d)

import math

n = int(input())

ans = 0

for i in range(math.ceil(math.sqrt(n) + 1))[:0:-1]:

    if 0 == n % i:

        ans = i

        break

ans_partner = n // ans

print((ans + ans_partner - 2))

N = int(input())

def dist(i, j):
    return i + j - 2

min = 10**24
i = 1
while (i**2) <= N:
    if 0 == N % i:
        j = N // i
        d = dist(i, j)
        if min > d:
            min = d
    i += 1
print(d)

+for i in range(math.ceil(math.sqrt(n) + 1))[:0:-1]:


Input:
n = int(input())

print((1 if n == 0 else 0))

n = int(input())

print((0 if n else 1))

-print((1 if n == 0 else 0))
+print((0 if n else 1))


Input:
import sys

readline = sys.stdin.readline

sys.setrecursionlimit(10**8)

mod = 10**9+7

#mod = 998244353

INF = 10**18

eps = 10**-7

m,n,k = list(map(int,readline().split()))

def comb(n, r, mod):

    r = min(r, n-r)

    mol = 1

    deno = 1

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

        mol = mol * (n-r+i) % mod

        deno = deno * i % mod

    ret = mol * pow(deno, mod-2, mod) % mod

    return ret

def f1(x,y,a,b):

    return (a+b-x-y)*(a-x+1)*(b-y+1)//2

def f2(x,a):

    return (a-x)*(a-x+1)//2

ans = 0

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

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

        ans += f1(x,y,n,m)+f1(1,1,x,y)+f1(1,y,x,m)+f1(x,1,n,y)

        ans -= f2(x,n) + f2(1,x) + f2(1,y) + f2(y,m)

        ans %= mod

ans = (ans * comb(m*n-2,k-2,mod))%mod

ans = ans*pow(2,mod-2,mod)%mod

print(ans)

import sys

readline = sys.stdin.readline

sys.setrecursionlimit(10**8)

mod = 10**9+7

#mod = 998244353

INF = 10**18

eps = 10**-7

m,n,k = list(map(int,readline().split()))

def comb(n, r, mod):

    r = min(r, n-r)

    mol = 1

    deno = 1

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

        mol = mol * (n-r+i) % mod

        deno = deno * i % mod

    ret = mol * pow(deno, mod-2, mod) % mod

    return ret

def f1(x,y,a,b):

    return (a+b-x-y)*(a-x+1)*(b-y+1)//2

def f2(x,a):

    return (a-x)*(a-x+1)//2

ans = 0

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

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

        a = n-x+1

        b = m-y+1

        c = m*(a*(a-1)+x*(x-1))

        d = n*(b*(b-1)+y*(y-1))

        ans += (c+d)//2

        ans %= mod

ans = (ans * comb(m*n-2,k-2,mod))%mod

ans = ans*pow(2,mod-2,mod)%mod

print(ans)

import sys

readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9 + 7
# mod = 998244353
INF = 10**18
eps = 10**-7
m, n, k = list(map(int, readline().split()))

def comb(n, r, mod):
    r = min(r, n - r)
    mol = 1
    deno = 1
    for i in range(1, r + 1):
        mol = mol * (n - r + i) % mod
        deno = deno * i % mod
    ret = mol * pow(deno, mod - 2, mod) % mod
    return ret

def f1(x, y, a, b):
    return (a + b - x - y) * (a - x + 1) * (b - y + 1) // 2

def f2(x, a):
    return (a - x) * (a - x + 1) // 2

ans = 0
for x in range(1, n + 1):
    for y in range(1, m + 1):
        ans += f1(x, y, n, m) + f1(1, 1, x, y) + f1(1, y, x, m) + f1(x, 1, n, y)
        ans -= f2(x, n) + f2(1, x) + f2(1, y) + f2(y, m)
        ans %= mod
ans = (ans * comb(m * n - 2, k - 2, mod)) % mod
ans = ans * pow(2, mod - 2, mod) % mod
print(ans)

import sys

readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9 + 7
# mod = 998244353
INF = 10**18
eps = 10**-7
m, n, k = list(map(int, readline().split()))

def comb(n, r, mod):
    r = min(r, n - r)
    mol = 1
    deno = 1
    for i in range(1, r + 1):
        mol = mol * (n - r + i) % mod
        deno = deno * i % mod
    ret = mol * pow(deno, mod - 2, mod) % mod
    return ret

def f1(x, y, a, b):
    return (a + b - x - y) * (a - x + 1) * (b - y + 1) // 2

def f2(x, a):
    return (a - x) * (a - x + 1) // 2

ans = 0
for x in range(1, n + 1):
    for y in range(1, m + 1):
        a = n - x + 1
        b = m - y + 1
        c = m * (a * (a - 1) + x * (x - 1))
        d = n * (b * (b - 1) + y * (y - 1))
        ans += (c + d) // 2
        ans %= mod
ans = (ans * comb(m * n - 2, k - 2, mod)) % mod
ans = ans * pow(2, mod - 2, mod) % mod
print(ans)


Input:
import sys

sys.setrecursionlimit(10**7)

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

n, m = lmi()

l = [0] * (n+1)

l[0] = 1

a = 0

if m != 0:

    a = int(input())

k = 1

for i in range(n+1):

    if i != a:

        if i == 1:

            l[i] = 1

        elif i >= 2:

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

            l[i] %= 1000000007

    else:

        k += 1

        if k <= m:

            a = int(input())

#print(l)

print((l[n] % 1000000007))

import sys

sys.setrecursionlimit(10**7)

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

n, m = lmi()

a = [1] * (n+1)

for i in range(m):

    a[int(input())] = 0

k = [0] * (n+1)

k[0] = 1

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

    if i == 1 and a[1]:

        k[1] = 1

    elif i >= 2 and a[i]:

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

        k[i] %= 10 ** 9 + 7

print((k[n]))

import sys

sys.setrecursionlimit(10**7)

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

n, m = lmi()
l = [0] * (n + 1)
l[0] = 1
a = 0
if m != 0:
    a = int(input())
k = 1
for i in range(n + 1):
    if i != a:
        if i == 1:
            l[i] = 1
        elif i >= 2:
            l[i] = l[i - 1] + l[i - 2]
            l[i] %= 1000000007
    else:
        k += 1
        if k <= m:
            a = int(input())
# print(l)
print((l[n] % 1000000007))

import sys

sys.setrecursionlimit(10**7)

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

n, m = lmi()
a = [1] * (n + 1)
for i in range(m):
    a[int(input())] = 0
k = [0] * (n + 1)
k[0] = 1
for i in range(1, n + 1):
    if i == 1 and a[1]:
        k[1] = 1
    elif i >= 2 and a[i]:
        k[i] = k[i - 1] + k[i - 2]
        k[i] %= 10**9 + 7
print((k[n]))


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

MOD = 10**9+7

dp = [0]*(N+1)

dp[0]=1

A = [int(input()) for i in range(M)]

ban = [1]*(N+1)

for a in A:

    ban[a]=0

for i in range(N):

    if i-1>=0:

        dp[i+1]=dp[i]*ban[i]+dp[i-1]*ban[i-1]

    else:

        dp[i+1]=dp[i]*ban[i]

    dp[i+1] %= MOD

print((dp[N]))

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

A = [int(input()) for i in range(M)]

MOD = 10**9+7

A = set(A)

dp = [0]*(N+1)

dp[0] = 1

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

    if i in A:

        continue

    if i == 1:

        dp[1] = dp[0]

    else:

        dp[i] = (dp[i-1]+dp[i-2])%MOD

print((dp[N]))

N, M = list(map(int, input().split()))
MOD = 10**9 + 7
dp = [0] * (N + 1)
dp[0] = 1
A = [int(input()) for i in range(M)]
ban = [1] * (N + 1)
for a in A:
    ban[a] = 0
for i in range(N):
    if i - 1 >= 0:
        dp[i + 1] = dp[i] * ban[i] + dp[i - 1] * ban[i - 1]
    else:
        dp[i + 1] = dp[i] * ban[i]
    dp[i + 1] %= MOD
print((dp[N]))

N, M = list(map(int, input().split()))
A = [int(input()) for i in range(M)]
MOD = 10**9 + 7
A = set(A)
dp = [0] * (N + 1)
dp[0] = 1
for i in range(1, N + 1):
    if i in A:
        continue
    if i == 1:
        dp[1] = dp[0]
    else:
        dp[i] = (dp[i - 1] + dp[i - 2]) % MOD
print((dp[N]))


Input:
def main():

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

    M = max(X, Y)

    k = 0

    ans = A * X + B * Y

    while True:

        ans = min(

            ans,

            AB * k * 2 + max(0, X - k) * A + max(0, Y - k) * B

        )

        if k >= M: break

        k += 1

    print(ans)

if __name__ == '__main__':

    main()

def main():

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

    ans = 1 << 30

    c = 0  # even

    while True:

        j = c // 2

        t = A * max(0, X - j) + B * max(0, Y - j) + C * c

        if ans <= t: break

        ans = t

        c += 2

    print(ans)

if __name__ == '__main__':

    main()

def main():
    A, B, AB, X, Y = list(map(int, input().split()))
    M = max(X, Y)
    k = 0
    ans = A * X + B * Y
    while True:
        ans = min(ans, AB * k * 2 + max(0, X - k) * A + max(0, Y - k) * B)
        if k >= M:
            break
        k += 1
    print(ans)

if __name__ == "__main__":
    main()

def main():
    A, B, C, X, Y = list(map(int, input().split()))
    ans = 1 << 30
    c = 0  # even
    while True:
        j = c // 2
        t = A * max(0, X - j) + B * max(0, Y - j) + C * c
        if ans <= t:
            break
        ans = t
        c += 2
    print(ans)

if __name__ == "__main__":
    main()

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

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

-        ans = min(ans, AB * k * 2 + max(0, X - k) * A + max(0, Y - k) * B)


Input:
import sys

init = ''.join(sys.stdin.read().split())

# Bidirectional search using breadth-first search

def solve(start, goal):

    if start == goal:

        return 0

    generated_state = {start: 0, goal: 1}   # 0: foreward, 1: backward

    step = {start: 0, goal: 0}

    s = 0

    c_d = [(start, 0), (goal, 1)] # configure and direction

    while True:

        q = iter(c_d)

        c_d = []

        s += 1

        for c, d in q:

            i = c.find('0')

            if 0 <= i <= 5:

                t = str.maketrans('0' + c[i+3], c[i+3] + '0')

                t_c = c.translate(t)

                if t_c in generated_state:

                    if generated_state[t_c] != d:

                        return s + step[t_c]

                else:

                    generated_state[t_c] = d

                    step[t_c] = s

                    c_d.append((t_c, d))

            if i >= 3:

                t = str.maketrans('0' + c[i-3], c[i-3] + '0')

                t_c = c.translate(t)

                if t_c in generated_state:

                    if generated_state[t_c] != d:

                        return s + step[t_c]

                else:

                    generated_state[t_c] = d

                    step[t_c] = s

                    c_d.append((t_c, d))

            if i == 0 or i == 1 or i == 3 or i == 4 or i == 6 or i == 7:

                t = str.maketrans('0' + c[i+1], c[i+1] + '0')

                t_c = c.translate(t)

                if t_c in generated_state:

                    if generated_state[t_c] != d:

                        return s + step[t_c]

                else:

                    generated_state[t_c] = d

                    step[t_c] = s

                    c_d.append((t_c, d))

            if i == 1 or i == 2 or i == 4 or i == 5 or i == 7 or i == 8:

                t = str.maketrans('0' + c[i-1], c[i-1] + '0')

                t_c = c.translate(t)

                if t_c in generated_state:

                    if generated_state[t_c] != d:

                        return s + step[t_c]

                else:

                    generated_state[t_c] = d

                    step[t_c] = s

                    c_d.append((t_c, d))

print((solve(init, '123456780')))

adjacent = (

    (1, 3),       # 0

    (0, 2, 4),    # 1

    (1, 5),       # 2

    (0, 4, 6),    # 3

    (1, 3, 5, 7), # 4

    (2, 4, 8),    # 5

    (3, 7),       # 6

    (4, 6, 8),    # 7

    (5, 7)        # 8

)

import collections

GOAL = [1, 2, 3, 4, 5, 6, 7, 8, 0]

# Bidirectional search using breadth-first search

def solve(start):

    if start == GOAL:

        return 0

    # 0: forward, 1: backward

    generated_state = {tuple(start): 0, tuple(GOAL): 1}

    step = {tuple(start): 0, tuple(GOAL): 0}

    s = 0

    state1 = [(start, 0), (GOAL, 1)]

    while state1:

        state2 = state1[:]

        state1 = []

        s += 1

        for s2, d in state2:

            i = s2.index(0)

            for p in adjacent[i]:

                s1 = s2[:]

                s1[i], s1[p] = s1[p], 0

                key = tuple(s1)

                if key in generated_state:

                    if generated_state[key] != d:

                        return s + step[key]

                    continue

                state1.append((s1, d))

                generated_state[key] = d

                step[key] = s

import sys

start = list(map(int, sys.stdin.read().split()))

print((solve(start)))

import sys

init = "".join(sys.stdin.read().split())
# Bidirectional search using breadth-first search
def solve(start, goal):
    if start == goal:
        return 0
    generated_state = {start: 0, goal: 1}  # 0: foreward, 1: backward
    step = {start: 0, goal: 0}
    s = 0
    c_d = [(start, 0), (goal, 1)]  # configure and direction
    while True:
        q = iter(c_d)
        c_d = []
        s += 1
        for c, d in q:
            i = c.find("0")
            if 0 <= i <= 5:
                t = str.maketrans("0" + c[i + 3], c[i + 3] + "0")
                t_c = c.translate(t)
                if t_c in generated_state:
                    if generated_state[t_c] != d:
                        return s + step[t_c]
                else:
                    generated_state[t_c] = d
                    step[t_c] = s
                    c_d.append((t_c, d))
            if i >= 3:
                t = str.maketrans("0" + c[i - 3], c[i - 3] + "0")
                t_c = c.translate(t)
                if t_c in generated_state:
                    if generated_state[t_c] != d:
                        return s + step[t_c]
                else:
                    generated_state[t_c] = d
                    step[t_c] = s
                    c_d.append((t_c, d))
            if i == 0 or i == 1 or i == 3 or i == 4 or i == 6 or i == 7:
                t = str.maketrans("0" + c[i + 1], c[i + 1] + "0")
                t_c = c.translate(t)
                if t_c in generated_state:
                    if generated_state[t_c] != d:
                        return s + step[t_c]
                else:
                    generated_state[t_c] = d
                    step[t_c] = s
                    c_d.append((t_c, d))
            if i == 1 or i == 2 or i == 4 or i == 5 or i == 7 or i == 8:
                t = str.maketrans("0" + c[i - 1], c[i - 1] + "0")
                t_c = c.translate(t)
                if t_c in generated_state:
                    if generated_state[t_c] != d:
                        return s + step[t_c]
                else:
                    generated_state[t_c] = d
                    step[t_c] = s
                    c_d.append((t_c, d))

print((solve(init, "123456780")))

adjacent = (
    (1, 3),  # 0
    (0, 2, 4),  # 1
    (1, 5),  # 2
    (0, 4, 6),  # 3
    (1, 3, 5, 7),  # 4
    (2, 4, 8),  # 5
    (3, 7),  # 6
    (4, 6, 8),  # 7
    (5, 7),  # 8
)
import collections

GOAL = [1, 2, 3, 4, 5, 6, 7, 8, 0]
# Bidirectional search using breadth-first search
def solve(start):
    if start == GOAL:
        return 0
    # 0: forward, 1: backward
    generated_state = {tuple(start): 0, tuple(GOAL): 1}
    step = {tuple(start): 0, tuple(GOAL): 0}
    s = 0
    state1 = [(start, 0), (GOAL, 1)]
    while state1:
        state2 = state1[:]
        state1 = []
        s += 1
        for s2, d in state2:
            i = s2.index(0)
            for p in adjacent[i]:
                s1 = s2[:]
                s1[i], s1[p] = s1[p], 0
                key = tuple(s1)
                if key in generated_state:
                    if generated_state[key] != d:
                        return s + step[key]
                    continue
                state1.append((s1, d))
                generated_state[key] = d
                step[key] = s

import sys

start = list(map(int, sys.stdin.read().split()))
print((solve(start)))

+# Bidirectional search using breadth-first search

+    generated_state = {tuple(start): 0, tuple(GOAL): 1}

+    step = {tuple(start): 0, tuple(GOAL): 0}

+                if key in generated_state:

+                    if generated_state[key] != d:

+                        return s + step[key]

+                generated_state[key] = d

-# Bidirectional search using breadth-first search

-    generated_state = {start: 0, goal: 1}  # 0: foreward, 1: backward

-    c_d = [(start, 0), (goal, 1)]  # configure and direction

-                if t_c in generated_state:

-                    if generated_state[t_c] != d:

-                        return s + step[t_c]

-                    generated_state[t_c] = d

-                    c_d.append((t_c, d))

-                if t_c in generated_state:

-                    if generated_state[t_c] != d:

-                        return s + step[t_c]

-                    generated_state[t_c] = d

-                    c_d.append((t_c, d))

-                if t_c in generated_state:

-                    if generated_state[t_c] != d:

-                        return s + step[t_c]

-                    generated_state[t_c] = d

-                    c_d.append((t_c, d))

-                if t_c in generated_state:

-                    if generated_state[t_c] != d:

-                        return s + step[t_c]

-                    generated_state[t_c] = d

-                    c_d.append((t_c, d))

+start = list(map(int, sys.stdin.read().split()))


Input:
def popcount(x):

  r, q = 0, x

  while x:

    r += 1

    x &= x - 1

  return r, q

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)

l = (1 << n) - 1

b = sorted(map(popcount, list(range(1, l + 1))))

ans = next((i for i, x in reversed(b)

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

print(ans)

from itertools import combinations, count

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)

r = next(i for i in count(n, - 1) for x in map(set, combinations(r, i))

         if all(a[j][0].isdisjoint(x) and a[j][1] < x for j in x))

print(r)

def popcount(x):
    r, q = 0, x
    while x:
        r += 1
        x &= x - 1
    return r, q

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)
l = (1 << n) - 1
b = sorted(map(popcount, list(range(1, l + 1))))
ans = next(
    (
        i
        for i, x in reversed(b)
        if all(a[j][0] & x == a[j][1] & l - x == 0 for j in r if x >> j & 1)
    ),
    0,
)
print(ans)

from itertools import combinations, count

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)
r = next(
    i
    for i in count(n, -1)
    for x in map(set, combinations(r, i))
    if all(a[j][0].isdisjoint(x) and a[j][1] < x for j in x)
)
print(r)

+from itertools import combinations, count

-b = sorted(map(popcount, list(range(1, l + 1))))

+    for x in map(set, combinations(r, i))

+    if all(a[j][0].isdisjoint(x) and a[j][1] < x for j in x)


Input:
import queue

n = int(input())

abc = [[] for i in range(n+1)]

for i in range(n-1):

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

    abc[a].append([b,c])

    abc[b].append([a,c])

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

xy = [[int(i) for i in input().split()] for j in range(Q)]

q = queue.Queue()

q.put(k)

path = [-1]*(n+1)

path[k] = 0

while not q.empty():

    tmp = q.get()

    for i in range(len(abc[tmp])):

        #print(abc[tmp])

        #exit()

        num = abc[tmp][i][0]

        if path[num] != -1:

            continue

        else:

            path[num] = path[tmp] + abc[tmp][i][1]

            q.put(num)

for i in range(Q):

    print((path[xy[i][0]]+path[xy[i][1]]))

import queue

import sys

sys.setrecursionlimit(10**7)

n = int(input())

abc = [[] for i in range(n+1)]

for i in range(n-1):

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

    abc[a].append([b,c])

    abc[b].append([a,c])

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

xy = [[int(i) for i in input().split()] for j in range(Q)]

q = queue.Queue()

q.put(k)

path = [-1]*(n+1)

path[k] = 0

def dfs(p):

    for i in range(len(abc[p])):

        num = abc[p][i][0]

        if path[num] != -1:

            continue

        else:

            path[num] = path[p] + abc[p][i][1]

            dfs(num)

dfs(k)

for i in range(Q):

    print((path[xy[i][0]]+path[xy[i][1]]))

import queue

n = int(input())
abc = [[] for i in range(n + 1)]
for i in range(n - 1):
    a, b, c = list(map(int, input().split()))
    abc[a].append([b, c])
    abc[b].append([a, c])
Q, k = list(map(int, input().split()))
xy = [[int(i) for i in input().split()] for j in range(Q)]
q = queue.Queue()
q.put(k)
path = [-1] * (n + 1)
path[k] = 0
while not q.empty():
    tmp = q.get()
    for i in range(len(abc[tmp])):
        # print(abc[tmp])
        # exit()
        num = abc[tmp][i][0]
        if path[num] != -1:
            continue
        else:
            path[num] = path[tmp] + abc[tmp][i][1]
            q.put(num)
for i in range(Q):
    print((path[xy[i][0]] + path[xy[i][1]]))

import queue
import sys

sys.setrecursionlimit(10**7)
n = int(input())
abc = [[] for i in range(n + 1)]
for i in range(n - 1):
    a, b, c = list(map(int, input().split()))
    abc[a].append([b, c])
    abc[b].append([a, c])
Q, k = list(map(int, input().split()))
xy = [[int(i) for i in input().split()] for j in range(Q)]
q = queue.Queue()
q.put(k)
path = [-1] * (n + 1)
path[k] = 0

def dfs(p):
    for i in range(len(abc[p])):
        num = abc[p][i][0]
        if path[num] != -1:
            continue
        else:
            path[num] = path[p] + abc[p][i][1]
            dfs(num)

dfs(k)
for i in range(Q):
    print((path[xy[i][0]] + path[xy[i][1]]))

-            path[num] = path[tmp] + abc[tmp][i][1]

+            path[num] = path[p] + abc[p][i][1]


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

if x - r >= 0 and x + r <= W and y - r >= 0 and y + r <= H:

    print("Yes")

else:

    print("No")

W, H, x, y, r = list(map(int, 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()]
if x - r >= 0 and x + r <= W and y - r >= 0 and y + r <= H:
    print("Yes")
else:
    print("No")

W, H, x, y, r = list(map(int, 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()]

-if x - r >= 0 and x + r <= W and y - r >= 0 and y + r <= H:

+W, H, x, y, r = list(map(int, input().split()))

+if r <= x <= W - r and r <= y <= H - r:


Input:
from math import log2

n = int(input())

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

ans = 0

for _a in a:

    if _a%2 == 0:

        cnt =0

        while _a%2 == 0:

            _a //= 2

            cnt += 1

        ans += cnt

print((int(ans)))

def f(k):

    cnt = 0

    while k%2 == 0:

        k //= 2

        cnt += 1

        # print(k)

        # print("-----")

    return cnt

n = int(input())

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

ans = 0

for a in A:

    if a%2 == 0:

        ans += f(a)

print(ans)

from math import log2

n = int(input())
a = list(map(int, input().split()))
ans = 0
for _a in a:
    if _a % 2 == 0:
        cnt = 0
        while _a % 2 == 0:
            _a //= 2
            cnt += 1
        ans += cnt
print((int(ans)))

def f(k):
    cnt = 0
    while k % 2 == 0:
        k //= 2
        cnt += 1
        # print(k)
        # print("-----")
    return cnt

n = int(input())
A = list(map(int, input().split()))
ans = 0
for a in A:
    if a % 2 == 0:
        ans += f(a)
print(ans)

-from math import log2
+def f(k):
+    cnt = 0
+    while k % 2 == 0:
+        k //= 2
+        cnt += 1
+        # print(k)
+    return cnt
+
-a = list(map(int, input().split()))
+A = list(map(int, input().split()))
-for _a in a:
-    if _a % 2 == 0:
-        cnt = 0
-        while _a % 2 == 0:
-            _a //= 2
-            cnt += 1
-        ans += cnt
-print((int(ans)))
+for a in A:
+    if a % 2 == 0:
+        ans += f(a)
+print(ans)


Input:
# coding: utf-8

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

    n, m, k = list(map(int, args[0].split()))

    mod = 998244353

    if m == 1 and n-1 == k:

        return str(1)

    ncr = 1

    p = m*pow(m-1, n-1, mod) % mod

    ret = p

    inv = pow(m-1, mod-2, mod)

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

        ncr = (ncr * (n-i)*pow(i, mod-2, mod)) % mod

        p = (p*inv) % mod

        ret += p*ncr % mod

    return str(ret % mod)

if __name__ == "__main__":

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

# coding: utf-8

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

    n, m, k = list(map(int, args[0].split()))

    mod = 998244353

    if m == 1 and n-1 == k:

        return str(1)

    ncr = 1

    p = m*pow(m-1, n-1, mod) % mod

    ret = p

    iR = [1]*(k+1)

    im = pow(m-1, mod-2, mod)

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

        iR[i] = max(1, (-(mod//i) * iR[mod % i]) % mod)

        ncr = (ncr * (n-i)*iR[i]) % mod

        p = (p*im) % mod

        ret += p*ncr % mod

    return str(ret % mod)

if __name__ == "__main__":

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

# coding: utf-8
def solve(*args: str) -> str:
    n, m, k = list(map(int, args[0].split()))
    mod = 998244353
    if m == 1 and n - 1 == k:
        return str(1)
    ncr = 1
    p = m * pow(m - 1, n - 1, mod) % mod
    ret = p
    inv = pow(m - 1, mod - 2, mod)
    for i in range(1, k + 1):
        ncr = (ncr * (n - i) * pow(i, mod - 2, mod)) % mod
        p = (p * inv) % mod
        ret += p * ncr % mod
    return str(ret % mod)

if __name__ == "__main__":
    print((solve(*(open(0).read().splitlines()))))

# coding: utf-8
def solve(*args: str) -> str:
    n, m, k = list(map(int, args[0].split()))
    mod = 998244353
    if m == 1 and n - 1 == k:
        return str(1)
    ncr = 1
    p = m * pow(m - 1, n - 1, mod) % mod
    ret = p
    iR = [1] * (k + 1)
    im = pow(m - 1, mod - 2, mod)
    for i in range(1, k + 1):
        iR[i] = max(1, (-(mod // i) * iR[mod % i]) % mod)
        ncr = (ncr * (n - i) * iR[i]) % mod
        p = (p * im) % mod
        ret += p * ncr % mod
    return str(ret % mod)

if __name__ == "__main__":
    print((solve(*(open(0).read().splitlines()))))

-        ncr = (ncr * (n - i) * pow(i, mod - 2, mod)) % mod

+        ncr = (ncr * (n - i) * iR[i]) % mod


Input:
import collections as col

def prime(n):

    ans = []

    num = n

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

        if i%2==0 and i!=2: continue

        while num%i == 0: num //= i ; ans.append(i)

    return ans

n = int(input())

mod = 10**9 + 7

primes = []

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

cnt = col.Counter(primes)

ans = 1

for key,val in list(cnt.items()): ans *= (val + 1) ; ans %= mod

print(ans)

import collections as col

def prime(n):

    ans = []

    num = n

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

        if i%2==0 and i!=2: continue

        while num%i == 0: num //= i ; ans.append(i)

    if num != 1: ans.append(num)

    return ans

n = int(input())

mod = 10**9 + 7

primes = []

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

cnt = col.Counter(primes)

ans = 1

for key,val in list(cnt.items()): ans *= (val + 1) ; ans %= mod

print(ans)

import collections as col

def prime(n):
    ans = []
    num = n
    for i in range(2, n + 1):
        if i % 2 == 0 and i != 2:
            continue
        while num % i == 0:
            num //= i
            ans.append(i)
    return ans

n = int(input())
mod = 10**9 + 7
primes = []
for i in range(2, n + 1):
    primes += prime(i)
cnt = col.Counter(primes)
ans = 1
for key, val in list(cnt.items()):
    ans *= val + 1
    ans %= mod
print(ans)

import collections as col

def prime(n):
    ans = []
    num = n
    for i in range(2, int(n**0.5) + 1):
        if i % 2 == 0 and i != 2:
            continue
        while num % i == 0:
            num //= i
            ans.append(i)
    if num != 1:
        ans.append(num)
    return ans

n = int(input())
mod = 10**9 + 7
primes = []
for i in range(2, n + 1):
    primes += prime(i)
cnt = col.Counter(primes)
ans = 1
for key, val in list(cnt.items()):
    ans *= val + 1
    ans %= mod
print(ans)

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


Input:
import sys

def main():

    input = sys.stdin.readline

    N = int(input())

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

    for i in range(N-1):

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

        a, b = a-1, b-1

        E[a].append((b,i))

        E[b].append((a,i))

    def dfs(s, g):

        par = [(-1,-1)] * N

        par[s] = (s,-1)

        stack = [s]

        while stack:

            v = stack.pop()

            for to, i in E[v]:

                if par[to][0] >= 0: continue

                par[to] = (v, i)

                if to == g: break

                stack.append(to)

        r = set()

        v = g

        while v != s:

            v, i = par[v]

            r.add(i)

        return r

    M = int(input())

    path = [None] * M

    for i in range(M):

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

        u, v = u-1, v-1

        path[i] = dfs(u, v)

    def calc(s): return 1<<(N-1-len(s))

    i_ans = 0

    for p in range(1, 1<<M):

        is_odd = bin(p).count('1') & 1

        s = set()

        for i in range(M):

            if p&1: s |= path[i]

            p >>= 1

        if is_odd: i_ans += calc(s)

        else: i_ans -= calc(s)

    print(((1<<(N-1)) - i_ans))

if __name__ == '__main__':

    main()

import sys

def main():

    input = sys.stdin.readline

    N = int(input())

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

    for i in range(N-1):

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

        a, b = a-1, b-1

        E[a].append((b,i))

        E[b].append((a,i))

    def dfs(s, g):

        par = [(-1,-1)] * N

        par[s] = (s,-1)

        stack = [s]

        while stack:

            v = stack.pop()

            for to, i in E[v]:

                if par[to][0] >= 0: continue

                par[to] = (v, i)

                if to == g: break

                stack.append(to)

        r = set()

        v = g

        while v != s:

            v, i = par[v]

            r.add(i)

        return r

    M = int(input())

    path = [None] * M

    for i in range(M):

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

        u, v = u-1, v-1

        path[i] = dfs(u, v)

    def calc(s): return 1<<(N-1-len(s))

    i_ans = 0

    for p in range(1, 1<<M):

        is_odd = 0

        s = set()

        for i in range(M):

            if p&1:

                s |= path[i]

                is_odd ^= 1

            p >>= 1

        if is_odd: i_ans += calc(s)

        else: i_ans -= calc(s)

    print(((1<<(N-1)) - i_ans))

if __name__ == '__main__':

    main()

import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    E = [[] for _ in range(N)]
    for i in range(N - 1):
        a, b = list(map(int, input().split()))
        a, b = a - 1, b - 1
        E[a].append((b, i))
        E[b].append((a, i))

    def dfs(s, g):
        par = [(-1, -1)] * N
        par[s] = (s, -1)
        stack = [s]
        while stack:
            v = stack.pop()
            for to, i in E[v]:
                if par[to][0] >= 0:
                    continue
                par[to] = (v, i)
                if to == g:
                    break
                stack.append(to)
        r = set()
        v = g
        while v != s:
            v, i = par[v]
            r.add(i)
        return r

    M = int(input())
    path = [None] * M
    for i in range(M):
        u, v = list(map(int, input().split()))
        u, v = u - 1, v - 1
        path[i] = dfs(u, v)

    def calc(s):
        return 1 << (N - 1 - len(s))

    i_ans = 0
    for p in range(1, 1 << M):
        is_odd = bin(p).count("1") & 1
        s = set()
        for i in range(M):
            if p & 1:
                s |= path[i]
            p >>= 1
        if is_odd:
            i_ans += calc(s)
        else:
            i_ans -= calc(s)
    print(((1 << (N - 1)) - i_ans))

if __name__ == "__main__":
    main()

import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    E = [[] for _ in range(N)]
    for i in range(N - 1):
        a, b = list(map(int, input().split()))
        a, b = a - 1, b - 1
        E[a].append((b, i))
        E[b].append((a, i))

    def dfs(s, g):
        par = [(-1, -1)] * N
        par[s] = (s, -1)
        stack = [s]
        while stack:
            v = stack.pop()
            for to, i in E[v]:
                if par[to][0] >= 0:
                    continue
                par[to] = (v, i)
                if to == g:
                    break
                stack.append(to)
        r = set()
        v = g
        while v != s:
            v, i = par[v]
            r.add(i)
        return r

    M = int(input())
    path = [None] * M
    for i in range(M):
        u, v = list(map(int, input().split()))
        u, v = u - 1, v - 1
        path[i] = dfs(u, v)

    def calc(s):
        return 1 << (N - 1 - len(s))

    i_ans = 0
    for p in range(1, 1 << M):
        is_odd = 0
        s = set()
        for i in range(M):
            if p & 1:
                s |= path[i]
                is_odd ^= 1
            p >>= 1
        if is_odd:
            i_ans += calc(s)
        else:
            i_ans -= calc(s)
    print(((1 << (N - 1)) - i_ans))

if __name__ == "__main__":
    main()

-        is_odd = bin(p).count("1") & 1
+        is_odd = 0
+                is_odd ^= 1


Input:
'''

    Auther: ghoshashis545 Ashis Ghosh

    College: jalpaiguri Govt Enggineering College

'''

from os import path

import sys

from functools import cmp_to_key as ctk

from collections import deque,defaultdict as dd

from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right

from itertools import permutations

from datetime import datetime

from math import ceil,sqrt,log,gcd

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

def si():return input()

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

def li():return list(mi())

abc='abcdefghijklmnopqrstuvwxyz'

mod=1000000007

#mod=998244353

inf = float("inf")

vow=['a','e','i','o','u']

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

def bo(i):

    return ord(i)-ord('a')

file = 0

def solve():

    # for _ in range(ii()):

    n=ii()

    n%=1000

    if n!=0:

        n=1000-n

    print(n)

if __name__ =="__main__":

    if(file):

        if path.exists('input1.txt'):

            sys.stdin=open('input1.txt', 'r')

            sys.stdout=open('output1.txt','w')

        else:

            input=sys.stdin.readline

    solve()

'''

    Auther: ghoshashis545 Ashis Ghosh

    College: jalpaiguri Govt Enggineering College

'''

from os import path

import sys

from heapq import heappush,heappop

from functools import cmp_to_key as ctk

from collections import deque,defaultdict as dd

from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right

from itertools import permutations

from datetime import datetime

from math import ceil,sqrt,log,gcd

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

def si():return input()

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

def li():return list(mi())

abc='abcdefghijklmnopqrstuvwxyz'

mod=1000000007

# mod=998244353

inf = float("inf")

vow=['a','e','i','o','u']

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

def bo(i):

    return ord(i)-ord('a')

file=0

def solve():

    # for _ in range(ii()):

    n=ii()

    n%=1000

    if(n!=0):

        n=1000-n

    print(n)

if __name__ =="__main__":

    if(file):

        if path.exists('input1.txt'):

            sys.stdin=open('input1.txt', 'r')

            sys.stdout=open('output1.txt','w')

        else:

            input=sys.stdin.readline

    solve()

"""
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College
"""
from os import path
import sys
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

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

def si():
    return input()

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

def li():
    return list(mi())

abc = "abcdefghijklmnopqrstuvwxyz"
mod = 1000000007
# mod=998244353
inf = float("inf")
vow = ["a", "e", "i", "o", "u"]
dx, dy = [-1, 1, 0, 0], [0, 0, 1, -1]

def bo(i):
    return ord(i) - ord("a")

file = 0

def solve():
    # for _ in range(ii()):
    n = ii()
    n %= 1000
    if n != 0:
        n = 1000 - n
    print(n)

if __name__ == "__main__":
    if file:
        if path.exists("input1.txt"):
            sys.stdin = open("input1.txt", "r")
            sys.stdout = open("output1.txt", "w")
        else:
            input = sys.stdin.readline
    solve()

"""
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College
"""
from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

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

def si():
    return input()

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

def li():
    return list(mi())

abc = "abcdefghijklmnopqrstuvwxyz"
mod = 1000000007
# mod=998244353
inf = float("inf")
vow = ["a", "e", "i", "o", "u"]
dx, dy = [-1, 1, 0, 0], [0, 0, 1, -1]

def bo(i):
    return ord(i) - ord("a")

file = 0

def solve():
    # for _ in range(ii()):
    n = ii()
    n %= 1000
    if n != 0:
        n = 1000 - n
    print(n)

if __name__ == "__main__":
    if file:
        if path.exists("input1.txt"):
            sys.stdin = open("input1.txt", "r")
            sys.stdout = open("output1.txt", "w")
        else:
            input = sys.stdin.readline
    solve()


Input:
M=998244353

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

a=0

c=1

for i in range(k+1):

  a+=c*m*pow(m-1,n-i-1,M)

  c=c*(n-i-1)*pow(i+1,M-2,M)%M

print((a%M))


Input:
N = int(input())

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

cnt, i = 0, 0

for j in range(10 ** 6):

    cnt += 1

    if a[i] == 2:

        print(cnt)

        #break

        quit()

    else:

        i = a[i] - 1

print((-1))

N = int(input())

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

cnt, i = 0, 0

for j in range(len(a)):

    cnt += 1

    if a[i] == 2:

        print(cnt)

        #break

        quit()

    else:

        i = a[i] - 1

print((-1))

N = int(input())
a = [int(input()) for _ in range(N)]
cnt, i = 0, 0
for j in range(10**6):
    cnt += 1
    if a[i] == 2:
        print(cnt)
        # break
        quit()
    else:
        i = a[i] - 1
print((-1))

N = int(input())
a = [int(input()) for _ in range(N)]
cnt, i = 0, 0
for j in range(len(a)):
    cnt += 1
    if a[i] == 2:
        print(cnt)
        # break
        quit()
    else:
        i = a[i] - 1
print((-1))

-for j in range(10**6):
+for j in range(len(a)):


Input:
import math

def combinations_count(n, r):

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

def combinations_with_replacement_count(n, r):

    return combinations_count(n + r - 1, r)

def main():

    s = int(input())

    ans = 0

    n = s // 3

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

        ans += combinations_with_replacement_count(i, 3*(n-i)+s % 3)

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

if __name__ == '__main__':

    main()

import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def combinations_with_replacement_count(n, r):
    return combinations_count(n + r - 1, r)

def main():
    s = int(input())
    ans = 0
    n = s // 3
    for i in range(1, n + 1):
        ans += combinations_with_replacement_count(i, 3 * (n - i) + s % 3)
    print((ans % (10**9 + 7)))

if __name__ == "__main__":
    main()

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

-    return combinations_count(n + r - 1, r)

-        ans += combinations_with_replacement_count(i, 3 * (n - i) + s % 3)


Input:
G_MAX = 40*10

N_MAX = 40

INF = float('inf')

dp = [[[INF]*(G_MAX+1) for _ in range(G_MAX+1)]

      for _ in range(N_MAX+1)]

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

a,b,c = [],[],[]

for _ in range(N):

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

    a.append(ai)

    b.append(bi)

    c.append(ci)

dp[0][0][0] = 0

from math import isinf

for i in range(N):

    for ca in range(G_MAX+1):

        for cb in range(G_MAX+1):

            if isinf(dp[i][ca][cb]): continue

            dp[i+1][ca][cb] = min(dp[i+1][ca][cb],

                                 dp[i][ca][cb])

            dp[i+1][ca+a[i]][cb+b[i]] = min(dp[i+1][ca+a[i]][cb+b[i]],

                                           dp[i][ca][cb]+c[i])

ans = INF

for ca in range(1,G_MAX+1):

    for cb in range(1,G_MAX+1):

        if ca*Mb==cb*Ma:

            ans = min(ans, dp[N][ca][cb])

if isinf(ans): ans = -1

print(ans)

from math import isinf

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

a = [0]*N

b = [0]*N

c = [0]*N

for i in range(N):

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

W_MAX = 40*10

dp = [[float('inf')]*(W_MAX+1) for _ in range(W_MAX+1)]

dp[0][0] = 0

for i in range(N):

    for wa in reversed(list(range(W_MAX+1))):

        for wb in reversed(list(range(W_MAX+1))):

            if isinf(dp[wa][wb]): continue

            if wa+a[i]<=W_MAX and wb+b[i]<=W_MAX:

                dp[wa+a[i]][wb+b[i]] = min(dp[wa+a[i]][wb+b[i]],

                                           dp[wa][wb]+c[i])

ans = float('inf')

for wa in range(1, W_MAX+1):

    for wb in range(1, W_MAX+1):

        if wa*Mb == wb*Ma:

            ans = min(ans, dp[wa][wb])

if isinf(ans): ans = -1

print(ans)

G_MAX = 40 * 10
N_MAX = 40
INF = float("inf")
dp = [[[INF] * (G_MAX + 1) for _ in range(G_MAX + 1)] for _ in range(N_MAX + 1)]
N, Ma, Mb = list(map(int, input().split()))
a, b, c = [], [], []
for _ in range(N):
    ai, bi, ci = list(map(int, input().split()))
    a.append(ai)
    b.append(bi)
    c.append(ci)
dp[0][0][0] = 0
from math import isinf

for i in range(N):
    for ca in range(G_MAX + 1):
        for cb in range(G_MAX + 1):
            if isinf(dp[i][ca][cb]):
                continue
            dp[i + 1][ca][cb] = min(dp[i + 1][ca][cb], dp[i][ca][cb])
            dp[i + 1][ca + a[i]][cb + b[i]] = min(
                dp[i + 1][ca + a[i]][cb + b[i]], dp[i][ca][cb] + c[i]
            )
ans = INF
for ca in range(1, G_MAX + 1):
    for cb in range(1, G_MAX + 1):
        if ca * Mb == cb * Ma:
            ans = min(ans, dp[N][ca][cb])
if isinf(ans):
    ans = -1
print(ans)

from math import isinf

N, Ma, Mb = list(map(int, input().split()))
a = [0] * N
b = [0] * N
c = [0] * N
for i in range(N):
    a[i], b[i], c[i] = list(map(int, input().split()))
W_MAX = 40 * 10
dp = [[float("inf")] * (W_MAX + 1) for _ in range(W_MAX + 1)]
dp[0][0] = 0
for i in range(N):
    for wa in reversed(list(range(W_MAX + 1))):
        for wb in reversed(list(range(W_MAX + 1))):
            if isinf(dp[wa][wb]):
                continue
            if wa + a[i] <= W_MAX and wb + b[i] <= W_MAX:
                dp[wa + a[i]][wb + b[i]] = min(
                    dp[wa + a[i]][wb + b[i]], dp[wa][wb] + c[i]
                )
ans = float("inf")
for wa in range(1, W_MAX + 1):
    for wb in range(1, W_MAX + 1):
        if wa * Mb == wb * Ma:
            ans = min(ans, dp[wa][wb])
if isinf(ans):
    ans = -1
print(ans)

-dp = [[[INF] * (G_MAX + 1) for _ in range(G_MAX + 1)] for _ in range(N_MAX + 1)]

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

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

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

+    a[i], b[i], c[i] = list(map(int, input().split()))

+dp = [[float("inf")] * (W_MAX + 1) for _ in range(W_MAX + 1)]

+    for wa in reversed(list(range(W_MAX + 1))):

+        for wb in reversed(list(range(W_MAX + 1))):

-            ans = min(ans, dp[N][ca][cb])

+            if wa + a[i] <= W_MAX and wb + b[i] <= W_MAX:

+                dp[wa + a[i]][wb + b[i]] = min(


Input:
from itertools import product

n = int(input())

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

count = 0

for C in product((-1, 0, 1), repeat=n):

  total = 1

  for a, c in zip(A, C):

    total *= (a + c)

  if total % 2 == 0:

    count += 1

print(count)

n = int(input())

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

count = 1

for a in A:

  count *= 2 if a % 2 == 0 else 1

print((3 ** n - count))

from itertools import product

n = int(input())
A = list(map(int, input().split()))
count = 0
for C in product((-1, 0, 1), repeat=n):
    total = 1
    for a, c in zip(A, C):
        total *= a + c
    if total % 2 == 0:
        count += 1
print(count)

n = int(input())
A = list(map(int, input().split()))
count = 1
for a in A:
    count *= 2 if a % 2 == 0 else 1
print((3**n - count))

-for C in product((-1, 0, 1), repeat=n):


Input:
import sys

sys.setrecursionlimit(2147483647)

INF=float("inf")

MOD=10**9+7

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

def resolve():

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

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

    V=100000

    dp=[INF]*(V+1)

    dp[0]=0

    for w,v in WV:

        for x in range(V,-1,-1):

            if(x+v<=V):

                dp[x+v]=min(dp[x+v],dp[x]+w)

    for x in range(V,-1,-1):

        if(dp[x]<=W):

            print(x)

            return

resolve()

import sys

INF = 1 << 60

MOD = 10**9 + 7 # 998244353

sys.setrecursionlimit(2147483647)

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

def resolve():

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

    dp = [INF] * (n * 1001 + 1)

    dp[0] = 0

    for _ in range(n):

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

        for v0 in range(n * 1001, -1, -1):

            if dp[v0] == INF:

                continue

            if v + v0 <= n * 1001:

                dp[v + v0] = min(dp[v + v0], dp[v0] + w)

    for v in range(n * 1001, -1, -1):

        if dp[v] <= W:

            print(v)

            return

resolve()

import sys

sys.setrecursionlimit(2147483647)
INF = float("inf")
MOD = 10**9 + 7
input = lambda: sys.stdin.readline().rstrip()

def resolve():
    n, W = list(map(int, input().split()))
    WV = [tuple(map(int, input().split())) for _ in range(n)]
    V = 100000
    dp = [INF] * (V + 1)
    dp[0] = 0
    for w, v in WV:
        for x in range(V, -1, -1):
            if x + v <= V:
                dp[x + v] = min(dp[x + v], dp[x] + w)
    for x in range(V, -1, -1):
        if dp[x] <= W:
            print(x)
            return

resolve()

import sys

INF = 1 << 60
MOD = 10**9 + 7  # 998244353
sys.setrecursionlimit(2147483647)
input = lambda: sys.stdin.readline().rstrip()

def resolve():
    n, W = list(map(int, input().split()))
    dp = [INF] * (n * 1001 + 1)
    dp[0] = 0
    for _ in range(n):
        w, v = list(map(int, input().split()))
        for v0 in range(n * 1001, -1, -1):
            if dp[v0] == INF:
                continue
            if v + v0 <= n * 1001:
                dp[v + v0] = min(dp[v + v0], dp[v0] + w)
    for v in range(n * 1001, -1, -1):
        if dp[v] <= W:
            print(v)
            return

resolve()

-    WV = [tuple(map(int, input().split())) for _ in range(n)]

-                dp[x + v] = min(dp[x + v], dp[x] + w)

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


Input:
from collections import defaultdict

def main():

    _ = int(input())

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

    """

    for j>i, j-i = Ai + Aj

    i + Ai = j -Aj

    """

    L = defaultdict(int)

    R = defaultdict(int)

    for i, height in enumerate(heights):

        L[i + height] += 1

        R[i - height] += 1

    total_count = 0

    for x, count_l in list(L.items()):

        if x in R:

            count_r = R[x]

            total_count += count_l * count_r

    print(total_count)

if __name__ == "__main__":

    main()

from collections import defaultdict

def main():

    N = int(input())

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

    """

    for j>i, j-i = Ai + Aj

    i + Ai = j -Aj

    """

    L = defaultdict(int)

    R = defaultdict(int)

    for i, height in enumerate(heights):

        if i + height < N:

            L[i + height] += 1

        if i - height > 0:

            R[i - height] += 1

    total_count = 0

    for x, count_l in list(L.items()):

        if x in R:

            count_r = R[x]

            total_count += count_l * count_r

    print(total_count)

if __name__ == "__main__":

    main()

-    _ = int(input())
+    N = int(input())
-        L[i + height] += 1
-        R[i - height] += 1
+        if i + height < N:
+            L[i + height] += 1
+        if i - height > 0:
+            R[i - height] += 1


Input:
import sys

readline = sys.stdin.readline

sys.setrecursionlimit(10**8)

mod = 10**9+7

#mod = 998244353

INF = 10**18

eps = 10**-7

m,n,k = list(map(int,readline().split()))

def comb(n, r, mod):

    r = min(r, n-r)

    mol = 1

    deno = 1

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

        mol = mol * (n-r+i) % mod

        deno = deno * i % mod

    ret = mol * pow(deno, mod-2, mod) % mod

    return ret

def f1(x,y,a,b):

    return (a+b-x-y)*(a-x+1)*(b-y+1)//2

def f2(x,a):

    return (a-x)*(a-x+1)//2

ans = 0

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

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

        a = n-x+1

        b = m-y+1

        c = m*(a*(a-1)+x*(x-1))

        d = n*(b*(b-1)+y*(y-1))

        ans += (c+d)//2

        ans %= mod

ans = (ans * comb(m*n-2,k-2,mod))%mod

ans = ans*pow(2,mod-2,mod)%mod

print(ans)

import sys

readline = sys.stdin.readline

sys.setrecursionlimit(10**8)

mod = 10**9+7

#mod = 998244353

INF = 10**18

eps = 10**-7

m,n,k = list(map(int,readline().split()))

def comb(n, r, mod):

    r = min(r, n-r)

    mol = 1

    deno = 1

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

        mol = mol * (n-r+i) % mod

        deno = deno * i % mod

    ret = mol * pow(deno, mod-2, mod) % mod

    return ret

ans = m*n*(m+n)*(m*n-1)//3

ans = (ans * comb(m*n-2,k-2,mod))%mod

ans = ans*pow(2,mod-2,mod)%mod

print(ans)

import sys

readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9 + 7
# mod = 998244353
INF = 10**18
eps = 10**-7
m, n, k = list(map(int, readline().split()))

def comb(n, r, mod):
    r = min(r, n - r)
    mol = 1
    deno = 1
    for i in range(1, r + 1):
        mol = mol * (n - r + i) % mod
        deno = deno * i % mod
    ret = mol * pow(deno, mod - 2, mod) % mod
    return ret

def f1(x, y, a, b):
    return (a + b - x - y) * (a - x + 1) * (b - y + 1) // 2

def f2(x, a):
    return (a - x) * (a - x + 1) // 2

ans = 0
for x in range(1, n + 1):
    for y in range(1, m + 1):
        a = n - x + 1
        b = m - y + 1
        c = m * (a * (a - 1) + x * (x - 1))
        d = n * (b * (b - 1) + y * (y - 1))
        ans += (c + d) // 2
        ans %= mod
ans = (ans * comb(m * n - 2, k - 2, mod)) % mod
ans = ans * pow(2, mod - 2, mod) % mod
print(ans)

import sys

readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9 + 7
# mod = 998244353
INF = 10**18
eps = 10**-7
m, n, k = list(map(int, readline().split()))

def comb(n, r, mod):
    r = min(r, n - r)
    mol = 1
    deno = 1
    for i in range(1, r + 1):
        mol = mol * (n - r + i) % mod
        deno = deno * i % mod
    ret = mol * pow(deno, mod - 2, mod) % mod
    return ret

ans = m * n * (m + n) * (m * n - 1) // 3
ans = (ans * comb(m * n - 2, k - 2, mod)) % mod
ans = ans * pow(2, mod - 2, mod) % mod
print(ans)


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

div =[]

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

    cur = 0

    while M%i==0:

        M = M//i

        cur += 1

    if cur >=1:

        div.append(cur)

if M>1: div.append(1)

div.sort()

mod = 10**9+7

frac = [1]*(N+50)

num = len(frac)

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

    frac[i+1] = frac[i]*(i+1)%mod

finv = [1] * (N+50)

finv[-1] = pow(frac[-1], mod-2, mod)

for i in range(1, num):

  finv[num-1-i] = finv[num-i] * (num-i) % mod

ans = 1

for i in div:

    ans = ans * frac[N+i-1]*finv[N-1]*finv[i]%mod

print(ans)

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

div = []

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

    cur = 0

    while M%i==0:

        M//=i

        cur += 1

    if cur >=1:

        div.append(cur)

if M>1:div.append(1)

div.sort()

mod = 10**9+7

frac = [1]*(N+50)

num=len(frac)

for i in range(num-1):

    frac[i+1] = frac[i]*(i+1)%mod

finv = [1]*(N+50)

finv[-1] = pow(frac[-1],mod-2,mod)

ans = 1

for i in range(1,num):

    finv[num-1-i] = finv[num-i]*(num-i)%mod

for i in div:

    ans  =ans*frac[N+i-1]*finv[N-1]*finv[i]%mod

print(ans)

N, M = list(map(int, input().split()))
div = []
for i in range(2, int(M ** (1 / 2)) + 2):
    cur = 0
    while M % i == 0:
        M = M // i
        cur += 1
    if cur >= 1:
        div.append(cur)
if M > 1:
    div.append(1)
div.sort()
mod = 10**9 + 7
frac = [1] * (N + 50)
num = len(frac)
for i in range(len(frac) - 1):
    frac[i + 1] = frac[i] * (i + 1) % mod
finv = [1] * (N + 50)
finv[-1] = pow(frac[-1], mod - 2, mod)
for i in range(1, num):
    finv[num - 1 - i] = finv[num - i] * (num - i) % mod
ans = 1
for i in div:
    ans = ans * frac[N + i - 1] * finv[N - 1] * finv[i] % mod
print(ans)

N, M = list(map(int, input().split()))
div = []
for i in range(2, int(M ** (1 / 2)) + 2):
    cur = 0
    while M % i == 0:
        M //= i
        cur += 1
    if cur >= 1:
        div.append(cur)
if M > 1:
    div.append(1)
div.sort()
mod = 10**9 + 7
frac = [1] * (N + 50)
num = len(frac)
for i in range(num - 1):
    frac[i + 1] = frac[i] * (i + 1) % mod
finv = [1] * (N + 50)
finv[-1] = pow(frac[-1], mod - 2, mod)
ans = 1
for i in range(1, num):
    finv[num - 1 - i] = finv[num - i] * (num - i) % mod
for i in div:
    ans = ans * frac[N + i - 1] * finv[N - 1] * finv[i] % mod
print(ans)

-        M = M // i
+        M //= i
-for i in range(len(frac) - 1):
+for i in range(num - 1):
+ans = 1
-ans = 1


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

# print(a, b, c, x, y)

ans = 10000 * (10 **5) + 7

c_max = 0

if x > y:

  c_max = x * 2

else:

  c_max = y * 2

for cc in range(c_max+1):

  ac = x - (cc // 2)

  bc = y - (cc // 2)

  if ac < 0:

    ac = 0

  if bc < 0:

    bc = 0

  price = ac * a + bc * b + cc * c

  # print('price: {}, ac: {}, bc: {}, cc: {}'.format(price, ac, bc, cc))

  ans = min(ans, price)

print(ans)

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

# print(a, b, c, x, y)

ans = 10000 * (10 **5) + 7

c_max = 0

if x > y:

  c_max = x * 2

else:

  c_max = y * 2

for cc in range(0, c_max+1, 2):

  ac = x - (cc // 2)

  bc = y - (cc // 2)

  if ac < 0:

    ac = 0

  if bc < 0:

    bc = 0

  price = ac * a + bc * b + cc * c

  # print('price: {}, ac: {}, bc: {}, cc: {}'.format(price, ac, bc, cc))

  ans = min(ans, price)

print(ans)

a, b, c, x, y = list(map(int, input().split()))
# print(a, b, c, x, y)
ans = 10000 * (10**5) + 7
c_max = 0
if x > y:
    c_max = x * 2
else:
    c_max = y * 2
for cc in range(c_max + 1):
    ac = x - (cc // 2)
    bc = y - (cc // 2)
    if ac < 0:
        ac = 0
    if bc < 0:
        bc = 0
    price = ac * a + bc * b + cc * c
    # print('price: {}, ac: {}, bc: {}, cc: {}'.format(price, ac, bc, cc))
    ans = min(ans, price)
print(ans)

a, b, c, x, y = list(map(int, input().split()))
# print(a, b, c, x, y)
ans = 10000 * (10**5) + 7
c_max = 0
if x > y:
    c_max = x * 2
else:
    c_max = y * 2
for cc in range(0, c_max + 1, 2):
    ac = x - (cc // 2)
    bc = y - (cc // 2)
    if ac < 0:
        ac = 0
    if bc < 0:
        bc = 0
    price = ac * a + bc * b + cc * c
    # print('price: {}, ac: {}, bc: {}, cc: {}'.format(price, ac, bc, cc))
    ans = min(ans, price)
print(ans)

-for cc in range(c_max + 1):
+for cc in range(0, c_max + 1, 2):


Input:
N = int(input())

name = {'M':0, 'A':0, 'R':0, 'C':0, 'H':0}

march = "MARCH"

for _ in range(N):

  S = input()

  if(S[0] in name):

    name[S[0]] += 1

ans = 0

for i in range(3):

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

    for k in range(j+1,5):

      ans += name[march[i]] * name[march[j]] * name[march[k]]

print(ans)

N = int(input())

S = [input() for _ in range(N)]

head = {'M':0, 'A':0, 'R':0, 'C':0, 'H':0}

march = "MARCH"

for s in S:

  if(s[0] in head):

    head[s[0]] += 1

ans = 0

for i in range(3):

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

    for k in range(j+1,5):

      ans += head[march[i]] * head[march[j]] * head[march[k]]

print(ans)

N = int(input())
name = {"M": 0, "A": 0, "R": 0, "C": 0, "H": 0}
march = "MARCH"
for _ in range(N):
    S = input()
    if S[0] in name:
        name[S[0]] += 1
ans = 0
for i in range(3):
    for j in range(i + 1, 4):
        for k in range(j + 1, 5):
            ans += name[march[i]] * name[march[j]] * name[march[k]]
print(ans)

N = int(input())
S = [input() for _ in range(N)]
head = {"M": 0, "A": 0, "R": 0, "C": 0, "H": 0}
march = "MARCH"
for s in S:
    if s[0] in head:
        head[s[0]] += 1
ans = 0
for i in range(3):
    for j in range(i + 1, 4):
        for k in range(j + 1, 5):
            ans += head[march[i]] * head[march[j]] * head[march[k]]
print(ans)

-            ans += name[march[i]] * name[march[j]] * name[march[k]]

+            ans += head[march[i]] * head[march[j]] * head[march[k]]


Input:
n = int(input())

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

ans = [i for i in range(n)]

a1 = sorted(a)

for i in range(n):

    num = a[i]

    ans[num - 1] = str(i + 1)

print((' '.join(ans)))

n = int(input())

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

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

ans = [0 for _ in range(n)]

for i in range(n):

  ans[a[i] - 1] = str(i + 1)

print((" ".join(ans)))

n = int(input())
a = list(map(int, input().split()))
ans = [i for i in range(n)]
a1 = sorted(a)
for i in range(n):
    num = a[i]
    ans[num - 1] = str(i + 1)
print((" ".join(ans)))

n = int(input())
# n,k = map(int,input().split())
a = list(map(int, input().split()))
ans = [0 for _ in range(n)]
for i in range(n):
    ans[a[i] - 1] = str(i + 1)
print((" ".join(ans)))

+# n,k = map(int,input().split())
-ans = [i for i in range(n)]
-a1 = sorted(a)
+ans = [0 for _ in range(n)]
-    num = a[i]
-    ans[num - 1] = str(i + 1)
+    ans[a[i] - 1] = str(i + 1)


Input:
INF = 2*10**7

def main():

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

    A = []

    B = []

    C = []

    for _ in range(M):

        a, b = (int(i) for i in input().split())

        A.append(a)

        B.append(b)

        bit = 0

        for i in input().split():

            bit |= (1 << (int(i)-1))

        C.append(bit)

    dp = [[INF]*(1 << N) for _ in range(M+1)]

    dp[0][0] = 0

    for i in range(M):

        for j in range(1 << N):

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

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

                next_bit = j | C[i]

                dp[i+1][next_bit] = min(dp[i+1][next_bit], dp[i][j] + A[i])

    ans = dp[-1][-1]

    if ans == INF:

        print((-1))

    else:

        print(ans)

    # print(*dp, sep="\n")

if __name__ == '__main__':

    main()

INF = 2*10**7

def main():

    import sys

    input = sys.stdin.buffer.readline

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

    A = []

    B = []

    C = []

    for _ in range(M):

        a, b = (int(i) for i in input().split())

        A.append(a)

        B.append(b)

        bit = 0

        for i in input().split():

            bit |= (1 << (int(i)-1))

        C.append(bit)

    dp = [[INF]*(1 << N) for _ in range(M+1)]

    dp[0][0] = 0

    for i in range(M):

        for j in range(1 << N):

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

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

                next_bit = j | C[i]

                dp[i+1][next_bit] = min(dp[i+1][next_bit], dp[i][j] + A[i])

    ans = dp[-1][-1]

    if ans == INF:

        print((-1))

    else:

        print(ans)

    # print(*dp, sep="\n")

if __name__ == '__main__':

    main()

INF = 2 * 10**7

def main():
    N, M = (int(i) for i in input().split())
    A = []
    B = []
    C = []
    for _ in range(M):
        a, b = (int(i) for i in input().split())
        A.append(a)
        B.append(b)
        bit = 0
        for i in input().split():
            bit |= 1 << (int(i) - 1)
        C.append(bit)
    dp = [[INF] * (1 << N) for _ in range(M + 1)]
    dp[0][0] = 0
    for i in range(M):
        for j in range(1 << N):
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
            if dp[i][j] != INF:
                next_bit = j | C[i]
                dp[i + 1][next_bit] = min(dp[i + 1][next_bit], dp[i][j] + A[i])
    ans = dp[-1][-1]
    if ans == INF:
        print((-1))
    else:
        print(ans)
    # print(*dp, sep="\n")

if __name__ == "__main__":
    main()

INF = 2 * 10**7

def main():
    import sys

    input = sys.stdin.buffer.readline
    N, M = (int(i) for i in input().split())
    A = []
    B = []
    C = []
    for _ in range(M):
        a, b = (int(i) for i in input().split())
        A.append(a)
        B.append(b)
        bit = 0
        for i in input().split():
            bit |= 1 << (int(i) - 1)
        C.append(bit)
    dp = [[INF] * (1 << N) for _ in range(M + 1)]
    dp[0][0] = 0
    for i in range(M):
        for j in range(1 << N):
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
            if dp[i][j] != INF:
                next_bit = j | C[i]
                dp[i + 1][next_bit] = min(dp[i + 1][next_bit], dp[i][j] + A[i])
    ans = dp[-1][-1]
    if ans == INF:
        print((-1))
    else:
        print(ans)
    # print(*dp, sep="\n")

if __name__ == "__main__":
    main()

+    import sys
+
+    input = sys.stdin.buffer.readline


Input:
def prime_factorize(num):

    """

    This function performs prime factorization on the input natural number.

    The result is returned in the form of a dictionary with the prime number as the key

    and its number as the value.

    :param num:

    :return prime_factor: Dictionary with the prime number as the key and its number as the value.

    """

    prime_factor = {}

    i = 2

    while i ** 2 <= num:

        while num % i == 0:

            num //= i

            if i in list(prime_factor.keys()):

                prime_factor[i] += 1

            else:

                prime_factor[i] = 1

        i += 1

    if num > 1:

        prime_factor[num] = 1

    return prime_factor

from math import factorial

def comb(n, r, R):

    if n not in R:

        a = factorial(n)

        R[n] = a

    else:

        a = R[n]

    if n - r not in R:

        b = factorial(n - r)

        R[n - r] = b

    else:

        b = R[n - r]

    if r not in R:

        c = factorial(r)

        R[r] = c

    else:

        c = R[r]

    return a // (b * c)

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

r = 1

R = dict()

prime_fac = prime_factorize(m)

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

    r *= comb(v + n - 1, v, R)

mod = 1000000000 + 7

print((r % mod))

def prime_factorize(num):

    """

    This function performs prime factorization on the input natural number.

    The result is returned in the form of a dictionary with the prime number as the key

    and its number as the value.

    :param num:

    :return prime_factor: Dictionary with the prime number as the key and its number as the value.

    """

    prime_factor = {}

    i = 2

    while i ** 2 <= num:

        while num % i == 0:

            num //= i

            if i in list(prime_factor.keys()):

                prime_factor[i] += 1

            else:

                prime_factor[i] = 1

        i += 1

    if num > 1:

        prime_factor[num] = 1

    return prime_factor

def combination(n, r):

    r = min(n-r, r)

    result = 1

    for i in range(n, n-r, -1):

        result *= i

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

        result //= i

    return result

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

r = 1

prime_fac = prime_factorize(m)

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

    r *= combination(v + n - 1, v)

mod = 1000000000 + 7

print((r % mod))

def prime_factorize(num):
    """
    This function performs prime factorization on the input natural number.
    The result is returned in the form of a dictionary with the prime number as the key
    and its number as the value.
    :param num:
    :return prime_factor: Dictionary with the prime number as the key and its number as the value.
    """
    prime_factor = {}
    i = 2
    while i**2 <= num:
        while num % i == 0:
            num //= i
            if i in list(prime_factor.keys()):
                prime_factor[i] += 1
            else:
                prime_factor[i] = 1
        i += 1
    if num > 1:
        prime_factor[num] = 1
    return prime_factor

from math import factorial

def comb(n, r, R):
    if n not in R:
        a = factorial(n)
        R[n] = a
    else:
        a = R[n]
    if n - r not in R:
        b = factorial(n - r)
        R[n - r] = b
    else:
        b = R[n - r]
    if r not in R:
        c = factorial(r)
        R[r] = c
    else:
        c = R[r]
    return a // (b * c)

n, m = list(map(int, input().split()))
r = 1
R = dict()
prime_fac = prime_factorize(m)
for v in list(prime_fac.values()):
    r *= comb(v + n - 1, v, R)
mod = 1000000000 + 7
print((r % mod))

def prime_factorize(num):
    """
    This function performs prime factorization on the input natural number.
    The result is returned in the form of a dictionary with the prime number as the key
    and its number as the value.
    :param num:
    :return prime_factor: Dictionary with the prime number as the key and its number as the value.
    """
    prime_factor = {}
    i = 2
    while i**2 <= num:
        while num % i == 0:
            num //= i
            if i in list(prime_factor.keys()):
                prime_factor[i] += 1
            else:
                prime_factor[i] = 1
        i += 1
    if num > 1:
        prime_factor[num] = 1
    return prime_factor

def combination(n, r):
    r = min(n - r, r)
    result = 1
    for i in range(n, n - r, -1):
        result *= i
    for i in range(1, r + 1):
        result //= i
    return result

n, m = list(map(int, input().split()))
r = 1
prime_fac = prime_factorize(m)
for v in list(prime_fac.values()):
    r *= combination(v + n - 1, v)
mod = 1000000000 + 7
print((r % mod))

-from math import factorial
-def comb(n, r, R):
-    if n not in R:
-        a = factorial(n)
-        R[n] = a
-    else:
-        a = R[n]
-    if n - r not in R:
-        b = factorial(n - r)
-        R[n - r] = b
-    else:
-        b = R[n - r]
-    if r not in R:
-        c = factorial(r)
-        R[r] = c
-    else:
-        c = R[r]
-    return a // (b * c)
+def combination(n, r):
+    r = min(n - r, r)
+    result = 1
+    for i in range(n, n - r, -1):
+        result *= i
+    for i in range(1, r + 1):
+        result //= i
+    return result
-R = dict()
-    r *= comb(v + n - 1, v, R)
+    r *= combination(v + n - 1, v)


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

if a < b + c:

    print((b+c-a))

else:

    print((0))

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

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

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

a, b, c = list(map(int, input().split()))
print((max(0, c - (a - b))))

-if a < b + c:
-    print((b + c - a))
-else:
-    print((0))
+print((max(0, c - (a - b))))


Input:
from collections import Counter

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

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

X = Counter(A)

for i in range(M):

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

    X[C] += B

Z = []

for key, value in list(X.items()):

    Z.append((key, value))

Z.sort(key=lambda x: x[0], reverse=True)

ans = 0

cnt = 0

for value, key in Z:

    if cnt + key <= N:

        ans += key * value

        cnt += key

    else:

        ans += (N - cnt) * value

        break

print(ans)

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

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

query = []

for i in range(M):

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

    query.append([b, c])

query.sort(key=lambda q: q[1], reverse=True)

i = 0

for b, c in query:

    while i < N and b > 0:

        if A[i] < c:

            A[i] = c

            b -= 1

        i += 1

print((sum(A)))

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

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

-Z.sort(key=lambda x: x[0], reverse=True)

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

+query.sort(key=lambda q: q[1], reverse=True)


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

if any(i % 2 != 0 for i in [a, b, c]):

    print((0))

    exit()

if a == b and b == c:

    print((-1))

    exit()

cnt = 0

a_list = [0]*(1001001)

b_list = [0]*(1001001)

c_list = [0]*(1001001)

a_list[0] = a

b_list[0] = b

c_list[0] = c

for i in range(1, 1001001):

    a_list[i] = (b_list[i-1] + c_list[i-1]) // 2

    b_list[i] = (c_list[i-1] + a_list[i-1]) // 2

    c_list[i] = (a_list[i-1] + b_list[i-1]) // 2

    cnt += 1

    if a_list[i] % 2 != 0 or b_list[i] % 2 != 0 or c_list[i] % 2 != 0:

        break

print(cnt)

def solve(a, b, c):

    if any(i % 2 == 1 for i in [a, b, c]):

        return 0

    if a == b == c:

        return -1

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

    return solve(a, b, c) + 1

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

print((solve(a, b, c)))

+    if any(i % 2 == 1 for i in [a, b, c]):


Input:
from functools import lru_cache

import sys

sys.setrecursionlimit(30000)

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

a = list(int(input()) for _ in range(M))

MOD = pow(10, 9) + 7

isOk = True

for i in range(M-1):

    if a[i] + 1 == a[i+1]:

        isOk = False

funcb = [-1] * 100001

def func(n):

    if n <= 0:

        return 1

    cnt = funcb[n]

    if cnt > 0:

        return cnt

    cnt = 0

    if n >= 2:

        cnt += func(n-2)

    if n >= 1:

        cnt += func(n-1)

    funcb[n] = cnt % MOD

    return funcb[n]

func(30000)

func(50000)

func(80000)

func(100000)

if not isOk:

    print((0))

else:

    ans = 1

    s = 0

    for i in range(M+1):

        if i >= M:

            e = N

        else:

            e = a[i] - 1

        ans = ans * func(e-s) % MOD

        if i >= M:

            break

        s = a[i] + 1

    print(ans)

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

a = list(int(input()) for _ in range(M))

MOD = pow(10, 9) + 7

MAP = [0] * (N + 1)

for x in a:

    MAP[x] = -1

MAP[0] = 1

for i in range(N):

    if MAP[i] < 0:

        continue

    MAP[i] %= MOD

    if i + 1 <= N and MAP[i+1] >= 0:

        MAP[i+1] += MAP[i]

    if i + 2 <= N and MAP[i+2] >= 0:

        MAP[i+2] += MAP[i]

print((MAP[N] % MOD))

from functools import lru_cache
import sys

sys.setrecursionlimit(30000)
N, M = list(map(int, input().split()))
a = list(int(input()) for _ in range(M))
MOD = pow(10, 9) + 7
isOk = True
for i in range(M - 1):
    if a[i] + 1 == a[i + 1]:
        isOk = False
funcb = [-1] * 100001

def func(n):
    if n <= 0:
        return 1
    cnt = funcb[n]
    if cnt > 0:
        return cnt
    cnt = 0
    if n >= 2:
        cnt += func(n - 2)
    if n >= 1:
        cnt += func(n - 1)
    funcb[n] = cnt % MOD
    return funcb[n]

func(30000)
func(50000)
func(80000)
func(100000)
if not isOk:
    print((0))
else:
    ans = 1
    s = 0
    for i in range(M + 1):
        if i >= M:
            e = N
        else:
            e = a[i] - 1
        ans = ans * func(e - s) % MOD
        if i >= M:
            break
        s = a[i] + 1
    print(ans)

N, M = list(map(int, input().split()))
a = list(int(input()) for _ in range(M))
MOD = pow(10, 9) + 7
MAP = [0] * (N + 1)
for x in a:
    MAP[x] = -1
MAP[0] = 1
for i in range(N):
    if MAP[i] < 0:
        continue
    MAP[i] %= MOD
    if i + 1 <= N and MAP[i + 1] >= 0:
        MAP[i + 1] += MAP[i]
    if i + 2 <= N and MAP[i + 2] >= 0:
        MAP[i + 2] += MAP[i]
print((MAP[N] % MOD))

-from functools import lru_cache
-import sys
-sys.setrecursionlimit(30000)
-isOk = True
-for i in range(M - 1):
-    if a[i] + 1 == a[i + 1]:
-        isOk = False
-funcb = [-1] * 100001
-def func(n):
-    if n <= 0:
-        return 1
-    cnt = funcb[n]
-    if cnt > 0:
-        return cnt
-    cnt = 0
-    if n >= 2:
-        cnt += func(n - 2)
-    if n >= 1:
-        cnt += func(n - 1)
-    funcb[n] = cnt % MOD
-    return funcb[n]
-func(30000)
-func(50000)
-func(80000)
-func(100000)
-if not isOk:
-    print((0))
-else:
-    ans = 1
-    s = 0
-    for i in range(M + 1):
-        if i >= M:
-            e = N
-        else:
-            e = a[i] - 1
-        ans = ans * func(e - s) % MOD
-        if i >= M:
-            break
-        s = a[i] + 1
-    print(ans)
+MAP = [0] * (N + 1)
+for x in a:
+    MAP[x] = -1
+MAP[0] = 1
+for i in range(N):
+    if MAP[i] < 0:
+        continue
+    MAP[i] %= MOD
+    if i + 1 <= N and MAP[i + 1] >= 0:
+        MAP[i + 1] += MAP[i]
+    if i + 2 <= N and MAP[i + 2] >= 0:
+        MAP[i + 2] += MAP[i]
+print((MAP[N] % MOD))


Input:
# 2019-11-17 20:44:23(JST)

import sys

# import collections

# import math

# from string import ascii_lowercase, ascii_uppercase, digits

# from bisect import bisect_left as bi_l, bisect_right as bi_r

# import itertools

# from functools import reduce

# import operator as op

# import re

# import heapq

# import array

# from scipy.misc import comb # (default: exact=False)

# import numpy as np

def main():

    s, t = sys.stdin.read().split()

    count = 0

    for i in range(3):

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

            count += 1

    print(count)

if __name__ == "__main__":

    main()

import sys

s, t = sys.stdin.read().split()

def main():

    cnt = 0

    for i in range(3):

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

            cnt += 1

    return cnt

if __name__ == '__main__':

    ans = main()

    print(ans)

import sys

s, t = sys.stdin.read().split()

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

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

-# from string import ascii_lowercase, ascii_uppercase, digits

-# from bisect import bisect_left as bi_l, bisect_right as bi_r

-# from scipy.misc import comb # (default: exact=False)


Input:
#                         author:  kagemeka

#                         created: 2019-11-06 21:30:22(JST)

import sys

# import collections

# import math

# import string

# import bisect

# import re

# import itertools

# import statistics

# import functools

# import operator

def main():

    a, b = (int(x) for x in sys.stdin.read().split())

    count = 0

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

        n = str(n)

        if n == n[::-1]:

            count += 1

    print(count)

if __name__ == "__main__":

    # execute only if run as a script

    main()

import sys

from bisect import bisect_left as bi_l, bisect_right as bi_r

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

def main():

    palindromic_nums = []

    for i in range(1, 10):

        for j in range(10):

            for k in range(10):

                x = [i, j, k, j, i]

                palindromic_nums.append(int(''.join(list(map(str, x)))))

    return bi_r(palindromic_nums, b) - bi_l(palindromic_nums, a)

if __name__ == '__main__':

    ans = main()

    print(ans)

#                         author:  kagemeka
#                         created: 2019-11-06 21:30:22(JST)
import sys

# import collections
# import math
# import string
# import bisect
# import re
# import itertools
# import statistics
# import functools
# import operator
def main():
    a, b = (int(x) for x in sys.stdin.read().split())
    count = 0
    for n in range(a, b + 1):
        n = str(n)
        if n == n[::-1]:
            count += 1
    print(count)

if __name__ == "__main__":
    # execute only if run as a script
    main()

import sys
from bisect import bisect_left as bi_l, bisect_right as bi_r

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

def main():
    palindromic_nums = []
    for i in range(1, 10):
        for j in range(10):
            for k in range(10):
                x = [i, j, k, j, i]
                palindromic_nums.append(int("".join(list(map(str, x)))))
    return bi_r(palindromic_nums, b) - bi_l(palindromic_nums, a)

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

-#                         author:  kagemeka

+from bisect import bisect_left as bi_l, bisect_right as bi_r

+a, b = list(map(int, sys.stdin.readline().split()))

-    a, b = (int(x) for x in sys.stdin.read().split())

+                palindromic_nums.append(int("".join(list(map(str, x)))))

+    return bi_r(palindromic_nums, b) - bi_l(palindromic_nums, a)


Input:
N = int(input())

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

X = list(sorted(X))

if N == 1:

  print((0))

  exit()

ans = float('inf')

for p in range(X[0], X[N - 1] + 1):

  sum = 0

  for x in X:

    sum += (x - p) ** 2

  ans = min(sum, ans)

print(ans)

N = int(input())

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

avg = round(sum(X) / N)

print((sum([(x - avg) ** 2 for x in X])))

N = int(input())
X = list(map(int, input().split()))
X = list(sorted(X))
if N == 1:
    print((0))
    exit()
ans = float("inf")
for p in range(X[0], X[N - 1] + 1):
    sum = 0
    for x in X:
        sum += (x - p) ** 2
    ans = min(sum, ans)
print(ans)

+print((sum([(x - avg) ** 2 for x in X])))


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

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

d = [0] * n

d_max = 0

for i in range(n - 1):

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

    d_max = max(d_max, d[i])

d[-1] = (k - a[-1]) + a[0]

d_max = max(d_max, d[-1])

print((k - d_max))

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

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

d_max = 0

for i in range(n - 1):

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

    d_max = max(d_max, d)

d = (k - a[-1]) + a[0]

d_max = max(d_max, d)

print((k - d_max))


Input:
import sys

input = sys.stdin.readline

from collections import deque

N = int(input())

X = [[] for i in range(N)]

for i in range(N-1):

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

    X[x-1].append(y-1)

    X[y-1].append(x-1)

i0 = min([i for i in range(N) if len(X[i]) == 1])

P = [-1] * N

Q = deque([i0])

R = []

while Q:

    i = deque.popleft(Q)

    R.append(i)

    for a in X[i]:

        if a != P[i]:

            P[a] = i

            X[a].remove(i)

            deque.append(Q, a)

mod = 10 ** 9 + 7

inv2 = (mod + 1) // 2

A = [1] * N

for i in R[::-1]:

    s = 0

    for j in X[i]:

        A[i] += A[j]

ans = 0

for i in range(N):

    s = pow(inv2, N - A[i], mod)

    t = 1 + (1-pow(inv2, N - A[i], mod)) * pow(2, N - A[i], mod)

    for j in X[i]:

        s = (s * pow(inv2, A[j], mod)) % mod

        t = (t + (1-pow(inv2, A[j], mod)) * pow(2, A[j], mod)) % mod

    ans = (ans + 1 - s * t) % mod

print((ans * inv2 % mod))

import sys

input = sys.stdin.readline

from collections import deque

N = int(input())

X = [[] for i in range(N)]

for i in range(N-1):

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

    X[x-1].append(y-1)

    X[y-1].append(x-1)

i0 = min([i for i in range(N) if len(X[i]) == 1])

P = [-1] * N

Q = deque([i0])

R = []

while Q:

    i = deque.popleft(Q)

    R.append(i)

    for a in X[i]:

        if a != P[i]:

            P[a] = i

            X[a].remove(i)

            deque.append(Q, a)

mod = 10 ** 9 + 7

pow2 = [1]

for i in range(N + 10):

    pow2.append(pow2[-1] * 2 % mod)

inv2 = (mod + 1) // 2

invpow2 = [pow(pow2[-1], mod-2, mod)]

for i in range(N + 10)[::-1]:

    invpow2.append(invpow2[-1] * 2 % mod)

invpow2 = invpow2[::-1]

A = [1] * N

for i in R[::-1]:

    s = 0

    for j in X[i]:

        A[i] += A[j]

ans = 0

for i in range(N):

    s = pow(inv2, N - A[i], mod)

    t = 1 + (1-invpow2[N-A[i]]) * pow2[N-A[i]]

    for j in X[i]:

        s = (s * invpow2[A[j]]) % mod

        t = (t + (1-invpow2[A[j]]) * pow2[A[j]]) % mod

    ans = (ans + 1 - s * t) % mod

print((ans * inv2 % mod))

import sys

input = sys.stdin.readline
from collections import deque

N = int(input())
X = [[] for i in range(N)]
for i in range(N - 1):
    x, y = list(map(int, input().split()))
    X[x - 1].append(y - 1)
    X[y - 1].append(x - 1)
i0 = min([i for i in range(N) if len(X[i]) == 1])
P = [-1] * N
Q = deque([i0])
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)
mod = 10**9 + 7
inv2 = (mod + 1) // 2
A = [1] * N
for i in R[::-1]:
    s = 0
    for j in X[i]:
        A[i] += A[j]
ans = 0
for i in range(N):
    s = pow(inv2, N - A[i], mod)
    t = 1 + (1 - pow(inv2, N - A[i], mod)) * pow(2, N - A[i], mod)
    for j in X[i]:
        s = (s * pow(inv2, A[j], mod)) % mod
        t = (t + (1 - pow(inv2, A[j], mod)) * pow(2, A[j], mod)) % mod
    ans = (ans + 1 - s * t) % mod
print((ans * inv2 % mod))

import sys

input = sys.stdin.readline
from collections import deque

N = int(input())
X = [[] for i in range(N)]
for i in range(N - 1):
    x, y = list(map(int, input().split()))
    X[x - 1].append(y - 1)
    X[y - 1].append(x - 1)
i0 = min([i for i in range(N) if len(X[i]) == 1])
P = [-1] * N
Q = deque([i0])
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)
mod = 10**9 + 7
pow2 = [1]
for i in range(N + 10):
    pow2.append(pow2[-1] * 2 % mod)
inv2 = (mod + 1) // 2
invpow2 = [pow(pow2[-1], mod - 2, mod)]
for i in range(N + 10)[::-1]:
    invpow2.append(invpow2[-1] * 2 % mod)
invpow2 = invpow2[::-1]
A = [1] * N
for i in R[::-1]:
    s = 0
    for j in X[i]:
        A[i] += A[j]
ans = 0
for i in range(N):
    s = pow(inv2, N - A[i], mod)
    t = 1 + (1 - invpow2[N - A[i]]) * pow2[N - A[i]]
    for j in X[i]:
        s = (s * invpow2[A[j]]) % mod
        t = (t + (1 - invpow2[A[j]]) * pow2[A[j]]) % mod
    ans = (ans + 1 - s * t) % mod
print((ans * inv2 % mod))

+invpow2 = [pow(pow2[-1], mod - 2, mod)]

+    invpow2.append(invpow2[-1] * 2 % mod)

-        s = (s * pow(inv2, A[j], mod)) % mod


Input:
from math import factorial

def myAnswer(N:int) ->int:

   ans =factorial(N)

   return ans % (10**9 + 7)

def modelAnswer():

   tmp=1

def main():

   N = int(input())

   print((myAnswer(N)))

if __name__ == '__main__':

   main()

def modelAnswer(N:int) -> int:

   ans = 1

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

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

   return ans

def main():

   N = int(input())

   print((modelAnswer(N)))

if __name__ == '__main__':

   main()

from math import factorial

def myAnswer(N: int) -> int:
    ans = factorial(N)
    return ans % (10**9 + 7)

def modelAnswer():
    tmp = 1

def main():
    N = int(input())
    print((myAnswer(N)))

if __name__ == "__main__":
    main()

def modelAnswer(N: int) -> int:
    ans = 1
    for i in range(1, N + 1):
        ans = (ans * i) % (10**9 + 7)
    return ans

def main():
    N = int(input())
    print((modelAnswer(N)))

if __name__ == "__main__":
    main()

-from math import factorial
-def myAnswer(N: int) -> int:
-    ans = factorial(N)
-    return ans % (10**9 + 7)
-def modelAnswer():
-    tmp = 1
+def modelAnswer(N: int) -> int:
+    ans = 1
+    for i in range(1, N + 1):
+        ans = (ans * i) % (10**9 + 7)
+    return ans
-    print((myAnswer(N)))
+    print((modelAnswer(N)))


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

while True:

    if len(s) == 0:

        break

    if s[:7] == "remaerd":

        s = s[7:]

    elif s[:6] == "resare":

        s = s[6:]

    elif s[:5] == "maerd" or s[:5] == "esare":

        s = s[5:]

    else:

        print("NO")

        exit()

print("YES")

import re

s = input()

if re.fullmatch(r"(dream|dreamer|erase|eraser)+", s):

    print("YES")

else:

    print("NO")

-    elif s[:5] == "maerd" or s[:5] == "esare":


Input:
import sys

stdin = sys.stdin

ni = lambda: int(ns())

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

ns = lambda: stdin.readline()

def Solve():

    N,K=na()

    S=[int(d) for d in input()]

    l=[]

    N=len(S)

    if(S[0]==0):

        l.append(0)

    b=S[0]

    ct=1

    for i in S[1:]:

        if(b!=i):

            l.append(ct)

            ct=1

            b=i

        else:

            ct+=1

    l.append(ct)

    len_l=len(l)

    if(len_l//2 <= K):

        return sum(l)

    ans = res = sum(l[:2*K+1])

    if(len_l%2==0):

        l.append(0)

        len_l+=1

    g = K * 2 + 1

    for i in range(K*2+1,len_l-1,2):

        ans -= l[i-g] + l[i-g+1]

        ans += l[i] + l[1+i]

        if(res<ans):

            res=ans

    #print(S)

    #print(l)

    #print(len_l)

    return res

print((Solve()))

import sys

stdin = sys.stdin

ni = lambda: int(ns())

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

ns = lambda: stdin.readline()

def Solve():

    N,K=na()

    S=input()

    l=[]

    N=len(S)

    #print(len(S))

    if(S[0]=="0"):

        #print("00")

        l.append(0)

    t=S[0]

    ct=1

    #print("aa")

    #print(S)

    #print(S[0],S[1],S[2])

    for b in S[1:]:

        #print(b)

        if(t!=b):

            l.append(ct)

            ct=1

            t=b

        else:

            ct+=1

    #print("bb")

    l.append(ct)

    len_l=len(l)

    if(len_l//2 <= K):

        return sum(l)

    ans = res = sum(l[:2*K+1])

    if(len_l%2==0):

        l.append(0)

        len_l+=1

    g = K * 2 + 1

    for i in range(K*2+1,len_l-1,2):

        ans -= l[i-g] + l[i-g+1]

        ans += l[i] + l[1+i]

        if(res<ans):

            res=ans

    #print(S)

    #print(l)

    #print(len_l)

    return res

print((Solve()))

import sys

stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

def Solve():
    N, K = na()
    S = [int(d) for d in input()]
    l = []
    N = len(S)
    if S[0] == 0:
        l.append(0)
    b = S[0]
    ct = 1
    for i in S[1:]:
        if b != i:
            l.append(ct)
            ct = 1
            b = i
        else:
            ct += 1
    l.append(ct)
    len_l = len(l)
    if len_l // 2 <= K:
        return sum(l)
    ans = res = sum(l[: 2 * K + 1])
    if len_l % 2 == 0:
        l.append(0)
        len_l += 1
    g = K * 2 + 1
    for i in range(K * 2 + 1, len_l - 1, 2):
        ans -= l[i - g] + l[i - g + 1]
        ans += l[i] + l[1 + i]
        if res < ans:
            res = ans
    # print(S)
    # print(l)
    # print(len_l)
    return res

print((Solve()))

import sys

stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

def Solve():
    N, K = na()
    S = input()
    l = []
    N = len(S)
    # print(len(S))
    if S[0] == "0":
        # print("00")
        l.append(0)
    t = S[0]
    ct = 1
    # print("aa")
    # print(S)
    # print(S[0],S[1],S[2])
    for b in S[1:]:
        # print(b)
        if t != b:
            l.append(ct)
            ct = 1
            t = b
        else:
            ct += 1
    # print("bb")
    l.append(ct)
    len_l = len(l)
    if len_l // 2 <= K:
        return sum(l)
    ans = res = sum(l[: 2 * K + 1])
    if len_l % 2 == 0:
        l.append(0)
        len_l += 1
    g = K * 2 + 1
    for i in range(K * 2 + 1, len_l - 1, 2):
        ans -= l[i - g] + l[i - g + 1]
        ans += l[i] + l[1 + i]
        if res < ans:
            res = ans
    # print(S)
    # print(l)
    # print(len_l)
    return res

print((Solve()))


Input:
def check(x, y):

    for a, b in zip(x, y):

        if a != b and b != -1:

            return False

    else:

        return True

n = int(input())

remarks = []

cnt_max = 0

for _ in range(n):

    a = int(input())

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

    r = [-1] * n

    for i, j in x:

        r[i - 1] = j

    remarks.append(r)

for r in range(2 ** n):

    r = list(bin(r)[2:].zfill(n))

    r = [int(i) for i in r[:]]

    cnt = 0

    for i in range(n):

        if r[i] == 1:

            flag = check(r, remarks[i])

            cnt += 1

            if not flag:

                cnt = 0

                break

    cnt_max = max(cnt, cnt_max)

print(cnt_max)

import numpy as np

import itertools

n = int(input())

info = []

for i in range(n):

    a = int(input())

    for _ in range(a):

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

        info.append((i, x - 1, y))

a = np.array(list(itertools.product([0, 1], repeat=n)), dtype=np.bool)

for i, x, y in info:

    bl = (~a[:, i]) | (a[:, x] == bool(y))

    a = a[bl]

ans = a.sum(axis=1).max()

print(ans)

def check(x, y):
    for a, b in zip(x, y):
        if a != b and b != -1:
            return False
    else:
        return True

n = int(input())
remarks = []
cnt_max = 0
for _ in range(n):
    a = int(input())
    x = [tuple(map(int, input().split())) for i in range(a)]
    r = [-1] * n
    for i, j in x:
        r[i - 1] = j
    remarks.append(r)
for r in range(2**n):
    r = list(bin(r)[2:].zfill(n))
    r = [int(i) for i in r[:]]
    cnt = 0
    for i in range(n):
        if r[i] == 1:
            flag = check(r, remarks[i])
            cnt += 1
            if not flag:
                cnt = 0
                break
    cnt_max = max(cnt, cnt_max)
print(cnt_max)

-    x = [tuple(map(int, input().split())) for i in range(a)]

-            flag = check(r, remarks[i])

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

+a = np.array(list(itertools.product([0, 1], repeat=n)), dtype=np.bool)


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

count_target = 0

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

    str_i = str(i)

    reversed_i = ''

    for n_th_i in reversed(list(range(len(str_i)))):

        reversed_i += str_i[n_th_i]

    if str_i == reversed_i:

        count_target += 1

print(count_target)

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

count_target = 0

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

    str_i = str(i)

    reversed_i = str_i[::-1]

    if str_i == reversed_i:

        count_target += 1

print(count_target)

A, B = list(map(int, input().split()))
count_target = 0
for i in range(A, B + 1):
    str_i = str(i)
    reversed_i = ""
    for n_th_i in reversed(list(range(len(str_i)))):
        reversed_i += str_i[n_th_i]
    if str_i == reversed_i:
        count_target += 1
print(count_target)

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

-    for n_th_i in reversed(list(range(len(str_i)))):