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

def examA():
    S = SI(); N = len(S)
    prev = S[0]; cur = ""
    k = 0; ans = 1
    while(k<N-1):
        k +=1
        cur += S[k]
        if cur==prev:
            continue
        prev = cur
        cur = ""
        ans +=1
    print(ans)
    return

def examB():
    N = I()
    S = SI()
    return

def examC():
    N = I()
    A = LI()
    B = LI()
    K = 0
    ans = -1
    que = []
    for i in range(N):
        que.append((-B[i],i))
    heapify(que)
    flag = True
    while(que):
#        print(que)
        b,i = heappop(que)
        b *=(-1)
        if b<=A[i]:
            if b!=A[i]:
                flag = False
                break
            continue
        b -= (B[i-1]+B[i+1-N])
        B[i] = b
        heappush(que,(-b,i))
        K +=1
#    print(que)
#    print(K)
    if flag:
        ans = K
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18

if __name__ == '__main__':
    examC()


### FIXED CODE ###

def examA():
    S = SI(); N = len(S)
    prev = S[0]; cur = ""
    k = 0; ans = 1
    while(k<N-1):
        k +=1
        cur += S[k]
        if cur==prev:
            continue
        prev = cur
        cur = ""
        ans +=1
    print(ans)
    return

def examB():
    N = I()
    S = SI()
    return

def examC():
    N = I()
    A = LI()
    B = LI()
    ans = 0
    que = []
    for i in range(N):
        heappush(que,(-B[i],i))
    flag = True
    while(que):
        b,i = heappop(que)
        b *=(-1)
        if b == A[i]:
            continue
        a, c = B[(i - 1) % N], B[(i + 1) % N]
        if b - A[i] < a + c:
            flag = False
            break
        r = (b - A[i]) // (a + c)
        b -= (a + c) * r
        B[i] = b
        ans += r
        if b != A[i]:
            heapq.heappush(que, (-b, i))
#    print(que)
#    print(K)
    if flag:
        print(ans)
    else:
        print(-1)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18

if __name__ == '__main__':
    examC()
### BUGGY CODE ###

from fractions import Fraction
MOD=10**9+7
def comb(n, k):
    MOD=10**9+7
    if n < 0 or k < 0:
        pdt = 0
    else:
        pdt = 1
        for s in range(1, k + 1):
            pdt *= Fraction(n - s + 1, s)%MOD
    return pdt.numerator%MOD

N,K=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
import numpy as np
pos=[0]*N
neg=[0]*N
for i in range(N):
  if N-1-i>=K-1:
    neg[i]=comb(N-1-i,K-1)
  if i>=K-1:
    pos[i]=comb(i,K-1)
pos=np.array(pos)
neg=np.array(neg)
keisu=pos-neg
#print(keisu)
ans=0
for i in range(N):
  ans+=(keisu[i]*A[i])%MOD
print(ans%MOD)

### FIXED CODE ###

import numpy as np
mod=10**9+7
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
A=np.array(A)

def cmb(n, k, mod, fac, ifac): #calc nCk under mod
    k = min(k, n-k)
    return fac[n] * ifac[k] * ifac[n-k] % mod

def make_tables(mod, n): #Make factorial and inverse tables under mod 
    fac = [1, 1] # factorial table
    ifac = [1, 1] #factorial of inverse table
    inverse = [0, 1] #inverse
    for i in range(2, n+1):
        fac.append((fac[-1] * i) % mod)
        inverse.append((-inverse[mod % i] * (mod//i)) % mod)
        ifac.append((ifac[-1] * inverse[-1]) % mod)
    return fac, ifac
  
fac,ifac = make_tables(mod,N+1)
MAX=0
MIN=0

for i,a in enumerate(A):
  if i>=K-1:
    MAX+=cmb(i, K-1, mod, fac, ifac)*a
    MAX%=mod
  if N-i-1>=K-1:
    MIN+=cmb(N-i-1, K-1, mod, fac, ifac)*a
    MIN%=mod
print((MAX-MIN)%mod)
### BUGGY CODE ###

N = int(input())
P = list(map(int,input().split()))
ans = 0
for i in range(N):
  if P[i] == min(P[:i+1]):
    ans += 1
print(ans)

### FIXED CODE ###

N = int(input())
P = list(map(int,input().split()))
m = N + 1
ans = 0
for i in P:
  if i <= m:
    ans += 1
    m = i
  else:
    continue
print(ans)
### BUGGY CODE ###

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

### FIXED CODE ###

n=int(input())
power=1
for i in range(1,n+1):
    power*=i
    if power>10**9+7:
        power=power%(10**9+7)
print(power%(10**9+7))
### BUGGY CODE ###

r1,c1,r2,c2=map(int,input().split())
mod=10**9+7
f=[1]
for i in range(2*10**6+7):f+=[f[-1]*(i+1)%mod]
ans=0
for i in range(c1,c2+1):
  d=(r2+1)*f[i+r2+1]*f[r1]
  d%=mod
  d=(d+(mod-(r1*f[r2+1]*f[i+r1])%mod))%mod
  d*=pow((i+1)*f[i]*f[r1]*f[r2+1],mod-2,mod)
  d%=mod
  ans+=d
  ans%=mod
print(ans)

### FIXED CODE ###

r1,c1,r2,c2=map(int,input().split())
mod=10**9+7
f=[1]
for i in range(2*10**6+7):f+=[f[-1]*(i+1)%mod]
ans=0
for i in range(c1,c2+1):
  d=((r2+1)*f[i+r2+1]%mod)*f[r1]
  d%=mod
  d=(d+(mod-((r1*f[r2+1]%mod)*f[i+r1])%mod))%mod
  d*=pow((((i+1)*f[i]%mod)*f[r1]%mod)*f[r2+1]%mod,mod-2,mod)
  d%=mod
  ans+=d
  ans%=mod
print(ans)
### BUGGY CODE ###

n=int(input())
lis=[]
for i in range(n):
    a=input()
    if a not in lis:
        lis.append(a)

print(len(lis))


### FIXED CODE ###

n=int(input())
lis=[]
for i in range(n):
    a=input()
    lis.append(a)

m=0
lis.sort()
for i in range(n-1):
    if lis[i]==lis[i+1]:
        m+=1

print(int(n-m))
### BUGGY CODE ###

n,m=map(int,input().split())
A=list(map(int,input().split()))
for k in range(m):
    A[A.index(max(A))]//=2

print(sum(A))



### FIXED CODE ###

import heapq
n,m=map(int,input().split())
A=list(map(lambda x:int(x)*-1,input().split()))
heapq.heapify(A)
for k in range(m):
    a=heapq.heappop(A)
    heapq.heappush(A, -1*(-a//2))
print((sum(A))*-1)
### BUGGY CODE ###

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

a = sorted(a)
b = sorted(b)
c = sorted(c)

ans = 0

bl = [0] * len(b)

p = 0
for i,x in enumerate(b):
    for j in range(p,n):
        if x >= c[j]:
            pass
        else:
            bl[i] = n - j
            p = j
            break

p = 0
for x in a:
    for i in range(p,n):
        if x >= b[i]:
            pass
        else:
            ans += sum(bl[i:])
            p = i
            break

print(ans)

### FIXED CODE ###

import bisect

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

a = sorted(a)
b = sorted(b)
c = sorted(c)

ans = 0

for x in b:
    aa = bisect.bisect_left(a,x)
    cc = len(c) - bisect.bisect_right(c,x)
    ans += aa * cc

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

import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)
from numba import njit,i8


@njit(i8(i8,i8,i8[:,:],i8,i8[:],i8,i8,i8,i8))
def give_cnt(N,K,LR,mod,dp,L,R,l,r):
    for i in range(N):
        for k in range(K):
            l = LR[k][0]
            r = LR[k][1]
            if i + l >= N:
                continue
            else:
                L = i + l
            if i + r >= N:
                R = N - 1
            else:
                R = i + r
            dp[L:R+1] += dp[i]
            dp[L:R+1] %= mod
    return dp[-1]


def main():
    N,K = map(int,input().split())
    LR = [list(map(int,input().split())) for i in range(K)]
    LR = np.array(LR)

    mod = 998244353

    dp = [0 for i in range(N)]
    dp[0] = 1
    dp = np.array(dp)

    res = give_cnt(N,K,LR,mod,dp,0,0,0,0)

    # print(dp)
    print(res)



main()


### FIXED CODE ###

import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)
from numba import njit,i8


@njit(i8(i8,i8,i8,i8[:,:],i8[:],i8,i8))
def give_dp(N,K,mod,LR,dp,l,r):
    for i in range(N):
        if i > 0:
            dp[i] += dp[i-1]
            dp[i] %= mod
        for k in range(K):
            l = LR[k][0]
            r = LR[k][1]
            if i + l < N:
                dp[i+l] += dp[i]
                dp[i+1] %= mod
            if i + r < N:
                dp[i+r+1] -= dp[i]
                dp[i+1] %= mod
    return dp[-1]


def main():
    N,K = map(int,input().split())
    LR = [list(map(int,input().split())) for i in range(K)]
    LR = np.array(LR)

    mod = 998244353

    dp = [0 for i in range(N)]
    dp[0] = 1
    dp[1] = -1
    dp = np.array(dp)

    res = give_dp(N,K,mod,LR,dp,0,0)
    res %= mod

    print(res)



main()
### BUGGY CODE ###

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

dp = [[-1]*n for i in range(5000)]
for i in range(1000//a[0]+1):
    dp[i][0] = 1000-a[0]*i

for i in range(1, n):
    for g in range(5000):
        for l in range(5000):
            if dp[l][i-1] >= 0:
                if l < g:
                    dp[g][i] = max(dp[l][i-1] - a[i]*(g-l), dp[g][i])
                else:
                    dp[g][i] = max(dp[l][i-1] + a[i]*(l-g), dp[g][i])
            else:
                break

ans = 0
for m in range(5000):
    ans = max(ans, dp[m][n-1])
print(ans)


### FIXED CODE ###

n = int(input())
a = list(map(int, input().split())) + [-1]

money = 1000
k = 0

for i in range(n):
    if a[i+1] > a[i]:
        k += money//a[i]
        money = money - a[i]*(money//a[i])
    elif a[i+1] <= a[i]:
        money += k*a[i]
        k = 0

print(money)
### BUGGY CODE ###

from collections import deque
H,W=map(int,input().split())
S=[list(input()) for _ in range(H)]
A=[[0]*W for _ in range(H)]
ans=[]

for i in range(H):
    for j in range(W):
        sy=i
        sx=j
        if S[i][j]=='#':
            continue
        A[i][j]=1
        route=deque([(sy,sx,0)])
        while route:
            B=[]
            a,b,n=route.popleft()
            if 0<=a<=H-1 and 0<=b<=W-1:
                if S[a][b]=='.':
                    S[a][b]=='#'
                    B.append(n)
                    route.append((a+1,b,n+1))
                    route.append((a-1,b,n+1))
                    route.append((a,b+1,n+1))
                    route.append((a,b-1,n+1))
        ans.append(max(B))

print(max(ans))
                   

### FIXED CODE ###

from collections import deque
import copy
H,W=map(int,input().split())
S=[list(input()) for _ in range(H)]
S1=copy.deepcopy(S)
ans=[]

sy=0
sx=0
for i in range(H):
    for j in range(W):
        c=0
        route=deque([(i,j,0)])
        S=copy.deepcopy(S1)
        while route:
            a,b,n=route.popleft()
            c=n
            if 0<=a<=H-1 and 0<=b<=W-1:
                if S[a][b]=='.':
                    S[a][b]='#'
                    route.append((a+1,b,n+1))
                    route.append((a-1,b,n+1))
                    route.append((a,b+1,n+1))
                    route.append((a,b-1,n+1))
                    
        ans.append(c-1)

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

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

b = []
c = []
for i in range(q):
	B, C = map(int, input().split())
	b.append(B)
	c.append(C)

a1 = []
a2 = []

for i in a:
	if i in a1:
		a2[a1.index(i)]+=1
	else:
		if i in b:
			a1.insert(0,i)
			a2.insert(0,1)
		else:
			a1.append(i)
			a2.append(1)

ans = []
s = 0

for i in range(len(a1)):
	s += a1[i]*a2[i]

ans.append(s)

for i in range(q):
	if b[i] in a1:
		bi = a1.index(b[i])
		if c[i] in a1:
			a2[a1.index(c[i])]+=a2[bi]
			ans.append(ans[-1]+a2[bi]*(c[i]-b[i]))
			a2[bi] = 0
		else:
			a1.append(c[i])
			a2.append(a2[bi])
			ans.append(ans[-1]+a2[bi]*(c[i]-b[i]))
			a2[bi] = 0
	else:
		ans.append(ans[-1])

for i in range(len(ans)-1):
	print(ans[i+1])

### FIXED CODE ###

from collections import Counter

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

cnt = Counter(a)

s = sum(a)

for i in range(q):
	b, c = map(int, input().split())
	s = s - b*cnt[b]
	s = s + c*cnt[b]
	cnt[c] += cnt[b]
	cnt[b] = 0
	print(s)
### BUGGY CODE ###

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

### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
import collections
c=collections.Counter(A)
for i in range(1,N+1):
  print(c[i])
### BUGGY CODE ###

import sys
import numpy as np
def input(): return sys.stdin.readline().rstrip()


def main():
    h, w = map(int, input().split())
    A = np.array([list(map(int, input().split()))
                  for i in range(h)], dtype=int)
    B = np.array([list(map(int, input().split()))
                  for i in range(h)], dtype=int)
    abss = np.abs(A-B)
    dp = np.zeros((h, w, 800), dtype=int)
    kmax = 800
    dp[0][0][abss[0][0]] = 1
    for i in range(h):
        for j in range(w):
            for k in range(kmax):
                if dp[i][j][k] == 1:
                    if i != h-1:
                        dp[i+1][j][abs(k-abss[i+1][j])] = 1
                        if k+abss[i+1][j] < kmax:
                            dp[i+1][j][k+abss[i+1][j]] = 1
                    if j != w-1:
                        dp[i][j+1][abs(k-abss[i][j+1])] = 1
                        if k+abss[i][j+1] < kmax:
                            dp[i][j+1][k+abss[i][j+1]] = 1
    for k in range(kmax):
        if dp[-1][-1][k] == 1:
            print(k)
            break


if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
def input(): return sys.stdin.readline().rstrip()


def main():
    h, w = map(int, input().split())
    A = [list(map(int, input().split()))for i in range(h)]
    B = [list(map(int, input().split()))for i in range(h)]
    abss = [[abs(A[i][j]-B[i][j])for j in range(w)] for i in range(h)]
    kmax = 6400
    dp = [[[0]*kmax for j in range(w)]for i in range(h)]
    dp[0][0][abss[0][0]] = 1
    for i in range(h):
        for j in range(w):
            for k in range(kmax):
                if dp[i][j][k] == 1:
                    if i != h-1:
                        dp[i+1][j][abs(k-abss[i+1][j])] = 1
                        if k+abss[i+1][j] < kmax:
                            dp[i+1][j][k+abss[i+1][j]] = 1
                    if j != w-1:
                        dp[i][j+1][abs(k-abss[i][j+1])] = 1
                        if k+abss[i][j+1] < kmax:
                            dp[i][j+1][k+abss[i][j+1]] = 1
    for k in range(kmax):
        if dp[-1][-1][k] == 1:
            print(k)
            break


if __name__ == '__main__':
    main()
### BUGGY CODE ###

S = input()
Q = int(input())
cnt = 0
for i in range(Q):
    query = list(input().split())
    if query[0] == '1':
        cnt += 1
    elif query[1] == '1':
        if cnt % 2 == 0:
            S = query[2] + S
        else:
            S += query[2]
    else:
        if cnt % 2 == 0:
            S += query[2]
        else:
            S = query[2] + S
if cnt % 2 != 0:
    S = S[::-1]
print(S)


### FIXED CODE ###

from collections import deque
S = deque(list(input()))
Q = int(input())
cnt = 0
for i in range(Q):
    query = list(input().split())
    if query[0] == '1':
        cnt += 1
    elif query[1] == '1':
        if cnt % 2 == 0:
            S.appendleft(query[2])
        else:
            S.append(query[2])
    else:
        if cnt % 2 == 0:
            S.append(query[2])
        else:
            S.appendleft(query[2])
if cnt % 2 != 0:
    S.reverse()
print(''.join(S))
### BUGGY CODE ###

N = int(input())
max_T = 1
T = []
for _ in range(N):
    t = int(input())
    T.append(t)
    max_T = max(max_T, t)
time = max_T
while time <= 10**18:
    flag = True
    for t in T:
        if time % t != 0:
            flag = False
            break
    if flag == True:
        print(time)
        break
    else:
        time += max_T

### FIXED CODE ###

def min_mult(a, b):
    result = max(a, b)
    while result % min(a, b) != 0:
        result += max(a, b)
    return result
N = int(input())
multiple = 1
for _ in range(N):
    multiple = min_mult(multiple, int(input()))
print(multiple)
### BUGGY CODE ###


import heapq
n = int(input())
stw = []
x_list = []
y_list = []
for i in range(n):
  x, y = map(int, input().split())
  x_list += [[x, i]]
  y_list += [[y, i]]
x_list.sort()
y_list.sort()
for i in range(n-1):
  xs = x_list[i][1]
  xt = x_list[i+1][1]
  ys = y_list[i][1]
  yt = y_list[i+1][1]
  dis_x = abs(x_list[i][0]-x_list[i+1][0])
  dis_y = abs(y_list[i][0]-y_list[i+1][0])
  stw += [[dis_x, xs, xt]]
  stw += [[dis_x, xt, xs]]
  stw += [[dis_y, ys, yt]]
  stw += [[dis_y, yt, ys]]
  
#print(stw)
heapq.heapify(stw)
#stw = sorted(stw, key=lambda x:x[2])

class UnionFind():
  def __init__(self, n):
    self.n = n
    self.root = [-1]*(n+1)
    self.rnk = [0]*(n+1)
  def Find_Root(self, x):
    if(self.root[x] < 0):
      return x
    else:
      self.root[x] = self.Find_Root(self.root[x])
      return self.root[x]
  def Unite(self, x, y):
    x = self.Find_Root(x)
    y = self.Find_Root(y)
    if(x == y):
      return 
    if self.root[x] > self.root[y]:
      x, y = y, x
    self.root[x] += self.root[y]
    self.root[y] = x 
  def isSameGroup(self, x, y):
    return self.Find_Root(x) == self.Find_Root(y)
  def Count(self, x): # xが属するグループのサイズを返す
    return -self.root[self.Find_Root(x)]
  def Members(self, x): # xが属するグループに属する要素をリストで返す
    return [i for i in range(self.n) if self.Find_Root(i)==self.Find_Root(x)]
  def Roots(self): # 全ての根の要素をリストで返す
    return [i for i, x in enumerate(self.root) if x < 0]
  def Group_Count(self): # グループの数を返す
    return len(self.Roots())


def kruskal():
  # stw = [[s0, t0, w0], [s1, t1, w1], ..., [sn, tn, wn]]
  # stw: s: 始点, t: 終点, w: st間の重み
  # stwはwを小さい順にソートされたものとする
  res = 0
  uf = UnionFind(n)
  while stw:
    w, s, t = heapq.heappop(stw)
    if not uf.isSameGroup(s, t):
      uf.Unite(s, t)
      res += w
  return res

print(kruskal())

### FIXED CODE ###

import itertools
import heapq
from heapq import heappush, heappop
global G, inp_x, inp_y

def input_graph(N):
  global G, inp_x, inp_y
  inp_x = []
  inp_y = []
  for i in range(N):
    x, y = map(int, input().split())
    inp_x.append((x, i))
    inp_y.append((y, i))
  inp_x.sort()
  inp_y.sort()
  for i in range(N-1):
    xa = inp_x[i][1]
    xb = inp_x[i+1][1]
    ya = inp_y[i][1]
    yb = inp_y[i+1][1]
    cost_x = abs(inp_x[i][0]-inp_x[i+1][0])
    cost_y = abs(inp_y[i][0]-inp_y[i+1][0])
    G[xa] += [(cost_x, xb)]
    G[xb] += [(cost_x, xa)]
    G[ya] += [(cost_y, yb)]
    G[yb] += [(cost_y, ya)]

  
def prim(V):
  global G, used
  edgelist = []
  for e in G[0]:
    heappush(edgelist, e)
  used[0] = True
  res = 0
  while edgelist:
    minedge = heappop(edgelist)
    if used[minedge[1]]:
      continue 
    v = minedge[1]
    used[v] = True 
    for e in G[v]:
      if not used[e[1]]:
        heappush(edgelist, e)
    res += minedge[0]
  return res

def main():
  global G, used
  N = int(input())
  G = [[] for _ in range(N)]
  used = [False for _ in range(N)]
  input_graph(N)
  print(prim(N))

if __name__ == "__main__":
  main()
### BUGGY CODE ###

n = int(input())
li = list(map(int,input().split()))
icount = 0
for i in range(n):
    if li[i] == min(li[0:i+1]):
        icount += 1
print(icount)

### FIXED CODE ###

n = int(input())
p = list(map(int,input().split()))
count = 1
min_p = p[0]
for i in range(n):
    if min_p > p[i]:
        count += 1
        min_p = p[i]
print(count)
### BUGGY CODE ###

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


def bubble_sort(lst):
    for i in reversed(range(len(lst))):
        for j in range(i):
            if lst[j] > lst[j + 1]:
                lst[j], lst[j + 1] = lst[j + 1], lst[j]


bubble_sort(a)
zero_monster_indexes = []

while True:
    for monster_i in range(1, len(a)):
        if a[monster_i] == 0:
            zero_monster_indexes.append(monster_i)
        else:
            a[monster_i] %= a[0]

            if a[monster_i] == 0:
                zero_monster_indexes.append(monster_i)

    for zero_monster_index in reversed(zero_monster_indexes):
        a.pop(zero_monster_index)
    zero_monster_indexes.clear()

    bubble_sort(a)

    if len(a) == 1:
        break

print(a[0])


### FIXED CODE ###

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

a = sorted(a)
zero_monster_indexes = []

while True:
    for monster_i in range(1, len(a)):
        if a[monster_i] == 0:
            zero_monster_indexes.append(monster_i)
        else:
            a[monster_i] %= a[0]

            if a[monster_i] == 0:
                zero_monster_indexes.append(monster_i)

    for zero_monster_index in reversed(zero_monster_indexes):
        a.pop(zero_monster_index)
    zero_monster_indexes.clear()

    a = sorted(a)
    if len(a) == 1:
        break

print(a[0])
### BUGGY CODE ###

import math
K = int(input())
t = 0
for i in range(1,K+1):
    for j in range(1,K+1):
        for l in range(1,K+1):
            t1= math.gcd(i,j)
            t2 = math.gcd(t1,l)
            t = t + t2
print(t)

### FIXED CODE ###

import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)
K = int(input())
t = 0
t1 = 0
t2 = 0
for i in range(1,K+1):
    for j in range(i,K+1):
        for l in range(j,K+1):
            if i == j == l:
                t = t + i
            elif i == j or j ==l or l ==i:
                t = t + 3 * gcd(i, j,l)
            else:
                t = t + 6 * gcd(i,j,l)
             
           
           
print(t)
### BUGGY CODE ###

N,M=map(int,input().split())
A=list(map(int,input().split()))
num=[0,2,5,5,4,5,6,3,7,6]
dp=[[1,0,0,0,0,0,0,0,0,0]]
tmp=0
tmp1=0
tmp2=0
for i in range(0,N+1):
    dp.append([0,0,0,0,0,0,0,0,0,0])
    tmp=0
    for j in A:
        if(i-num[j]>=0):
            tmp1=dp[i-num[j]].copy()
            #存在しないなら不可
            if(tmp1[0]==0):
                continue
            tmp1[j]+=1
            tmp2=0
            tmp2=int("9"*tmp1[9]+"8"*tmp1[8]+"7"*tmp1[7]+"6"*tmp1[6]+"5"*tmp1[5]+"4"*tmp1[4]+"3"*tmp1[3]+"2"*tmp1[2]+"1"*tmp1[1])
            if(tmp2>tmp):
                tmp=tmp2
                dp[i]=tmp1
ans=0
for k in range(1,10):
    if(dp[N][10-k]!=0):
        ans*=10**dp[N][10-k]
        ans+=(10-k)*int("1"*dp[N][10-k])
print(int(ans))

### FIXED CODE ###

N,M=map(int,input().split())
A=list(map(int,input().split()))
if(1 in A):
    if(4 in A):
        A.remove(4)
    if(9 in A):
        A.remove(9)
    if(6 in A):
        A.remove(6)
if(5 in A):
    if(3 in A):
        A.remove(3)
    if(2 in A):
        A.remove(2)
if(3 in A):
    if(2 in A):
        A.remove(2)
if(9 in A):
    if(6 in A):
        A.remove(6)
if(7 in A):
    if(9 in A):
        A.remove(9)
    if(6 in A):
        A.remove(6)
num=[0,2,5,5,4,5,6,3,7,6]
dp=[0]
keta=[0]
tmp=0
tmp1=0
tmp_keta=-1
tmp1_keta=-1
for i in range(1,N+1):
    tmp=-1
    tmp_keta=-1
    for j in A:
        tmp1=-1
        tmp1_keta=-1
        if(i-num[j]>=0):
            if(dp[i-num[j]]>0):
                tmp1=dp[i-num[j]]+j*(10**keta[i-num[j]])
                tmp1_keta=keta[i-num[j]]+1
            elif(dp[i-num[j]]==0):
                tmp1=j
                tmp1_keta=1

            if(tmp1>tmp):
                tmp=tmp1
                tmp_keta=tmp1_keta
                
    dp.append(tmp)
    keta.append(tmp_keta)
print(dp[N])