#!/usr/bin/env python3
import sys, math, fractions, itertools

l = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

def solve(N: int, M: int, A: "List[int]"):
    dp = [-1] * (N+10) # 初期化
    dp[0] = 0 # 初期化
    for i in range(N-1): # 各桁について考える
        for s in A: # 各使える数字をみる
            if dp[i] == -1 and i!=0: # 条件を満たすように作れない場合、continue
                continue
            if dp[i + l[s]] < dp[i]*10+s:
                dp[i + l[s]] = dp[i]*10+s
    print(dp[N])
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(M) ]  # type: "List[int]"
    solve(N, M, A)

if __name__ == '__main__':
    main() import sys, math, fractions, itertools sys math fractions itertools l = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] l [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] 0 2 5 5 4 5 6 3 7 6 def solve(N: int, M: int, A: "List[int]"):
    dp = [-1] * (N+10) # 初期化
    dp[0] = 0 # 初期化
    for i in range(N-1): # 各桁について考える
        for s in A: # 各使える数字をみる
            if dp[i] == -1 and i!=0: # 条件を満たすように作れない場合、continue
                continue
            if dp[i + l[s]] < dp[i]*10+s:
                dp[i + l[s]] = dp[i]*10+s
    print(dp[N])
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template) solve dp = [-1] * (N+10) dp [-1] * (N+10) [-1] -1 N+10 N 10 dp[0] = 0 dp[0] dp 0 0 for i in range(N-1): # 各桁について考える
        for s in A: # 各使える数字をみる
            if dp[i] == -1 and i!=0: # 条件を満たすように作れない場合、continue
                continue
            if dp[i + l[s]] < dp[i]*10+s:
                dp[i + l[s]] = dp[i]*10+s
     i range(N-1) range N-1 N 1 for s in A: # 各使える数字をみる
            if dp[i] == -1 and i!=0: # 条件を満たすように作れない場合、continue
                continue
            if dp[i + l[s]] < dp[i]*10+s:
                dp[i + l[s]] = dp[i]*10+s
     s A if dp[i] == -1 and i!=0: # 条件を満たすように作れない場合、continue
                continue
             dp[i] == -1 and i!=0 dp[i] == -1 dp[i] dp i -1 i!=0 i 0 continue if dp[i + l[s]] < dp[i]*10+s:
                dp[i + l[s]] = dp[i]*10+s
     dp[i + l[s]] < dp[i]*10+s dp[i + l[s]] dp i + l[s] i l[s] l s dp[i]*10+s dp[i]*10 dp[i] dp i 10 s dp[i + l[s]] = dp[i]*10+s dp[i + l[s]] dp i + l[s] i l[s] l s dp[i]*10+s dp[i]*10 dp[i] dp i 10 s print(dp[N]) print dp[N] dp N return N: int N M: int M A: "List[int]" A def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(M) ]  # type: "List[int]"
    solve(N, M, A) main def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
     iterate_tokens for line in sys.stdin:
            for word in line.split():
                yield word
     line sys.stdin sys stdin for word in line.split():
                yield word
     word line.split() line.split line split yield word word tokens = iterate_tokens() tokens iterate_tokens() iterate_tokens N = int(next(tokens)) N int(next(tokens)) int next(tokens) next tokens M = int(next(tokens)) M int(next(tokens)) int next(tokens) next tokens int(next(tokens)) for _ in range(M) for _ in range(M) _ range(M) range M for _ in range(M) int(next(tokens)) int next(tokens) next tokens A = [ int(next(tokens)) for _ in range(M) ] A [ int(next(tokens)) for _ in range(M) ] solve(N, M, A) solve N M A if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def solve(N: int, M: int, A: "List[int]"):
    dp = [-1] * (N+10) # 初期化
    dp[0] = 0 # 初期化
    for i in range(N-1): # 各桁について考える
        for s in A: # 各使える数字をみる
            if dp[i] == -1 and i!=0: # 条件を満たすように作れない場合、continue
                continue
            if dp[i + l[s]] < dp[i]*10+s:
                dp[i + l[s]] = dp[i]*10+s
    print(dp[N])
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template) def solve(N: int, M: int, A: "List[int]"):
    dp = [-1] * (N+10) # 初期化
    dp[0] = 0 # 初期化
    for i in range(N-1): # 各桁について考える
        for s in A: # 各使える数字をみる
            if dp[i] == -1 and i!=0: # 条件を満たすように作れない場合、continue
                continue
            if dp[i + l[s]] < dp[i]*10+s:
                dp[i + l[s]] = dp[i]*10+s
    print(dp[N])
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template) solve l = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] [0, 2, 5, 5, 4, 5, 6, 3, 7, 6] l def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(M) ]  # type: "List[int]"
    solve(N, M, A) def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(M) ]  # type: "List[int]"
    solve(N, M, A) main
#!/usr/bin/env python3
import sys, math, fractions, itertools

l = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]

def solve(N: int, M: int, A: "List[int]"):
    dp = [-1] * (N+10) # 初期化
    dp[0] = 0 # 初期化
    for i in range(N-1): # 各桁について考える
        for s in A: # 各使える数字をみる
            if dp[i] == -1 and i!=0: # 条件を満たすように作れない場合、continue
                continue
            if dp[i + l[s]] < dp[i]*10+s:
                dp[i + l[s]] = dp[i]*10+s
    print(dp[N])
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(M) ]  # type: "List[int]"
    solve(N, M, A)

if __name__ == '__main__':
    main()
