unittest
{
	assert( [ "20 4", "3 7 8 4" ].solve == "777773" );
	assert( [ "101 9", "9 8 7 6 5 4 3 2 1" ].solve == "71111111111111111111111111111111111111111111111111" );
	assert( [ "15 3", "5 4 6" ].solve == "654" );
}



import std.algorithm;
import std.bigint;
import std.conv;
import std.format;
import std.range;
import std.stdio;

void main()
{
	stdin.byLineCopy.solve.writeln;
}

auto solve( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto n = 0L;
	auto m = 0L;
	input.front.formattedRead!"%s %s"( n, m );
	input.popFront;
	auto as = input.front.split.to!( long[] );
	
	const MS = [ 0L, 2L, 5L, 5L, 4L, 5L, 6L, 3L, 7L, 6L ];
	
	// dp[ 使用したマッチの本数 ] = 作れる最大の整数
	auto dp = new BigInt[ 1 + n ];
	foreach( a; as )
	{
		auto j = MS[ a ];
		if( j < dp.length ) dp[ j ] = max( dp[ j ], BigInt( a ) );
	}
	foreach( i; 0L .. dp.length.signed )
	{
		foreach( a; as )
		{
			auto j = MS[ a ];
			if( 0 < dp[ i ] && i + j < dp.length ) dp[ i + j ] = max( dp[ i + j ], dp[ i ] * 10 + a );
		}
	}
	return format( "%s", dp[ $ - 1 ] );
}
