#include<cstdio>
#include<iostream>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;

const int less_months[] = {4,6,9,11};
const string bar[10] = {"1110111", "0010010", "1011101", "1011011", "0111010",
			"1101011", "1101111", "1010010", "1111111", "1111011"};

inline bool is_uruu(int y){
  if(y%400==0)return true;
  if(y%100==0)return false;
  if(y%4==0)return true;
  return false;
}

inline int count(int x, int len, int pos, vector<int> &v){
  int res = 0;
  while(len--){
    pos--;
    for(int i=0;i<7;i++){
      if(bar[x%10][i] == '1' && v[pos*7+i] == 1)res++;
    }
    x /= 10;
  }
  return res;
}

int main(){
  int n;
  scanf("%d",&n);
  int k;
  scanf("%d",&k);
  vector<int> v(14*7,1);
  for(int i=0;i<k;i++){
    int a,b;
    scanf("%d%d",&a, &b);
    v[a*7+b] = 0;
  }

  ll ymd[110] = {}, hms[110] = {};
  for(int y=0;y<=9999;y++){
    int Y = count(y,4,4,v);

    for(int m=1;m<=12;m++){
      int M = count(m,2,6,v);

      int lim_d = 31;
      for(int i=0;i<4;i++){
	if(less_months[i]==m)lim_d = 30;
      }
      if(m==2){
	if(is_uruu(y))lim_d = 29;
	else lim_d = 28;
      }

      for(int d=1;d<=lim_d;d++){
	int D = count(d,2,8,v);
	ymd[Y+M+D]++;
      }
    }
  }

  for(int h=0;h<24;h++){
    int H = count(h,2,10,v);
    for(int m=0;m<60;m++){
      int M = count(m,2,12,v);
      for(int s=0;s<60;s++){
	int S = count(s,2,14,v);
	hms[H+M+S]++;
      }
    }
  }

  ll ans = 0;
  for(int i=0;i<=n;i++){
    ans += ymd[i] * hms[n-i];
  }
  printf("%lld\n",ans);
}