#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }

int main()
{
  cout << fixed << setprecision(10);
  int K,T;
  cin >> K >> T;
  vector<int> a(T);
  rep(i,T) cin >> a[i];
  sort(ALL(a));
  int sum = 0;
  rep(i,T-1)
  {
    sum += a[i];
  }
  if(sum>a[T-1])
  {
    cout << 0 << endl;
  }
  else
  {
    cout << a[T-1] - sum - 1 << endl;
  }
  return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
using namespace std;
int main()
{
  int n,m;cin >> n >> m;
  vector<int> a(m);
  rep(i,m) cin >> a[i];
  vector<vector<int>> dp(m+1, vector<int>(n+1, -1));
  sort(a.begin(), a.end());
  vector<int> b = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  dp[0][0] = 0;
  rep1(i,m) {
    rep(j,n+1) {
      if(j - b[a[i-1]] >= 0) {
	if(dp[i][j - b[a[i-1]]] != -1) {
	  dp[i][j] = max(dp[i][j - b[a[i-1]]] + 1, dp[i-1][j]);
	}
	else dp[i][j] = dp[i-1][j];
      }
      else dp[i][j] = dp[i-1][j];
    }
  }

  // rep(i,m+1) {
  //   rep(j,n+1) {
  //     cout << dp[i][j] << " ";
  //   }
  //   cout << "\n";
  // }

  int x = m, y = n;
  while(y > 0 ) {
    if(y - b[a[x-1]] >= 0) {
      if( dp[x][y] - 1 == dp[x][y - b[a[x-1]]] ) {
	cout << a[x-1];
	y -= b[a[x-1]];
      }
      else x--;
    }
    else x--;
  }
  cout << "\n";
  return 0;
}
