#include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;

int n, m, a, b, c;
LL d;
int main() {
	while ( ~scanf( "%d %d", &n, &m ) && n && m ) {
		LL mp[ 100 ][ 100 ];
		for ( int i = 0; i < n; i++ ) {
			for ( int j = 0; j < n; j++ ) {
				mp[ i ][ j ] = LL_INF;
			}
		}
		for ( int i = 0; i < n; i++ ) {
			mp[ i ][ i ] = 0;
		}
		for ( int i = 0; i < m; i++ ) {
			scanf( "%d", &a );
			switch ( a ) {
				case 0:
					scanf( "%d %d", &b, &c );
					if ( mp[ b - 1 ][ c - 1 ] == LL_INF ) {
						printf( "-1\n" );
					}
					else {
						printf( "%lld\n", mp[ b - 1 ][ c - 1 ] );
					}
					break;
				case 1:
					scanf( "%d %d %lld", &b, &c, &d );
					if ( mp[ b - 1 ][ c - 1 ] > d ) {
						mp[ b - 1 ][ c - 1 ] = mp[ c - 1 ][ b - 1 ] = min( d, mp[ b - 1 ][ c - 1 ] );
						for ( int j = 0; j < n; j++ ) {
							for ( int k = 0; k < n; k++ ) {
								for ( int l = 0; l < n; l++ ) {
									mp[ k ][ l ] = mp[ l ][ k ] = min( mp[ k ][ l ], mp[ k ][ j ] + mp[ j ][ l ] );
								}
							}
						}
					}
					break;
			}
		}
	}
	return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
int h[10] = { 6,2,5,5,4,5,6,3,7,6 };
bool b[10] = { false };
int dp[10100][10];
int main() {
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int a;
		cin >> a;
		b[a] = true;
	}
	for (int i = 0; i < n + 10; i++) {
		for (int j = 0; j < 10; j++) {
			dp[i][j] = -1;
		}
	}
	for (int i = 0; i < 10; i++) {
		dp[0][i] = 0;
	}
	for (int i = 0; i <= n; i++) {
		if (dp[i][0] == -1) {
			continue;
		}
		for (int j = 1; j < 10; j++) {
			if (!b[j]) {
				continue;
			}
			if (dp[i + h[j]][0] == -1) {
				for (int k = 0; k < 10; k++) {
					dp[i + h[j]][k] = dp[i][k];
					if (j == k) {
						dp[i + h[j]][k]++;
					}
				}
			}
			else {
				int kari[10];
				int sl = 0, sr = 0;
				for (int k = 0; k < 10; k++) {
					kari[k] = dp[i][k];
					if (j == k) {
						kari[k]++;
					}
					sl += dp[i + h[j]][k];
					sr += kari[k];
				}
				bool bo = true;
				if (sl != sr) {
					if (sl > sr) {
						bo = false;
					}
				}
				else {
					int l = 9, r = 9;
					while (1) {
						if (l == 0 && r == 0) {
							bo = false;
							break;
						}
						if (dp[i + h[j]][l] == 0) {
							l--;
							continue;
						}
						if (kari[r] == 0) {
							r--;
							continue;
						}
						if (l == r && dp[i + h[j]][l] == kari[r]) {
							l--;
							r--;
							continue;
						}
						else if (l < r) {
							break;
						}
						else if (l > r) {
							bo = false;
							break;
						}
						else if (dp[i + h[j]][l] < kari[r]) {
							break;
						}
						else if (dp[i + h[j]][l] > kari[r]) {
							bo = false;
							break;
						}
					}
				}
				if (bo) {
					for (int k = 0; k < 10; k++) {
						dp[i + h[j]][k] = kari[k];
					}
				}
			}
		}
	}
	for (int i = 9; i > 0; i--) {
		for (int j = 0; j < dp[n][i]; j++) {
			cout << i;
		}
	}
	cout << endl;
	return 0;
}
