def main():
    N, M = map(int, input().split())
    A = [int(a) for a in input().split()]
    d1 = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

    # for a given number of matches, give the largest digit
    d2 = dict()
    for a in A:
        if d1[a] not in d2: d2[d1[a]] = a
        else: d2[d1[a]] = max(a, d2[d1[a]])

    # for these digit, give the match count
    lv = []
    for a in d2.values(): lv.append(a)

    dp = [None for _ in range(N+1)]
    dp[0] = ""
    for i in range(1, N+1):
        for a in lv:
            j = i - d1[a]
            if j >= 0 and dp[j] != None:
                if dp[i] == None:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) < len(dp[j]) + 1:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) == len(dp[j]) + 1:
                    dp[i] = max(dp[i], dp[j] + str(a))
                # print(dp[i], i)
    # print(dp)
    return dp[N]




if __name__ == '__main__':
    print(main()) def main():
    N, M = map(int, input().split())
    A = [int(a) for a in input().split()]
    d1 = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

    # for a given number of matches, give the largest digit
    d2 = dict()
    for a in A:
        if d1[a] not in d2: d2[d1[a]] = a
        else: d2[d1[a]] = max(a, d2[d1[a]])

    # for these digit, give the match count
    lv = []
    for a in d2.values(): lv.append(a)

    dp = [None for _ in range(N+1)]
    dp[0] = ""
    for i in range(1, N+1):
        for a in lv:
            j = i - d1[a]
            if j >= 0 and dp[j] != None:
                if dp[i] == None:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) < len(dp[j]) + 1:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) == len(dp[j]) + 1:
                    dp[i] = max(dp[i], dp[j] + str(a))
                # print(dp[i], i)
    # print(dp)
    return dp[N] main 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()] d1 = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} d1 {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6} 1 1 2 2 2 5 3 3 5 4 4 4 5 5 5 6 6 6 7 7 3 8 8 7 9 9 6 d2 = dict() d2 dict() dict for a in A:
        if d1[a] not in d2: d2[d1[a]] = a
        else: d2[d1[a]] = max(a, d2[d1[a]])

    # for these digit, give the match count
     a A if d1[a] not in d2: d2[d1[a]] = a
        else: d2[d1[a]] = max(a, d2[d1[a]])

    # for these digit, give the match count
     d1[a] not in d2 d1[a] d1 a d2 lv = [] lv [] for a in d2.values(): lv.append(a)

     a d2.values() d2.values d2 values None for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) None dp = [None for _ in range(N+1)] dp [None for _ in range(N+1)] dp[0] = "" dp[0] dp 0 "" for i in range(1, N+1):
        for a in lv:
            j = i - d1[a]
            if j >= 0 and dp[j] != None:
                if dp[i] == None:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) < len(dp[j]) + 1:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) == len(dp[j]) + 1:
                    dp[i] = max(dp[i], dp[j] + str(a))
                # print(dp[i], i)
    # print(dp)
     i range(1, N+1) range 1 N+1 N 1 for a in lv:
            j = i - d1[a]
            if j >= 0 and dp[j] != None:
                if dp[i] == None:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) < len(dp[j]) + 1:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) == len(dp[j]) + 1:
                    dp[i] = max(dp[i], dp[j] + str(a))
                # print(dp[i], i)
    # print(dp)
     a lv j = i - d1[a] j i - d1[a] i d1[a] d1 a if j >= 0 and dp[j] != None:
                if dp[i] == None:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) < len(dp[j]) + 1:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) == len(dp[j]) + 1:
                    dp[i] = max(dp[i], dp[j] + str(a))
                # print(dp[i], i)
    # print(dp)
     j >= 0 and dp[j] != None j >= 0 j 0 dp[j] != None dp[j] dp j None if dp[i] == None:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) < len(dp[j]) + 1:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) == len(dp[j]) + 1:
                    dp[i] = max(dp[i], dp[j] + str(a))
                # print(dp[i], i)
    # print(dp)
     dp[i] == None dp[i] dp i None dp[i] = dp[j] + str(a) dp[i] dp i dp[j] + str(a) dp[j] dp j str(a) str a elif len(dp[i]) < len(dp[j]) + 1:
                    dp[i] = dp[j] + str(a)
                 len(dp[i]) < len(dp[j]) + 1 len(dp[i]) len dp[i] dp i len(dp[j]) + 1 len(dp[j]) len dp[j] dp j 1 dp[i] = dp[j] + str(a) dp[i] dp i dp[j] + str(a) dp[j] dp j str(a) str a elif len(dp[i]) == len(dp[j]) + 1:
                    dp[i] = max(dp[i], dp[j] + str(a))
                # print(dp[i], i)
    # print(dp)
     len(dp[i]) == len(dp[j]) + 1 len(dp[i]) len dp[i] dp i len(dp[j]) + 1 len(dp[j]) len dp[j] dp j 1 dp[i] = max(dp[i], dp[j] + str(a)) dp[i] dp i max(dp[i], dp[j] + str(a)) max dp[i] dp i dp[j] + str(a) dp[j] dp j str(a) str a return dp[N] dp[N] dp N if __name__ == '__main__':
    print(main()) __name__ == '__main__' __name__ '__main__' print(main()) print main() main def main():
    N, M = map(int, input().split())
    A = [int(a) for a in input().split()]
    d1 = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

    # for a given number of matches, give the largest digit
    d2 = dict()
    for a in A:
        if d1[a] not in d2: d2[d1[a]] = a
        else: d2[d1[a]] = max(a, d2[d1[a]])

    # for these digit, give the match count
    lv = []
    for a in d2.values(): lv.append(a)

    dp = [None for _ in range(N+1)]
    dp[0] = ""
    for i in range(1, N+1):
        for a in lv:
            j = i - d1[a]
            if j >= 0 and dp[j] != None:
                if dp[i] == None:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) < len(dp[j]) + 1:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) == len(dp[j]) + 1:
                    dp[i] = max(dp[i], dp[j] + str(a))
                # print(dp[i], i)
    # print(dp)
    return dp[N] def main():
    N, M = map(int, input().split())
    A = [int(a) for a in input().split()]
    d1 = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

    # for a given number of matches, give the largest digit
    d2 = dict()
    for a in A:
        if d1[a] not in d2: d2[d1[a]] = a
        else: d2[d1[a]] = max(a, d2[d1[a]])

    # for these digit, give the match count
    lv = []
    for a in d2.values(): lv.append(a)

    dp = [None for _ in range(N+1)]
    dp[0] = ""
    for i in range(1, N+1):
        for a in lv:
            j = i - d1[a]
            if j >= 0 and dp[j] != None:
                if dp[i] == None:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) < len(dp[j]) + 1:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) == len(dp[j]) + 1:
                    dp[i] = max(dp[i], dp[j] + str(a))
                # print(dp[i], i)
    # print(dp)
    return dp[N] main
def main():
    N, M = map(int, input().split())
    A = [int(a) for a in input().split()]
    d1 = {1:2, 2:5, 3:5, 4:4, 5:5, 6:6, 7:3, 8:7, 9:6}

    # for a given number of matches, give the largest digit
    d2 = dict()
    for a in A:
        if d1[a] not in d2: d2[d1[a]] = a
        else: d2[d1[a]] = max(a, d2[d1[a]])

    # for these digit, give the match count
    lv = []
    for a in d2.values(): lv.append(a)

    dp = [None for _ in range(N+1)]
    dp[0] = ""
    for i in range(1, N+1):
        for a in lv:
            j = i - d1[a]
            if j >= 0 and dp[j] != None:
                if dp[i] == None:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) < len(dp[j]) + 1:
                    dp[i] = dp[j] + str(a)
                elif len(dp[i]) == len(dp[j]) + 1:
                    dp[i] = max(dp[i], dp[j] + str(a))
                # print(dp[i], i)
    # print(dp)
    return dp[N]




if __name__ == '__main__':
    print(main())