#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <iomanip>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#include <stdio.h>
#include <unistd.h>
using namespace std;
typedef long long int ll;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define drep(i, n) for(int i = n - 1; i >= 0; i--)
#define itrep(itr, base) for (auto itr = base.begin(); itr != base.end(); itr++)
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define yes cout << "yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define no cout << "no" << endl
#define PI 3.14159265359

const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const int mod = 1000000007;

void P(int x) {cout << x << endl;}
void P(long x) {cout << x << endl;}
void P(double x) {cout << x << endl;}
void P(ll x) {cout << x << endl;}
void P(string x) {cout << x << endl;}
void P(char x) {cout << x << endl;}

ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}

int main() {
  int h[2], m[2], k;
  cin >>  h[0] >> m[0] >> h[1] >> m[1] >> k;
  int up   = h[0] * 60 + m[0];
  int down = h[1] * 60 + m[1];
  if (down - up - k < 0) {
    P(0);
  } else {
    P(down - up - k);
  }
  return 0;
}

#include <bits/stdc++.h>
#define de(x) cout << #x << "=" << x << ", "
#define dd cout << '\n';
#define XinAi ios::sync_with_stdio(0), cin.tie(0);
#define F first
#define S second
using namespace std;
typedef pair<int,int> pii;
const int N = 1e4+10;

string dp[N], val[15];
string num = "123456789";
int c[] = {2,5,5,4,5,6,3,7,6}, cs[200], n, m;
string a[15];

void update (string &x, string y, string z)
{
    if (x.size() > y.size() + 1) return;
    if (z[0] > y[0]) z += y, swap(y, z);
    else y += z;
    if (x.size() < y.size()) x = y;
    else
    {
        for (int i=0; i<x.size(); i++)
        {
            if (x[i] > y[i]) return;
            else if (x[i] < y[i])
            {
                x = y;
                break;
            }
        }
    }
}

signed main()
{
    XinAi
    for (int i=0; i<9; i++) cs[num[i]] = c[i];
    cin >> n >> m;
    for (int i=0; i<m; i++) cin >> a[i];
    for (int i=0; i<n; i++)
        for (int j=0; j<m; j++)
        {
            if (i!=0 && dp[i]=="") continue;
            update(dp[i+cs[a[j][0]]], dp[i], a[j]);
        }
    cout << dp[n] << '\n';
}
