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

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    n,m = LI()
    a = LI()
    match = [0,2,5,5,4,5,6,3,7,6]
    dp = [-1] * (n+1)
    dp[0] = 0
    for i in range(1,n+1):
        for x in a:
            if i < match[x]: continue
            if dp[i-match[x]] < 0: continue
            dp[i] = max(dp[i],dp[i-match[x]]*10+x)
    print(dp[-1])

if __name__ == '__main__':
    main() import sys,queue,math,copy,itertools,bisect,collections,heapq sys queue math copy itertools bisect collections heapq def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    n,m = LI()
    a = LI()
    match = [0,2,5,5,4,5,6,3,7,6]
    dp = [-1] * (n+1)
    dp[0] = 0
    for i in range(1,n+1):
        for x in a:
            if i < match[x]: continue
            if dp[i-match[x]] < 0: continue
            dp[i] = max(dp[i],dp[i-match[x]]*10+x)
    print(dp[-1]) main sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 INF = 10**18 INF 10**18 10 18 MOD = 10**9 + 7 MOD 10**9 + 7 10**9 10 9 7 int(x) for x in sys.stdin.readline().split() for x in sys.stdin.readline().split() x sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split for x in sys.stdin.readline().split() int(x) int x LI = lambda : [int(x) for x in sys.stdin.readline().split()] LI lambda : [int(x) for x in sys.stdin.readline().split()] [int(x) for x in sys.stdin.readline().split()] NI = lambda : int(sys.stdin.readline()) NI lambda : int(sys.stdin.readline()) int(sys.stdin.readline()) int sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline SI = lambda : sys.stdin.readline().rstrip() SI lambda : sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip n,m = LI() n LI() LI m a = LI() a LI() LI match = [0,2,5,5,4,5,6,3,7,6] match [0,2,5,5,4,5,6,3,7,6] 0 2 5 5 4 5 6 3 7 6 dp = [-1] * (n+1) dp [-1] * (n+1) [-1] -1 n+1 n 1 dp[0] = 0 dp[0] dp 0 0 for i in range(1,n+1):
        for x in a:
            if i < match[x]: continue
            if dp[i-match[x]] < 0: continue
            dp[i] = max(dp[i],dp[i-match[x]]*10+x)
     i range(1,n+1) range 1 n+1 n 1 for x in a:
            if i < match[x]: continue
            if dp[i-match[x]] < 0: continue
            dp[i] = max(dp[i],dp[i-match[x]]*10+x)
     x a if i < match[x]: continue
             i < match[x] i match[x] match x if dp[i-match[x]] < 0: continue
             dp[i-match[x]] < 0 dp[i-match[x]] dp i-match[x] i match[x] match x 0 dp[i] = max(dp[i],dp[i-match[x]]*10+x) dp[i] dp i max(dp[i],dp[i-match[x]]*10+x) max dp[i] dp i dp[i-match[x]]*10+x dp[i-match[x]]*10 dp[i-match[x]] dp i-match[x] i match[x] match x 10 x print(dp[-1]) print dp[-1] dp -1 if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    n,m = LI()
    a = LI()
    match = [0,2,5,5,4,5,6,3,7,6]
    dp = [-1] * (n+1)
    dp[0] = 0
    for i in range(1,n+1):
        for x in a:
            if i < match[x]: continue
            if dp[i-match[x]] < 0: continue
            dp[i] = max(dp[i],dp[i-match[x]]*10+x)
    print(dp[-1]) def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    n,m = LI()
    a = LI()
    match = [0,2,5,5,4,5,6,3,7,6]
    dp = [-1] * (n+1)
    dp[0] = 0
    for i in range(1,n+1):
        for x in a:
            if i < match[x]: continue
            if dp[i-match[x]] < 0: continue
            dp[i] = max(dp[i],dp[i-match[x]]*10+x)
    print(dp[-1]) main
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    n,m = LI()
    a = LI()
    match = [0,2,5,5,4,5,6,3,7,6]
    dp = [-1] * (n+1)
    dp[0] = 0
    for i in range(1,n+1):
        for x in a:
            if i < match[x]: continue
            if dp[i-match[x]] < 0: continue
            dp[i] = max(dp[i],dp[i-match[x]]*10+x)
    print(dp[-1])

if __name__ == '__main__':
    main()