/*
 * 2671.cc: Digital Clock
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int M = 14;
const int L = 7;
const int LBITS = 1 << L;
const int ML = M * L;

const int dbits[10] = {
  0x77, // '0' = 0,1,2,4,5,6 ("111 0111")
  0x24, // '1' = 2,5 ("010 0100")
  0x5d, // '2' = 0,2,3,4,6 ("101 1101")
  0x6d, // '3' = 0,2,3,5,6 ("110 1101")
  0x2e, // '4' = 1,2,3,5 ("010 1110")
  0x6b, // '5' = 0,1,3,5,6 ("110 1011")
  0x7b, // '6' = 0,1,3,4,5,6 ("111 1011")
  0x25, // '7' = 0,2,5 ("010 0101")
  0x7f, // '8' = 0,1,2,3,4,5,6 ("111 1111")
  0x6f, // '9' = 0,1,2,3,5,6 ("110 1111")
};

const int dom[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 29};

/* typedef */

typedef long long ll;

/* global variables */

int lns[LBITS], fbits[M];
ll dpy[10000][ML + 1], dpm[14][ML + 1], dpd[32][ML + 1];
ll dph[24][ML + 1], dpmn[60][ML + 1], dps[60][ML + 1];

/* subroutines */

inline int lnums(int i, int d) { return lns[dbits[d] & fbits[i]]; }

inline int leap(int y) {
  return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));
}

/* main */

int main() {
  for (int bits = 0; bits < LBITS; bits++) {
    lns[bits] = 0;
    for (int i = 0; i < L; i++)
      if (bits & (1 << i)) lns[bits]++;
  }
  
  for (int i = 0; i < M; i++) fbits[i] = LBITS - 1;
  //for (int i = 0; i < 10; i++) printf("%d=%d\n", i, lnums(0, i));

  int n, k;
  cin >> n >> k;

  for (int i = 0; i < k; i++) {
    int pi, qi;
    cin >> pi >> qi;
    fbits[pi] &= ~(1 << qi);
    //printf("fbits[%d] = %x\n", pi, fbits[pi]);
  }

  // sec
  for (int s = 0; s < 60; s++) {
    int d13 = s % 10;
    int d12 = s / 10;
    int l = lnums(12, d12) + lnums(13, d13);
    dps[s][l]++;
  }
  //printf("dps[11][4]=%lld\n", dps[11][4]);
  
  // min
  for (int mn = 0; mn < 60; mn++) {
    int d11 = mn % 10;
    int d10 = mn / 10;
    int l = lnums(10, d10) + lnums(11, d11);

    for (int s = 0; s < 60; s++)
      for (int sl = 0; sl <= ML; sl++)
	if (dps[s][sl] > 0)
	  dpmn[mn][l + sl] += dps[s][sl];
  }
  //printf("dpmn[11][8]=%lld\n", dpmn[11][8]);

  // hour
  for (int h = 0; h < 24; h++) {
    int d9 = h % 10;
    int d8 = h / 10;
    int l = lnums(8, d8) + lnums(9, d9);

    for (int mn = 0; mn < 60; mn++)
      for (int ml = 0; ml <= ML; ml++)
	if (dpmn[mn][ml] > 0)
	  dph[h][l + ml] += dpmn[mn][ml];
  }
  //printf("dph[11][12]=%lld\n", dph[11][12]);

  // day
  for (int d = 1; d <= 31; d++) {
    int d7 = d % 10;
    int d6 = d / 10;
    int l = lnums(6, d6) + lnums(7, d7);

    for (int h = 0; h < 24; h++)
      for (int hl = 0; hl <= ML; hl++)
	if (dph[h][hl] > 0)
	  dpd[d][l + hl] += dph[h][hl];
  }
  //printf("dpd[11][16]=%lld\n", dpd[11][16]);

  // month
  for (int m = 1; m <= 12; m++) {
    int d5 = m % 10;
    int d4 = m / 10;
    int l = lnums(4, d4) + lnums(5, d5);

    for (int d = 1; d <= dom[m]; d++)
      for (int dl = 0; dl <= ML; dl++)
	if (dpd[d][dl] > 0)
	  dpm[m][l + dl] += dpd[d][dl];
  }

  // m = 13 -> Feb of leap year
  int l13 = lnums(4, 0) + lnums(5, 2);
  for (int d = 1; d <= dom[13]; d++)
    for (int dl = 0; dl <= ML; dl++)
      if (dpd[d][dl] > 0)
	dpm[13][l13 + dl] += dpd[d][dl];
  //printf("dpm[11][20]=%lld\n", dpm[11][20]);

  for (int y = 0; y < 10000; y++) {
    int d3 = y % 10;
    int d2 = (y / 10) % 10;
    int d1 = (y / 100) % 10;
    int d0 = y / 1000;
    int l = lnums(0, d0) + lnums(1, d1) + lnums(2, d2) + lnums(3, d3);
    bool lp = leap(y);

    for (int m = 1; m <= 12; m++) {
      int m0 = (lp && m == 2) ? 13 : m;
      for (int ml = 0; ml <= ML; ml++)
	if (dpm[m0][ml] > 0)
	  dpy[y][l + ml] += dpm[m0][ml];
    }
  }

  ll sum = 0;
  for (int y = 0; y < 10000; y++) sum += dpy[y][n];
  printf("%lld\n", sum);

  return 0;
}

Measured memory_bytes: 9032