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

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, atan, degrees
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
import heapq

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
	N, K = MAP()
	A = LIST()
	F = LIST()

	A.sort(reverse=True)
	F.sort()

	time = [A[i]*F[i] for i in range(N)]
	# print(F)
	# print(time)
	upper = max(time)

	def is_ok(x):  # 時間をx以下にできるか？
		tmp = 0
		for i in range(N):
			if time[i] <= x:
				pass
			else:
				tmp += ceil((time[i]-x)/F[i])
		if tmp > K:
			return False
		else:
			return True

	min_ = -1
	max_ = upper
	while min_ + 1 != max_:
		search = (min_+max_)//2
		# print(min_, max_, search)
		if is_ok(search):
			max_ = search
		else:
			min_ = search

	print(max_)


if __name__ == "__main__":
	main()


### FIXED CODE ###

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, atan, degrees
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
import heapq

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
A = LIST()
F = LIST()

A.sort(reverse=True)
F.sort()

time = [A[i]*F[i] for i in range(N)]
# print(F)
# print(time)

def is_ok(x):  # 時間をx以下にできるか？
	tmp = 0
	for i in range(N):
		if time[i] <= x:
			pass
		else:
			tmp += ceil((time[i]-x)/F[i])
	if tmp > K:
		return False
	else:
		return True

min_ = -1
max_ = 10**18
while min_ + 1 != max_:
	search = (min_+max_)//2
	# print(min_, max_, search)
	if is_ok(search):
		max_ = search
	else:
		min_ = search

print(max_)
### BUGGY CODE ###

import sys
from collections import defaultdict
from collections import deque
import numpy as np
import heapq
from heapq import heappush, heappop
import itertools


#read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 7)

N = int(input())

PATH = []
COLOR = [[None for _ in range(N+1)] for _ in range(N+1)]
#COLOR = defaultdict(int)
PT = [[] for _ in range(N+1)]
for i in range(N-1):
    a, b = map(int, input().split())
    PATH.append((a, b))
    PT[a].append(b)
    PT[b].append(a)


q = deque([(1, 0)])
cmax = 1
while(q):
    a, ca = q.popleft()
    bs = PT[a]

    if len(bs) > cmax:
        cmax = len(bs)

    c = list(range(1, cmax+1))
    for b in bs:
        if COLOR[a][b] != None:
            continue

        clr = c.pop()
        if clr == ca:
            clr = c.pop()
        COLOR[a][b] = clr
        COLOR[b][a] = clr
        q.append((b, clr))

print(cmax)

for a, b in PATH:
    print(COLOR[a][b])


### FIXED CODE ###

import sys
from collections import defaultdict
from collections import deque
import numpy as np
import heapq
from heapq import heappush, heappop
import itertools


#read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 7)

N = int(input())

PATH = []
#COLOR = [[None for _ in range(N+1)] for _ in range(N+1)]
COLOR = defaultdict(int)
PT = [[] for _ in range(N+1)]
cmax = 1
for i in range(N-1):
    a, b = map(int, input().split())
    PATH.append((a, b))
    PT[a].append(b)
    PT[b].append(a)

    '''
    la = len(PT[a])
    lb = len(PT[b])

    l = la if la > lb else lb
    if l > cmax:
        cmax = l
    '''

q = deque([(1, -1)])
while(q):
    a, ng = q.popleft()
    bs = PT[a]

    c = 1
    for b in bs:
        if b > a:
            na = a
            nb = b
        else:
            na = b
            nb = a

        cab = COLOR[(na, nb)]

        if cab == ng:
            continue

        if cab == 0:
            if c == ng:
                c += 1
            clr = c
            COLOR[(na, nb)] = clr
            q.append((b, clr))
            c += 1

print(max(COLOR.values()))

clr = []
for a, b in PATH:
    clr.append(COLOR[(a, b)])

print("\n".join(map(str, clr)))
### BUGGY CODE ###

N = int(input())
S = input()
res = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            tnp = [i,j,k]
            flag = 0
            for s in S:
                if int(s) == tnp[flag]:
                    flag += 1
                if flag == 3:
                    res += 1
                    break
print(res)

### FIXED CODE ###

N = int(input())
S = input()
res = 0
for i in range(10):
    for j in range(10):
        for k in range(10):
            tnp = [i,j,k]
            flag = 0
            for s in S:
                if int(s) == tnp[flag]:
                    flag += 1
                if flag == 3:
                    res += 1
                    break
print(res)
### BUGGY CODE ###

import sys
read = sys.stdin.read
from itertools import permutations
def main():
    def warshall_floyd(d):
        for k in range(n):
            for i in range(n):
                for j in range(n):
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j])
        return d
 
    n, m, r = map(int, input().split())
    rs = tuple(map(int, input().split()))
    d = [[10**9] * n for _ in range(n)]
    m = map(int, read().split())
    mmm = zip(m, m, m)
    for s, t, dis in mmm:
        s -= 1
        t -= 1
        d[t][s] = d[s][t] = dis
    for j1 in range(n):
        d[j1][j1] = 0
    res = warshall_floyd(d)
    rsp = tuple(permutations(rs))
    result = float('inf')
    for rspe in rsp:
        res_t = 0
        for i1 in range(r - 1):
            res_t += res[rspe[i1]-1][rspe[i1+1]-1]
        result = min(result, res_t)
    print(result)
 
if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
read = sys.stdin.read
from itertools import permutations
def main():
    def warshall_floyd(d):
        for k in range(n):
            for i in range(n):
                for j in range(n):
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j])
        return d
 
    n, m, r = map(int, input().split())
    rs = tuple(map(int, input().split()))
    d = [[10**9] * n for _ in range(n)]
    m = map(int, read().split())
    mmm = zip(m, m, m)
    for s, t, dis in mmm:
        s -= 1
        t -= 1
        d[t][s] = d[s][t] = dis
    for j1 in range(n):
        d[j1][j1] = 0
    res = warshall_floyd(d)
    rsp = tuple(permutations(rs))
    result = float('inf')
    for rspe in rsp:
        res_t = 0
        for i1 in range(r - 1):
            res_t += res[rspe[i1]-1][rspe[i1+1]-1]
        result = min(result, res_t)
    print(result)
 
if __name__ == '__main__':
    main()
### BUGGY CODE ###

import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  H,W,M=LI()
  h=[0]*H
  w=[0]*W
  lh=[]
  lw=[]
  d={}
  for _ in range(M):
    a,b=LI()
    lh.append(a)
    lw.append(b)
    a-=1
    b-=1
    h[a]+=1
    w[b]+=1
    d[(a,b)]=1

  ans=0
  for a in lh:
    a-=1
    for b in lw:
      b-=1
      _ans=h[a]+w[b]
      if (a,b) in d:
        _ans-=1
      ans=max(ans,_ans)

  return ans

# main()
print(main())


### FIXED CODE ###

import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  H,W,M=LI()
  h=[0]*H
  w=[0]*W
  d={}
  for _ in range(M):
    a,b=LI()
    a-=1
    b-=1
    h[a]+=1
    w[b]+=1
    d[(a,b)]=1

  h_koho=[]
  w_koho=[]

  h_max=max(h)
  w_max=max(w)

  for i,x in enumerate(h):
    if x==h_max:
      h_koho.append(i)
  for i,x in enumerate(w):
    if x==w_max:
      w_koho.append(i)

  for x in h_koho:
    for y in w_koho:
      if (x,y) not in d:
        return h_max+w_max

  return h_max+w_max-1

# main()
print(main())
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
for i in range (n):
    print (a.count(i+1))

### FIXED CODE ###

n=int(input())-1
a=list(map(int,input().split()))
r=[]
for i in range(n):
    r.append(0)
    r[a[i]-1]+=1
for i in range (n):
    print (r[i])
print (0)
### BUGGY CODE ###

S = input()

S = S.split()

L = int(S[0])
R = int(S[1])
min = 2 * (10**10)
for i in range(L, R, 1):
    for j in range(i+1, R+1, 1):
        mod = (i*j) % 2019
        if mod < min:
            min = mod

print(min)


### FIXED CODE ###


S = input()

S = S.split()

L = int(S[0])
R = int(S[1])
min = 2 * (10**10)

flg = 0

for i in range(L, R, 1):
    if flg ==0:
        for j in range(i+1, R+1, 1):
            mod = (i*j) % 2019
            if mod < min:
                min = mod
                if min == 0:
                    flg = 1
                    break
    else:
        break

print(min)
### BUGGY CODE ###

N = int(input())
L = []
C = []
A = []
b = w = 0
b_d = {}
w_d = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    n = 0
    for cc, aa in zip(C, A):
        if cc == c and aa > a:
            n += 1
    L.append([0] * (N + 1))
    cnt = 0
    for j in range(N, -1, -1):
        L[-1][j] = cnt+n
        if j in (b_d if c=="W" else w_d):
            cnt+=1

    if c=="B":
        b_d[a] = i
        b+=1
    else:
        w_d[a] = i
        w+=1
    C.append(c)
    A.append(a)
#print(L, b_d, w_d)

dp = [[0]*(N+2) for i in range(N+2)]
for i in range(N+1):
    for j in range(N+1):
        if i==j==0:
            continue
        if i==0:
            dp[i][j] = dp[i][j-1] + L[b_d[j]][i]
        elif j==0:
            dp[i][j] = dp[i-1][j] + L[w_d[i]][j]
        else:
            dp[i][j] = min(dp[i-1][j] + L[w_d[i]][j],
                           dp[i][j-1] + L[b_d[j]][i])
print(dp[-2][-2])


### FIXED CODE ###

N = int(input())
L = []
C = []
A = []
b = w = 0
b_d = {}
w_d = {}
for i in range(2*N):
    c, a = input().split()
    a = int(a)
    n = 0
    for cc, aa in zip(C, A):
        if cc == c and aa > a:
            n += 1
    L.append([0] * (N + 1))
    cnt = 0
    for j in range(N, -1, -1):
        L[-1][j] = cnt+n
        if j in (b_d if c=="W" else w_d):
            cnt+=1

    if c=="B":
        b_d[a] = i
        b+=1
    else:
        w_d[a] = i
        w+=1
    C.append(c)
    A.append(a)
#print(L, b_d, w_d)

dp = [[0]*(N+2) for i in range(N+2)]
for i in range(N+1):
    for j in range(N+1):
        if i==j==0:
            continue
        if i==0:
            dp[i][j] = dp[i][j-1] + L[b_d[j]][i]
        elif j==0:
            dp[i][j] = dp[i-1][j] + L[w_d[i]][j]
        else:
            dp[i][j] = min(dp[i-1][j] + L[w_d[i]][j],
                           dp[i][j-1] + L[b_d[j]][i])
print(dp[-2][-2])
### BUGGY CODE ###

import sys

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

def solve():
  ret = 1
  for i in range(N):
    ret *= A[i]
  if ret > 1e18:
      ret = -1
  return ret

print(solve())

### FIXED CODE ###

import sys

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

U = 10**18
ret = 1

for x in A:
  ret *= x
  if ret > U:
    ret = U+1
    
if ret > U:
  ret = -1

print(ret)
### BUGGY CODE ###

N=int(input())
S=input()

# 各文字がどこまでにいくつ含まれるかを累積和で管理
R=[0]*N
G=[0]*N
B=[0]*N

dic={}
for c in ("R","G","B"):
  dic[c] = [0] * N

rp=0
gp=0
bp=0
for i in range(len(S)):
  if S[i]=="R":
    rp+=1
    dic["R"][i]=rp
  elif S[i]=="G":
    gp+=1
    dic["G"][i]=gp
  elif S[i]=="B":
    bp+=1
    dic["B"][i]=bp

def getRest(s1,s2):
  for c in ["R","G","B"]:
    if c not in (s1,s2):
      return c

ans = 0
for i in range(N-2):
  for j in range(i+1,N-1):
    # 最初の2つの文字を固定する
    if S[i] == S[j]:
      continue
    # 探すべき文字を特定
    rest = getRest(S[i],S[j])
    ans += dic[rest][-1] - dic[rest][j]
    
    if j + (j-i) < N and S[j + (j-i)] == rest:
      ans -= 1
    
print(ans)    

### FIXED CODE ###

N=int(input())
S=input()

# 各文字がどこまでにいくつ含まれるかを累積和で管理
R=[0]*N
G=[0]*N
B=[0]*N

dic={}
for c in ("R","G","B"):
  dic[c] = [0] * N

rp=0
gp=0
bp=0
for i in range(len(S)):
  if S[i]=="R":
    rp+=1
    dic["R"][i]=rp
  elif S[i]=="G":
    gp+=1
    dic["G"][i]=gp
  elif S[i]=="B":
    bp+=1
    dic["B"][i]=bp

def getRest(s1,s2):
  for c in ["R","G","B"]:
    if c not in (s1,s2):
      return c

ans = 0
for i in range(N-2):
  for j in range(i+1,N-1):
    # 最初の2つの文字を固定する
    if S[i] == S[j]:
      continue
    # 探すべき文字を特定
    rest = getRest(S[i],S[j])
    ans += dic[rest][-1] - dic[rest][j]
    
    if j + (j-i) < N and S[j + (j-i)] == rest:
      ans -= 1
    
print(ans)
### BUGGY CODE ###

import sys
read = sys.stdin.buffer.read

sys.setrecursionlimit(100002)
def myfact(n):
    if n < 2:
        return 1
    else:
        return n * myfact(n - 1)
n,*X=map(int,read().split())
mod = 10**9+7
last=X[-1]
X=[last-i for i in X[:-1]]

f=myfact(n-1)
flst=[f//i%mod for i in range(1,n)]
s = sum(i*j%mod for i,j in zip(X,flst))
print(s%mod)

### FIXED CODE ###

# https://atcoder.jp/contests/dwacon6th-prelims/submissions/9426910


def solve():
    MOD = 10**9 + 7

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

    ds = []
    for i in range(N-1):
        d = xs[i+1] - xs[i]
        ds.append(d)

    nums = [1, 1]
    num = 1
    for i in range(2, N-1):
        num *= i
        num %= MOD
        nums.append(num)

    ms = [N-1]
    m = N-1
    for i in reversed(range(2, N-1)):
        m *= i
        m %= MOD
        ms.append(m)
    ms.reverse()
    ms.append(1)

    ks = []
    k = 0
    for num, m in zip(nums, ms):
        k += num*m % MOD
        k %= MOD
        ks.append(k)

    ans = 0
    for d, k in zip(ds, ks):
        ans += d*k % MOD
        ans %= MOD

    print(ans)

solve()
### BUGGY CODE ###

#D問題再チャレンジ
#Union Find

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size = [1] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        #print([x,y])
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] = self.size[x] + self.size[y]
            self.size[x] = 0
        else:
            self.par[y] = x
            if x != y:
                self.size[x] = self.size[x] + self.size[y]
                self.size[y] = 0
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

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

uf = UnionFind(n)

con = [0] * (m+1)#convenience
x = [0] * (m+1)
y = [0] * (m+1)

con[m] = 0

for i in range(1,m+1):
    x[i], y[i] = map(int, input().split())
    #print([x,y])
    
for j in range(m-1,0,-1):
    #print([x[j+1],y[j+1]])
    uf.union(x[j+1],y[j+1])
    
    for k in range(1,n+1):
        s = uf.size[k]
        con[j] = con[j] + int(s*(s-1) / 2)
    
    #print(uf.size)

N = int(n * (n-1) / 2)

for i in range(1,m+1):
    print(N-con[i])

### FIXED CODE ###

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size = [1] * (n+1)
        self.con = [0] * (m+1)#convenience

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y, j):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            self.con[j] = self.con[j+1] + self.size[x] * self.size[y]
        else:
            self.con[j] = self.con[j+1]
        #print([x,y])
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] = self.size[x] + self.size[y]
            self.size[x] = 0
        else:
            self.par[y] = x
            if x != y:
                self.size[x] = self.size[x] + self.size[y]
                self.size[y] = 0
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

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

uf = UnionFind(n)

x = [0] * (m+1)
y = [0] * (m+1)


for i in range(1,m+1):
    x[i], y[i] = map(int, input().split())
    #print([x,y])
    
for j in range(m-1,0,-1):
    #print([x[j+1],y[j+1]])
    uf.union(x[j+1],y[j+1],j)
    
    #print(uf.size)
    #print(uf.con)

N = int(n * (n-1) / 2)

for i in range(1,m+1):
    print(N-uf.con[i])
### BUGGY CODE ###

import sys
sys.setrecursionlimit(1000000)
N,Q = list(map(int,input().split()))
ab = [list(map(int,input().split())) for i in range(N-1)]
px = [list(map(int,input().split())) for i in range(Q)]
counter = []
ab.append([0,1])

px_shin =[0 for i in range(N)]
temp=0
def dfs(p,x):
    for i in range(N):
        if ab[i][0] == p:
            for j in range(Q):##
                if px[j][0] == ab[i][1]:
                    px_shin[ab[i][1]-1] += px[j][1]
            px_shin[ab[i][1]-1]+=x
            dfs(ab[i][1], px_shin[ab[i][1]-1])
        
dfs(0,0)
print(' '.join(map(str, px_shin)))



### FIXED CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)

N,Q = map(int,input().split())
to = [[] for _ in range(N+1)]
ans = [0 for _ in range(N+1)]

for _ in range(N-1):
    a,b=map(int,input().split())
    to[a].append(b)
    to[b].append(a)
for _ in range(Q):
    p,x = map(int,input().split())
    ans[p] += x

def dfs(v, p):
    for u in to[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)
dfs(1, -1)
print(*ans[1:])
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
data=[]
cnt=0
for i in range(n):
  if a.count(a[i])>=4:
    data.append(a[i])
    cnt+=1
    if cnt==2:
      break
    data.append(a[i])
    break
  elif 2<=a.count(a[i])<4:
    if a[i] not in data:
      data.append(a[i])
      cnt+=1
    if cnt==2:
      break
if len(data)==0 or len(data)==1:
  print(0)
else:
  print(data[0]*data[1])


### FIXED CODE ###

from collections import Counter
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
A=Counter(a)
s=set(a)
S=sorted(list(s))
S.reverse()
h=0
w=0
for i in S:
  res=A[i]
  if res>=4:
    if h==0:
      h=i
    if w==0:
      w=i
  elif 2<=res<4:
    if w==0:
      w=i
    elif w!=0 and h==0:
      h=i
print(h*w)
### BUGGY CODE ###

import math
x = int(input())
ans = 0
for i in range(1, x + 1):
    for j in range(1, x + 1):
        for k in range(1, x + 1):
            ans += math.gcd(math.gcd(i, j), k)
print(ans)

### FIXED CODE ###

import math
x = int(input())
ans = 0
for i in range(1, x + 1):
    for j in range(1, x + 1):
        for k in range(1, x + 1):
            ans += math.gcd(math.gcd(i, j), k)
print(ans)
### BUGGY CODE ###

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

MOD = 10 ** 9 + 7


def gcd(a, b):
    while a and b:
        a, b = b, a % b
    return a if a > 0 else b


def lcm(a, b):
    return a * b // gcd(a, b)


l = A[0]
for i in range(1, N):
    l = lcm(l, A[i])

res = 0
for i, a in enumerate(A):
    res = (res + l // A[i]) % MOD

print(res)


### FIXED CODE ###

from fractions import gcd
import sys

input = sys.stdin.readline

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

MOD = 10 ** 9 + 7


def lcm(a, b):
    return a * b // gcd(a, b)


l = A[0]
for a in A:
    l = lcm(l, a)

res = 0
for a in A:
    res = (res + (l * pow(a, MOD - 2, MOD)) % MOD) % MOD
    # res = (res + l // a) % MOD

print(res)
### BUGGY CODE ###

def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
e=[[] for i in range(3*10**5+10)]

[n,m]=i2()
for i in range(m):
 [u,v]=i2()
 u-=1
 v-=1
 e[3*u].append(3*v+1)
 e[3*u+1].append(3*v+2)
 e[3*u+2].append(3*v)
 
[s,t]=i2()
s-=1
t-=1
d=[-1 for i in range(3*10**5+10)]
def bfs(v):
   d[v]=0
   q=[v]
   while len(q):
    v=q.pop(0)
    for i in e[v]:
      if d[i]<0:
         d[i]=d[v]+1
         q.append(i)
   if d[3*t]>-1:
     return d[3*t]//3
   return d[3*t]  
print(bfs(3*s))

### FIXED CODE ###

def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
e=[[] for i in range(3*10**5+10)]

[n,m]=i2()
for i in range(m):
 [u,v]=i2()
 u-=1
 v-=1
 e[3*u].append(3*v+1)
 e[3*u+1].append(3*v+2)
 e[3*u+2].append(3*v)
 
[s,t]=i2()
s-=1
t-=1
from collections import deque
d=[-1 for i in range(3*10**5+10)]
def bfs(v):
   d[v]=0
   q=deque([v])
   while len(q):
    v=q.popleft()
    for i in e[v]:
      if d[i]<0:
         d[i]=d[v]+1
         q.append(i)
   if d[3*t]>-1:
     return d[3*t]//3
   return d[3*t]  
print(bfs(3*s))
### BUGGY CODE ###

x, y, z, k = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
c = list(map(int, input().split(' ')))
ab = []
for i in range(len(a)):
    for j in range(len(b)):
        ab.append(a[i]+b[j])
ab.sort(reverse = True)
c.sort(reverse = True)
abc = []
k_1 = min([len(ab), k])
for i in range(k_1):
    for j in range(len(c)):
        abc.append(ab[i]+c[j])
abc.sort(reverse = True)
for i in range(k):
    print(abc[i])

### FIXED CODE ###

import heapq
import sys
x, y, z, k = map(int, input().split())

a, b, c = (sorted(map(int, sys.stdin.readline().split()), reverse=True) for _ in range(3))

abc = {(0,0,0)}

stu = [[-a[0]-b[0]-c[0], 0,0,0]]
heapq.heapify(stu)
result = []
while k > 0:
    out_p, n, m, l = heapq.heappop(stu)
    if n+1<x and not((n+1,m,l) in abc):
        abc.add((n+1,m,l))
        heapq.heappush(stu, [-a[n+1]-b[m]-c[l], n+1, m, l])
    if m+1<y and not((n,m+1,l) in abc):
        abc.add((n,m+1,l))
        heapq.heappush(stu, [-a[n]-b[m+1]-c[l], n, m+1, l])
    if l+1<z and not((n,m,l+1) in abc):
        abc.add((n,m,l+1))
        heapq.heappush(stu, [-a[n]-b[m]-c[l+1], n, m, l+1])
    result.append(-out_p)
    k -= 1
result.sort(reverse=True)
for i in result:
    print(i)
### BUGGY CODE ###

n,x,m = map(int,input().split())
#listを2つ使って初項からループ手前までのセット、そして二回目のループのセット。
k_1 = list()
k_2 = list()
#len(k_2)でも良かった
#loop_sumはk_2によって表されるループの和
loop_number = 0
loop_sum = 0
while x not in k_1:
	k_1.append(x)
	x = x*x%m
while x not in k_2:
	k_2.append(x)
	loop_number+=1
	loop_sum+=x 
	x = x*x%m
ans = 0
for i in range(len(k_1)):
	ans +=  k_1[i]
ans = ans + loop_sum * ((n-len(k_1))//loop_number)
for i in range((n-len(k_1))%loop_number):
	ans += k_2[i]
print(ans)

### FIXED CODE ###

n,x,m = map(int,input().split())
k_1 = set()
k_2 = set()
tmp = list()
loop_number = 0
loop_sum = 0
while x not in k_1:
	k_1.add(x)
	x = x*x%m
while x not in k_2:
	k_2.add(x)
	tmp.append(x)
	loop_number+=1
	loop_sum+=x 
	x = x*x%m
ans = 0
k_1 = list(k_1)
for i in range(len(k_1)):
	ans +=  k_1[i]
ans = ans + loop_sum * ((n-len(k_1))//loop_number)
for i in range((n-len(k_1))%loop_number):
	ans += tmp[i]
print(ans)