#include <bits/stdc++.h>
using namespace std;

typedef long long lli;

struct data{
  int p,q;
  bool operator < (const data &d) const {
    return p < d.p;
  }
};

int n,n2;
int v[14][10];
data d[100];

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

void fillv(){
  bool v2[10][7] = {{1,1,1,0,1,1,1},
                    {0,0,1,0,0,1,0},
                    {1,0,1,1,1,0,1},
                    {1,0,1,1,0,1,1},
                    {0,1,1,1,0,1,0},
                    {1,1,0,1,0,1,1},
                    {1,1,0,1,1,1,1},
                    {1,0,1,0,0,1,0},
                    {1,1,1,1,1,1,1},
                    {1,1,1,1,0,1,1}};
  bool d2[14][7];

  /*
  for(int i=0;i<10;i++){
    cout << "-- " << i << " --" << endl;
    cout << ' ' << v2[i][0] << endl;
    cout << v2[i][1] << ' ' << v2[i][2] << endl;
    cout << ' ' << v2[i][3] << endl;
    cout << v2[i][4] << ' ' << v2[i][5] << endl;
    cout << ' ' << v2[i][6] << endl;
    cout << endl;
  }
  */
  

  fill(v[0], v[14], 0);
  fill(d2[0],d2[14],true);
  sort(d, d+n2);

  for(int i=0;i<n2;i++){
    d2[d[i].p][d[i].q] = false;
  }

  for(int i=0;i<14;i++){
    for(int j=0;j<10;j++){
      for(int k=0;k<7;k++){
        v[i][j] += v2[j][k] & d2[i][k];
      }
    }
  }
  //cout << endl;
}

void solve(){  
  lli ans = 0;
  int lres = 7 * 14;
  lli res[5][lres];

  fillv();
  fill(res[0],res[5],0);

  for(int h=0;h<24;h++){
    for(int m=0;m<60;m++){
      for(int s=0;s<60;s++){
        int resn = 0;
        resn += v[8][h/10];
        resn += v[9][h%10];
        resn += v[10][m/10];
        resn += v[11][m%10];
        resn += v[12][s/10];
        resn += v[13][s%10];
        res[0][resn]++;
      }
    }
  }

  for(int m=1;m<=12;m++){
    for(int d=1;d<=31;d++){
      if(d == 31 && (m == 4 || m == 6 || m == 9 || m == 11)) continue;
      if(m == 2 && d >= 30) continue;
      int resn = 0;
      resn += v[4][m/10];
      resn += v[5][m%10];
      resn += v[6][d/10];
      resn += v[7][d%10];
      if(!(m == 2 && d == 29)) res[1][resn]++;
      res[2][resn]++;
    }
  }

  for(int i=1;i<=2;i++){
    for(int j=0;j<lres;j++){
      for(int k=0;j+k<lres;k++){
        res[2+i][j+k] += res[0][j] * res[i][k];
      }
    }
  }

  for(int y=0;y<=9999;y++){
    int resn = 0;
    resn += v[0][y/1000];
    resn += v[1][(y%1000)/100];
    resn += v[2][((y%1000)%100)/10];
    resn += v[3][((y%1000)%100)%10];
    /*
    if(y == 1171) {
      cout << y/1000 << ": " << v[0][y/1000] << endl;
      cout << (y%1000)/100 << ": " << v[1][(y%1000)/100] << endl;
      cout << ((y%1000)%100)/10 << ": " << v[2][((y%1000)%100)/10] << endl;
      cout << ((y%1000)%100)%10 << ": " << v[3][((y%1000)%100)%10] << endl;
      cout << resn << endl;
    }
    */
    if(n - resn < 0 || n - resn >= lres) continue;
    if(is_uruu(y)) {
      //if(res[4][n-resn] > 0) cout << y << ' ';
      ans += res[4][n-resn];
    }
    else {
      //if(res[3][n-resn] > 0) cout << y << ' ';
      ans += res[3][n-resn];
    }
  }

  cout << ans << endl;
}

int main(){
  while(cin >> n){
    cin >> n2;
    for(int i=0;i<n2;i++){
      cin >> d[i].p >> d[i].q;
    }
    solve();
  }
}

Measured memory_bytes: 1180