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

def search(N, M, adj):
    reachable = [[set() for i in range(N)] for _ in range(N)]
    reachable[0] = adj
    start = -1
    for m in range(1, N):
        prev = reachable[m-1]
        cur = reachable[m]
        for i in range(N):
            cur[i] = set(prev[i])
        for i in range(N):
            for nxt in adj[i]:
                if i in prev[nxt]:
                    return (i, reachable, m+1)
                for p in prev[nxt]:
                    cur[i].add(p)
    return None


def main():
    N, M = map(int, input().split())
    adj = [set() for _ in range(N)]
    for _ in range(M):
        A, B = map(int, input().split())
        adj[A-1].add(B-1)
    result = search(N, M, adj)
    if not result:
        print(-1)
        return
    start, reachable, cnt = result
    ans = [-1] * cnt
    x = start
    for i in range(cnt):
        ans[i] = x+1
        for nxt in adj[x]:
            if start in reachable[cnt-i-1][nxt]:
                x = nxt
                break
    print(len(ans))
    for a in ans:
        print(a)


if __name__ == "__main__":
    main()


### FIXED CODE ###

def search(N, M, adj):
    reachable = [[set() for i in range(N)] for _ in range(N)]
    reachable[0] = adj
    start = -1
    for m in range(1, N):
        prev = reachable[m-1]
        cur = reachable[m]
        #        for i in range(N):
        #            cur[i] = set(prev[i])
        for i in range(N):
            for nxt in adj[i]:
                for p in prev[nxt]:
                    cur[i].add(p)
                if i in prev[nxt]:
                    return (i, reachable, m+1)
    return None


def main():
    N, M = map(int, input().split())
    adj = [set() for _ in range(N)]
    for _ in range(M):
        A, B = map(int, input().split())
        adj[A-1].add(B-1)
    result = search(N, M, adj)
    if not result:
        print(-1)
        return
    start, reachable, cnt = result
    ans = [-1] * cnt
    x = start
    for i in range(cnt):
        ans[i] = x+1
        for nxt in adj[x]:
            if start in reachable[cnt-i-2][nxt]:
                x = nxt
                break
    print(len(ans))
    for a in ans:
        print(a)


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

# ABC129 E

L=input()[::-1]
A=[0]*(len(L)+1)
B=[0]*(len(L)+1)
p=10**9+7

A[0],B[0]=1,2
res=0
tmp=1
for i in reversed(range(len(L))):
    if L[i]=='1':
        res=(res+tmp*3**i)%p
        tmp=(2*tmp)%p
print((res+tmp)%p) 

### FIXED CODE ###

# ABC129 E

L=input()
N=len(L)
p=10**9+7

def pow_k(x,n,p=10**9+7):
    if n==0:
        return 1
    K=1
    while n>1:
        if n%2!=0:
            K=(K*x)%p
        x=(x*x)%p
        n//=2
    return (K*x)%p


res=0
tmp=1
for i in reversed(range(N)):
    if L[N-1-i]=='1':
        res=(res+tmp*pow_k(3,i,p))%p
        tmp=(2*tmp)%p
print((res+tmp)%p)
### BUGGY CODE ###

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

P = [None] + P
C = [None] + C

all_max = C[1]
for st in range(1, N + 1):
    scores = []
    visit = set()
    p = st

    while p not in visit and len(visit) < K:
        next_p = P[p]
        scores.append(C[next_p])
        visit.add(p)
        p = next_p

    num_elem = len(scores)
    all_sum = sum(scores)

    q, r = divmod(K, num_elem)

    max_ = scores[0]
    temp = scores[0]

    max_r = scores[0]
    temp_r = scores[0]
    for x in range(1, num_elem):
        if x < r:
            temp_r += scores[x]
            max_r = max(max_r, temp_r)

        temp += scores[x]
        max_ = max(max_, temp)

    temp_max = scores[0]
    if all_sum > 0 and q > 0:
        if r > 0:
            temp_max = max(all_sum * (q - 1) + max_, all_sum * q + max_r)
        else:
            temp_max = all_sum * (q - 1) + max_
    else:
        temp_max = max_

    all_max = max(all_max, temp_max)

print(all_max)


### FIXED CODE ###

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

P = [None] + P
C = [None] + C

all_max = C[1]
for st in range(1, N + 1):
    scores = []
    visit = set()
    p = st

    while p not in visit and len(visit) < K:
        next_p = P[p]
        scores.append(C[next_p])
        visit.add(p)
        p = next_p

    num_elem = len(scores)
    all_sum = sum(scores)

    q, r = divmod(K, num_elem)

    max_ = scores[0]
    temp = scores[0]

    max_r = scores[0]
    temp_r = scores[0]
    for x in range(1, num_elem):
        if x < r:
            temp_r += scores[x]
            max_r = max(max_r, temp_r)

        temp += scores[x]
        max_ = max(max_, temp)

    temp_max = scores[0]
    if all_sum > 0 and q > 0:
        if r > 0:
            temp_max = max(all_sum * (q - 1) + max_, all_sum * q + max_r)
        else:
            temp_max = all_sum * (q - 1) + max_
    else:
        temp_max = max_

    all_max = max(all_max, temp_max)


print(all_max)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
def main():
    s = input()
    t = input()
    len_s = len(s)
    len_t = len(t)
    dp = [[0] * len_t for i in range(len_s)]

    for j in range(len_t):
        if s[0] == t[j]:
            for k in range(j, len_t):
                dp[0][k] = 1
            break

    for i in range(len_s):
        if s[i] == t[0]:
            for k in range(i, len_s):
                dp[k][0] = 1
            break
    
    for i in range(1, len_s):
        for j in range(1, len_t):
            if s[i] == t[j]:
                dp[i][j]  = dp[i-1][j-1] + 1
            else:
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
    length = dp[-1][-1]
    i = len_s - 1
    j = len_t - 1
    ans = ''
    while (length > 0):
        if s[i] == t[j]:
            ans = s[i] + ans
            i -= 1
            j -= 1
            length -= 1
        elif (j > 0 and dp[i][j - 1] == dp[i][j]) or i == 0:
            j -= 1
        else:
            i -= 1
            
    # for d in dp:
    #     print(d)
    
    print(ans)
if __name__ == "__main__":
    main()

### FIXED CODE ###

# -*- coding: utf-8 -*-
def main():
    s = input()
    t = input()
    len_s = len(s)
    len_t = len(t)
    dp = [[0] * len_t for i in range(len_s)]

    for j in range(len_t):
        if s[0] == t[j]:
            for k in range(j, len_t):
                dp[0][k] = 1
            break

    for i in range(len_s):
        if s[i] == t[0]:
            for k in range(i, len_s):
                dp[k][0] = 1
            break
    
    for i in range(1, len_s):
        for j in range(1, len_t):
            if s[i] == t[j]:
                dp[i][j]  = dp[i-1][j-1] + 1
            else:
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
    length = dp[-1][-1]
    i = len_s - 1
    j = len_t - 1
    ans = ''
    while (length > 0):
        if s[i] == t[j]:
            ans = s[i] + ans
            i -= 1
            j -= 1
            length -= 1
        elif (j > 0 and dp[i][j - 1] == dp[i][j]) or i == 0:
            j -= 1
        else:
            i -= 1
            
    # for d in dp:
    #     print(d)
    
    print(ans)
if __name__ == "__main__":
    main()
### BUGGY CODE ###

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

from copy import deepcopy

h, w = map(int, readline().split())
s = [input() for _ in range(h)]
cnt_x = [[0] * w for _ in range(h)]
cnt_y = deepcopy(cnt_x)
for i in range(h):
    memo = 0
    for j in range(w):
        if s[i][j] == '.':
            memo += 1
            cnt_x[i][j] = memo
        else:
            memo = 0
    for j in range(w - 1, 0, -1):
        if s[i][j - 1] == '.':
            cnt_x[i][j - 1] = max(cnt_x[i][j - 1], cnt_x[i][j])
for i in range(w):
    memo = 0
    for j in range(h):
        if s[j][i] == '.':
            memo += 1
            cnt_y[j][i] = memo
        else:
            memo = 0
    for j in range(h - 1, 0, -1):
        if s[j - 1][i] == '.':
            cnt_y[j - 1][i] = max(cnt_y[j - 1][i], cnt_y[j][i])
ans = 0
for i in range(h):
    for j in range(w):
        ans = max(ans, cnt_x[i][j] + cnt_y[i][j] - 1)
print(ans)


### FIXED CODE ###

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

from copy import deepcopy

h, w = map(int, readline().split())
s = [input() for _ in range(h)]
cnt_x = [[0] * w for _ in range(h)]
cnt_y = deepcopy(cnt_x)
for i in range(h):
    memo = 0
    for j in range(w):
        if s[i][j] == '.':
            memo += 1
            cnt_x[i][j] = memo
        else:
            memo = 0
    for j in range(w - 1, 0, -1):
        if s[i][j - 1] == '.':
            if cnt_x[i][j - 1] < cnt_x[i][j]:
                cnt_x[i][j - 1] = cnt_x[i][j]
for i in range(w):
    memo = 0
    for j in range(h):
        if s[j][i] == '.':
            memo += 1
            cnt_y[j][i] = memo
        else:
            memo = 0
    for j in range(h - 1, 0, -1):
        if s[j - 1][i] == '.':
            cnt_y[j - 1][i] = max(cnt_y[j - 1][i], cnt_y[j][i])
            if cnt_y[j - 1][i] < cnt_y[j][i]:
                cnt_y[j - 1][i] = cnt_y[j][i]
ans = 0
for i in range(h):
    for j in range(w):
        v = cnt_x[i][j] + cnt_y[i][j] - 1
        if ans < v:
            ans = v
print(ans)
### BUGGY CODE ###

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

N, Q = map(int, input().split())
ab = [None] * (N - 1)
for i in range(N - 1):
    ab[i] = list(map(int, input().split()))
ab = sorted(ab, key=lambda x: (x[0], x[1]))

px = [None] * Q
for i in range(Q):
    px[i] = list(map(int, input().split()))
px = sorted(px, key=lambda x: (x[0], x[1]))

ans = [0] * N
ans[0] = sum([x for (p, x) in px if p == 1])
for i in range(N - 1):
    a, b = ab[i]
    ans[b-1] += ans[a-1] + sum([x for (p, x) in px if p == b])

print(*ans)


### FIXED CODE ###

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

import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
ab = [None] * (N - 1)
for i in range(N - 1):
    ab[i] = list(map(int, input().split()))
ab = sorted(ab, key=lambda x: (x[0], x[1]))

px_n = [0] * N
for i in range(Q):
    p, x = list(map(int, input().split()))
    px_n[p-1] += x

ans = [0] * N
ans[0] = px_n[0]
for (a, b) in ab:
    ans[b-1] += ans[a-1] + px_n[b-1]

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

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

runlength_one = []
runlength_zero = []
counter = 0
prev = '1'
for s in S:
    if s == prev:
        counter += 1
    else:
        if s == '1':
            runlength_zero.append(counter)
        else:
            runlength_one.append(counter)
        counter = 1
        prev = s
if S[-1] == '1':
    runlength_one.append(counter)
else:
    runlength_zero.append(counter)


max = 0
for i in range(len(runlength_one) - K + 1):
    length = sum(runlength_one[i:i+K+1]) + sum(runlength_zero[i:i+K])
    max = length if max < length else max

if max == 0:
    max = sum(runlength_one) + sum(runlength_zero)

print(str(max))


### FIXED CODE ###

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

runlength_one = []
runlength_zero = []
counter = 0
prev = '1'
for s in S:
    if s == prev:
        counter += 1
    else:
        if s == '1':
            runlength_zero.append(counter)
        else:
            runlength_one.append(counter)
        counter = 1
        prev = s
if S[-1] == '1':
    runlength_one.append(counter)
else:
    runlength_one.append(0)
    runlength_zero.append(counter)

max = 0
sum_one = 0
sum_zero = 0
for i in range(len(runlength_one)):
    if i + K >= len(runlength_one):
        break

    if i == 0:
        sum_one = sum(runlength_one[0:K + 1])
        sum_zero = sum(runlength_zero[0:K])
    else:
        sum_one = sum_one - runlength_one[i - 1] + runlength_one[i + K]
        sum_zero = sum_zero - runlength_zero[i - 1] + runlength_zero[i + K - 1]
        
    length = sum_one + sum_zero
    max = length if max < length else max

if max == 0:
    max = sum(runlength_one) + sum(runlength_zero)

print(str(max))
### BUGGY CODE ###

# 正解ではない

from queue import PriorityQueue

q = PriorityQueue()


def main():
    H, W = map(int, input().split())
    Ch, Cw = map(int, input().split())
    Dh, Dw = map(int, input().split())
    S = [list(input()) for i in range(H)]
    q.put((0, (Ch - 1, Cw - 1)))

    answer = resolve(S, (Dh - 1, Dw - 1), H, W)
    print(answer)


def resolve(S, D, H, W):
    while not q.empty():
        worp_count, (x, y) = q.get()
        S[x][y] = '/'

        if (x, y) == D:
            return worp_count

        for i, j in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
            if 0 <= i < H and 0 <= j < W and S[i][j] in ('.', '1'):
                q.put((worp_count, (i, j)))
                S[i][j] = '0'

        for i, j in [(x + a, y + b) for a in range(-2, 3) for b in range(-2, 3)]:
            if (i, j) in ((x, y), (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)):
                continue
            if 0 <= i < H and 0 <= j < W and S[i][j] == '.':
                q.put((worp_count + 1, (i, j)))
                S[i][j] = '1'

    return -1


if __name__ == "__main__":
    main()


### FIXED CODE ###

from collections import deque


def main():
    H, W = map(int, input().split())
    Ch, Cw = map(int, input().split())
    Dh, Dw = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    d = [[-1 for _ in range(W)] for _ in range(H)]

    q = deque([])
    q.appendleft((0, Ch - 1, Cw - 1))

    while len(q):
        worp_count, x, y = q.popleft()

        if d[x][y] != -1:
            continue
        d[x][y] = worp_count

        for i, j in ((a, b) for a in range(-2, 3) for b in range(-2, 3)):
            nx = x + i
            ny = y + j
            if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == '.':
                if i == 0 and j == 0:
                    continue
                elif abs(i) + abs(j) == 1:
                    q.appendleft((worp_count, nx, ny))
                else:
                    q.append((worp_count + 1, nx, ny))

    print(d[Dh - 1][Dw - 1])


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

import functools
import fractions

def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N = int(input())
    A = list(map(int, input().split()))
  else:
    # N = 3
    # A = [7,6,8]
    # N = 3
    # A = [12,15,18]
    N = 2
    A = [1000000000,1000000000]

  x = 0
  for i in range(N):
    tmp = functools.reduce(fractions.gcd, A[0:i]+A[i+1:])
    if tmp > x:
      x = tmp
  
  print(x)
  
main()

### FIXED CODE ###

import functools
import fractions

def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N = int(input())
    A = list(map(int, input().split()))
  else:
    # N = 3
    # A = [7,6,8]
    N = 3
    A = [12,15,18]
    # N = 3
    # A = [1,1000000000,1000000000]

  AL = [0] * N    
  AR = [0] * N
  for i in range(N):
    if i != 0:
      AL[i] = fractions.gcd(AL[i-1],A[i])
    else:
      AL[i] = A[i]
    j = N - i - 1
    if j!= N - 1:
      AR[j] = fractions.gcd(AR[j+1],A[j])
    else:
      AR[j] = A[j]
      
  if 'get_ipython' in globals():   
    print(AL)
    print(AR)
  
  x = 0
  for i in range(N):
    if i == 0:
      tmp = AR[i+1]
    elif i == N - 1:
      tmp = AL[i-1]
    else:
      tmp = fractions.gcd(AL[i-1],AR[i+1])
    if tmp > x: x = tmp
  
  print(x)
    
main()
### BUGGY CODE ###

def cf(x1,x2):
    count=[]
    for i in range(2,min(x1,x2)+1):
        if x1 % i == 0 and x2 % i == 0:
            count.append(i)
    return count

A,B = map(int,input().split())
list = cf(A,B)
c = 1
for i in list:
    for j in list:
        if i % j == 0:
            if i == j:
                c +=1
            else:
                break
print(c)

### FIXED CODE ###

import math
import collections
def f(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n = n//f
        else:f += 2
    if n != 1:
        a.append(n)
    return a

a,b = map(int,input().split())
a = set(collections.Counter(f(a)).keys())
b = set(collections.Counter(f(b)).keys())
print(len(a&b)+1)
### BUGGY CODE ###

N, K, Q = list(map(int, input().split()))
S =[K]*N
s = [1]*N
for i in range(Q):
    A = int(input())-1
    S = [S[j] - s[j] if j != A else S[j] for j in range(N)]
for i in range(N):
    if S[i]<1:
        print('No')
    else:
        print('Yes')

### FIXED CODE ###

def main():
    N, K, Q = list(map(int, input().split()))
    S =[K-Q]*N
    A=[]
    for i in range(Q):
        A.append(int(input())-1)
    for i in A:
        S[i] = S[i]+1
    for i in range(N):
        if S[i]<1:
            print('No')
        else:
            print('Yes')
if __name__ == '__main__':
    main()
### BUGGY CODE ###

N = int(input())
P = [i for i in map(int, input().split())]

count = 0
for i in range(N):
	flag = 1
	for j in range(i):
		if P[i] <= P[j]:
			flag = 1
		else:
			flag = -1
			break

	if flag == 1:
		count += 1

			
print(count)


### FIXED CODE ###

N = int(input())
P = [i for i in map(int, input().split())]

"""
count = 0
for i in range(N):
	flag = 1
	for j in range(i):
		if P[i] <= P[j]:
			flag = 1
		else:
			flag = -1
			break

	if flag == 1:
		count += 1

print(count)
"""
count = 0
min_ = P[0]
for p in P:
	if p <= min_:
		count += 1
		min_ = p

print(count)
### BUGGY CODE ###

a, b, c, d = map(int, input().split())
count = 0

for i in range(a, b + 1):
    if i % c != 0 and i % d != 0:
        count += 1

print(count)




### FIXED CODE ###

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

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

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

dif = b - a + 1

# divC = dif // c
# divD = dif // d
divC = 0
divD = 0

if a % c == 0:
    CF = a
else:
    CF = a + (c - (a % c))

if a % d == 0:
    DF = a
else:
    DF = a + (d - (a % d))

divC = (b - CF) // c + 1
divD = (b - DF) // d + 1


kobai = lcm(c, d)
if a % kobai == 0:
    KF = a
else:
    KF = a + (kobai - (a % kobai))

#divKobai = dif // kobai
divKobai = (b - KF) // kobai + 1

# print('# divC: ' + str(divC))
# print('# divD: ' + str(divD))
#print(divKobai)
print(dif - (divC + divD - divKobai))
### BUGGY CODE ###

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

E = []

for i in range(N-K+1):
    E.append((sum(p[i:i+K])+K)/2)

print(max(E))

### FIXED CODE ###

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

S = [0]
E = []

for i in range(0, N):
    S.append(S[i]+p[i])

for i in range(0, N-K+1):
    E.append(S[i+K]-S[i])


print((max(E)+K)/2)
### BUGGY CODE ###

def dw_heap(A,i):
    alen = len(A)
    l = i*2 + 1
    r = (i+1)*2
    largest = i
    if l < alen and A[l] > A[largest]: largest = l
    if r < alen and A[r] > A[largest]: largest = r
    if largest != i:
        A[i], A[largest] = A[largest], A[i] 
        dw_heap(A, largest)


def up_heap(A,i):
    if i==0: return
    p = i//2 if i%2 == 1 else i//2 - 1
    if A[i] > A[p]:
        A[i], A[p] = A[p], A[i] 
        up_heap(A,p)


def insert(A,k):
    A.append(k)
    up_heap(A,len(A)-1)


def extract(A):
    A[0],A[-1] = A[-1],A[0]
    max_item = A.pop()
    dw_heap(A,0)
    return max_item


if __name__=='__main__':
    hp = []
    while True:
        op = input().split() 
        if   op[0] == 'end': break
        if   op[0] == 'insert': insert(hp,int(op[1]))
        else: print(extract(hp))

### FIXED CODE ###

from sys import stdin

def dw_heap(A,i):
    largest = i
    while True:
        l = i*2
        r = l + 1
        if A[l] > A[largest]: largest = l
        if A[r] > A[largest]: largest = r
        if largest == i: return
        A[i], A[largest] = A[largest], A[i] 
        i = largest

def up_heap(A,i):
    p = i//2
    while A[i] > A[p]:
        A[i], A[p] = A[p], A[i] 
        i = p
        p = i//2

if __name__=='__main__':
    hp = [-1]*(4000000)
    hp[0] = 2000000001
    i = 1
    j = -1 
    for op in stdin:
        if op[0] == 'i':# insert
            hp[i] = int(op[7:])
            up_heap(hp,i)
            i += 1
        elif op[1] == 'x':# extract
            hp[j],hp[1],hp[i-1] = hp[1],hp[i-1],-1
            dw_heap(hp,1)
            i -= 1
            j -= 1
        else:# end
            break
    print(*hp[:j:-1],sep='\n')
### BUGGY CODE ###

import math
n = int(input())
mylist = [int(math.sqrt(i)) for i in range(1,n+1)]

def main():
    for N in range(1,n+1):
        result = 0
        for x in range(1,mylist[N-1]+1):
            tempx = x**2
            for y in range(1, mylist[N-x**2]+1):
                tempy = y**2
                tempxy = x*y
                if N-tempx-tempy-tempxy < 0:
                    break
                else:
                    for z in range(1, mylist[N-tempx-tempy-tempxy]+1):
                        if tempx + tempy + z**2 + tempxy + y*z + z*x == N:
                            result += 1
                        elif tempx + tempy + z**2 + tempxy + y*z + z*x > N:
                            break
        print(result)
main()

### FIXED CODE ###

import math
n = int(input())
mylist = [int(math.sqrt(i)) for i in range(1,n+1)]
mylist = [0]+mylist

def main():
    for N in range(1,n+1):
        result = 0
        for x in range(1,mylist[N//3]+1):
            tempx = x**2
            for y in range(x, mylist[(N-x**2)//2]+1):
                tempy = y**2
                tempxy = x*y
                tempxandy = (x+y)/2
                tempnum = N-tempx-tempy-tempxy+tempxandy**2
                if tempnum <= 0:
                    break
                else:
                    if math.sqrt(tempnum)*2 == int(math.sqrt(tempnum)*2):
                        z = math.sqrt(tempnum)-tempxandy
                        #print(x,y,z,N)
                        if z < y:
                            break
                        elif x != y and y != z:
                            result += 6
                        elif x == y and y == z:
                            result += 1
                        else:
                            result += 3
        print(result)
main()
### BUGGY CODE ###

N, M = map(int, input().split())
A = set(map(int, input().split()))
num = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
ans = {}
for i in range(N + 1):
    x = 0
    for j in range(1, 10):
        k = num[j]
        if j not in A or i - k < 0:
            continue
        if i == k:
            x = max(x, j)
            continue
        if i - k in ans:
            an = ans[i - k]
            if an == 0:
                continue
            x = max(x, an * 10 + j)
            x = max(x, j * 10 ** len(str(an)) + an)
    ans[i] = x
print(ans[N])

### FIXED CODE ###

N, M = map(int, input().split())
A = list(map(int, input().split()))
num = [2, 5, 5, 4, 5, 6, 3, 7, 6]
ans = {}
for i in range(N + 1):
    x = 0
    for j in A:
        k = num[j - 1]
        if i - k < 0:
            continue
        if i == k:
            x = max(x, j)
            continue
        an = ans[i - k]
        if an == 0:
            continue
        x = max(x, an * 10 + j)
    ans[i] = x
print(ans[N])