import sys

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    A = [int(a) for a in input().split()]
    use = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    U = dict()
    for a in A:
        if use[a] in U: U[use[a]] = max(a, U[use[a]])
        else: U[use[a]] = a

    DP = [-1] * (N + 1)
    DP[0] = 0
    for i in range(N):
        if DP[i] > -1:
            for key in U:
                if i + key <= N:
                    DP[i + key] = max(DP[i + key], DP[i] * 10 + U[key])
    print(DP[N])

    return 0

if __name__ =="__main__":
    solve() import sys sys def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    A = [int(a) for a in input().split()]
    use = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    U = dict()
    for a in A:
        if use[a] in U: U[use[a]] = max(a, U[use[a]])
        else: U[use[a]] = a

    DP = [-1] * (N + 1)
    DP[0] = 0
    for i in range(N):
        if DP[i] > -1:
            for key in U:
                if i + key <= N:
                    DP[i + key] = max(DP[i + key], DP[i] * 10 + U[key])
    print(DP[N])

    return 0 solve input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline N, M = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split M int(a) for a in input().split() for a in input().split() a input().split() ().split () input split for a in input().split() int(a) int a A = [int(a) for a in input().split()] A [int(a) for a in input().split()] use = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6] use [6, 2, 5, 5, 4, 5, 6, 3, 7, 6] 6 2 5 5 4 5 6 3 7 6 U = dict() U dict() dict for a in A:
        if use[a] in U: U[use[a]] = max(a, U[use[a]])
        else: U[use[a]] = a

     a A if use[a] in U: U[use[a]] = max(a, U[use[a]])
        else: U[use[a]] = a

     use[a] in U use[a] use a U 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(N):
        if DP[i] > -1:
            for key in U:
                if i + key <= N:
                    DP[i + key] = max(DP[i + key], DP[i] * 10 + U[key])
     i range(N) range N if DP[i] > -1:
            for key in U:
                if i + key <= N:
                    DP[i + key] = max(DP[i + key], DP[i] * 10 + U[key])
     DP[i] > -1 DP[i] DP i -1 for key in U:
                if i + key <= N:
                    DP[i + key] = max(DP[i + key], DP[i] * 10 + U[key])
     key U if i + key <= N:
                    DP[i + key] = max(DP[i + key], DP[i] * 10 + U[key])
     i + key <= N i + key i key N DP[i + key] = max(DP[i + key], DP[i] * 10 + U[key]) DP[i + key] DP i + key i key max(DP[i + key], DP[i] * 10 + U[key]) max DP[i + key] DP i + key i key DP[i] * 10 + U[key] DP[i] * 10 DP[i] DP i 10 U[key] U key print(DP[N]) print DP[N] DP N return 0 0 if __name__ =="__main__":
    solve() __name__ =="__main__" __name__ "__main__" solve() solve def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    A = [int(a) for a in input().split()]
    use = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    U = dict()
    for a in A:
        if use[a] in U: U[use[a]] = max(a, U[use[a]])
        else: U[use[a]] = a

    DP = [-1] * (N + 1)
    DP[0] = 0
    for i in range(N):
        if DP[i] > -1:
            for key in U:
                if i + key <= N:
                    DP[i + key] = max(DP[i + key], DP[i] * 10 + U[key])
    print(DP[N])

    return 0 def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    A = [int(a) for a in input().split()]
    use = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    U = dict()
    for a in A:
        if use[a] in U: U[use[a]] = max(a, U[use[a]])
        else: U[use[a]] = a

    DP = [-1] * (N + 1)
    DP[0] = 0
    for i in range(N):
        if DP[i] > -1:
            for key in U:
                if i + key <= N:
                    DP[i + key] = max(DP[i + key], DP[i] * 10 + U[key])
    print(DP[N])

    return 0 solve
import sys

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    A = [int(a) for a in input().split()]
    use = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
    U = dict()
    for a in A:
        if use[a] in U: U[use[a]] = max(a, U[use[a]])
        else: U[use[a]] = a

    DP = [-1] * (N + 1)
    DP[0] = 0
    for i in range(N):
        if DP[i] > -1:
            for key in U:
                if i + key <= N:
                    DP[i + key] = max(DP[i + key], DP[i] * 10 + U[key])
    print(DP[N])

    return 0

if __name__ =="__main__":
    solve()