C: Digital Clock

story

Aizu Nyan has recently caught a cold. I can't get out of bed because I'm too lazy. The spicy appearance is also cute.

However, Aizu Nyan, who had no choice but to spare time, came up with a way to play with the digital clock under the pillow. The number of glowing bars of a digital clock as shown in the figure below is counted every second, just earnestly.

<image>

After playing this game for several hours, Aizu Nyan noticed that the number of glowing sticks may be the same even if the time is different. I was wondering how many times the number of glowing sticks was the same, and Aizunyan asked you to count it while you were nursing.

For Aizu Nyan who can't get out of bed, you decide to write a program and ask for it.

problem

There is a digital clock like the one shown above that displays the year, month, day, and hour, minute, and second. Answer how many years, months, days, minutes, and seconds the number of glowing bars is exactly N. However, since the digital clock is partially broken, there are some sticks that never shine. Note that even if the display looks the same depending on the non-illuminating stick, if the year, month, day, hour, minute, and second you tried to display are different, they are counted separately.

The shining parts of each number are as shown in the following figure.

<image>

Digital clocks display the year in 0-sized 4 digits (0000 to 9999) and month, day, hour, minute, and second in 0-sized 2 digits.

The month starts on the 1st, April, 6, 9, November until the 30th, and 1, 3, 5, 7, 8, 10, December until the 31st. February is usually up to 28th, but leap years are up to 29th. Here, a leap year is the following year.

1. A year in which the Christian era is divisible by 400 is a leap year.
2. The year is not divisible by 400, but the year divisible by 100 is not a leap year.
3. The year is not divisible by 100, but the year divisible by 4 is a leap year.
4. A year in which the Christian era is not divisible by 4 is not a leap year.



The hours are from 0:00 to 23:00, the minutes are from 0 to 59 minutes, and the seconds are from 0 to 59 seconds. You don't have to think about leap seconds.

Input format

The number of glowing bars N (0 ≤ N ≤ 98) is given in the first line.

The number of broken parts K (0 ≤ K ≤ 98) is given in the second line, and then the information of the broken parts is given in the K line. On the i-th line of the K line, the digit p_i (0 ≤ p_i ≤ 13) and the position q_i (0 ≤ q_i ≤ 6) of the i-th broken bar are given, separated by blanks.

The digit is the ID assigned to each number of the digital clock as shown in the following figure, and is the number assigned from 0 to 13 in order from the highest number.

<image>

The position of the bar is the ID of the bar in each number as shown in the following figure, and is the number assigned the numbers from 0 to 6 in order from the upper bar.

<image>

Output format

Output the total number of years, months, hours, minutes, seconds, and seconds when the number of glowing bars is exactly N on one line.

Input example 1


28
0


Output example 1


1

It is one way of 11:11:11 on November 11, 1111.

Input example 2


28
1
2 0


Output example 2


2

There are two ways: 11:11:11 on November 11, 1171, and 11:11:11 on November 11, 1111.

Input example 3


98
1
3 0


Output example 3


0

Input example 4


60
1
3 0


Output example 4


13362470370





Example

Input

28
0


Output

1
#include<bits/stdc++.h>
#define int long long
using namespace std;

int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool isLeap(int year){return (year%4 == 0 && (year%100 != 0 || year%400 == 0));}

int n, b[14][7], C[100], num[10]={6, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int check(char c, int idx){
  
  int d=c-'0', res=0;
  
  if(b[idx][0]){
    
    if(d==0||d==2||d==3||d==5||d==6||d==7||d==8||d==9) res++;
      
  }
  if(b[idx][1]){

    if(d==0||d==4||d==5||d==6||d==8||d==9) res++;
    
  }
  if(b[idx][2]){
    
    if(d==0||d==1||d==2||d==3||d==4||d==7||d==8||d==9) res++;
    
  }
  if(b[idx][3]){
    
    if(d==2||d==3||d==4||d==5||d==6||d==8||d==9) res++;
    
  }
  if(b[idx][4]){
    
    if(d==0||d==2||d==6||d==8) res++;
    
  }
  if(b[idx][5]){
    
    if(d==0||d==1||d==3||d==4||d==5||d==6||d==7||d==8||d==9) res++;
    
  }
  if(b[idx][6]){
    
    if(d==0||d==2||d==3||d==5||d==6||d==8||d==9) res++;
    
  }

  return res;
}

int cal(int h, int m, int s){
  
  string H=to_string(h);
  string M=to_string(m);
  string S=to_string(s);
  
  if(H.size()==1) H='0'+H;
  if(M.size()==1) M='0'+M;
  if(S.size()==1) S='0'+S;
  
  int res=0;
  
  for(int i=0;i<2;i++) res+=num[H[i]-'0'];
  for(int i=0;i<2;i++) res+=num[M[i]-'0'];
  for(int i=0;i<2;i++) res+=num[S[i]-'0'];
  
  res-=check(H[0], 8);
  res-=check(H[1], 9);
  res-=check(M[0], 10);
  res-=check(M[1], 11);
  res-=check(S[0], 12);
  res-=check(S[1], 13);
  
  return res;
}

int cal2(int y, int m, int d){
  
  string Y=to_string(y);
  string M=to_string(m);
  string D=to_string(d);
  
  while(Y.size()!=4) Y='0'+Y;
  if(M.size()==1) M='0'+M;
  if(D.size()==1) D='0'+D;
  
  int res=0;
  
  for(int i=0;i<4;i++) res+=num[Y[i]-'0'];
  for(int i=0;i<2;i++) res+=num[M[i]-'0'];
  for(int i=0;i<2;i++) res+=num[D[i]-'0'];
  
  res-=check(Y[0], 0);
  res-=check(Y[1], 1);
  res-=check(Y[2], 2);
  res-=check(Y[3], 3);
  res-=check(M[0], 4);
  res-=check(M[1], 5);
  res-=check(D[0], 6);
  res-=check(D[1], 7);
  
  return res;
}

void solve(){
  
  for(int h=0;h<=23;h++)
    
    for(int m=0;m<=59;m++)
      
      for(int s=0;s<=59;s++){
	
	int cnt = cal(h, m, s);
	
	C[cnt]++;
	
      }
  
  int ans = 0;
  
  int y=0, m=1, d=1;
  
  while(y<=9999){
    
    while(m<=12){
      
      while(d<=days[m]+(m==2&&isLeap(y))){
	
	int cnt = cal2(y, m, d);
	
	if(n-cnt>=0) ans+=C[n-cnt];
	
	d++;
      }
      
      d=1;
      
      m++;
    }
    
    m=1;
    
    y++;    
  }
  
  cout<<ans<<endl;
}

signed main(){
  
  cin>>n;
  
  int k;
  cin>>k;
  
  for(int i=0;i<k;i++){
    int p, q;
    cin>>p>>q;
    b[p][q]=1;
  }
  
  solve();
  
  return 0;
}

