Problem: Zeyad wants to commit n crimes in Egypt and not be punished at the end. There are several types of crimes. For example, bribery is a crime but is not considered such when repeated twice. Therefore, bribery is not considered a crime when repeated an even number of times. Speeding is a crime, but is not considered such when repeated a number of times which is a multiple of five.

More specifically, c conditions on crime repetitions are known. Each condition describes the crime type ti and its multiplicity mi. If the number of times Zeyad committed the crime ti is a multiple of mi, Zeyad will not be punished for crime ti. Some crimes may be listed more than once. In this case fulfilling at least one condition for this crime is enough to not be punished for it. Of course, if for certain crime the number of times Zeyad committed it is zero, he is innocent with respect to this crime.

Now Zeyad is interested in a number of ways he can commit exactly n crimes without any punishment.

The order of commiting the crimes matters. More formally, two ways, sequences w1 and w2, of committing n crimes are equal if w1i = w2i, for all 1 ≤ i ≤ n.

Input

The first line contains two integers n and c (0 ≤ n ≤ 1018, 0 ≤ c ≤ 1000) — the number of crimes Zeyad would like to commit and the number of conditions he is aware of.

Then the definitions for c conditions follow. There are 26 types of crimes. Each crime definition consists of crime type — a capital Latin letter — and its multiplicity. 

The multiplicity of each crime is a positive integer number and the product of all multiplicities does not exceed 123. Some conditions may be repeated in the input more than once.

Crime of multiplicity 1 is not yielding any punishment regardless of the number of times it was committed. The strictness of the law is compensated by the fact that it's non-mandatory.

Obviously, if some crime is not listed in the set of conditions, then Zeyad will not consider it, as committing it would unavoidably lead to the punishment.

Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin stream (you may also use the %I64d specificator).

Output

Output the number of different ways Zeyad can commit exactly n crimes with no punishment modulo 12345.

Examples

Input

5 2
A 1
B 2


Output

16


Input

6 3
A 1
B 2
C 3


Output

113


Input

8 3
A 2
A 3
B 2


Output

128

Note

In the first test case, the 16 ways are: AAAAA, AAABB, AABAB, AABBA, ABAAB, ABABA, ABBAA, BAAAB, BAABA, BABAA, BBAAA, ABBBB, BABBB, BBABB, BBBAB, BBBBA.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000000")
using namespace std;
const int INF = 2e18;
vector<vector<int> > iskh;
int mod = 12345;
void matrix_multiply(vector<vector<int> > &a, vector<vector<int> > &b,
                     vector<vector<int> > &c) {
  int n = (int)a.size();
  int q = (int)a[0].size();
  int m = (int)b[0].size();
  vector<int> temp(m, 0);
  c.clear();
  c.resize(n, temp);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < q; k++) {
        c[i][j] += (a[i][k] * b[k][j]) % mod;
        c[i][j] = c[i][j] % mod;
      }
    }
  }
}
void binpow(long long int id, vector<vector<int> > &to) {
  if (id == 1) {
    to = iskh;
    return;
  }
  vector<vector<int> > f, s;
  if (id & 1) {
    binpow(id - 1, f);
    s = iskh;
  } else {
    binpow(id / 2, f);
    s = f;
  }
  matrix_multiply(f, s, to);
}
map<long long int, long long int> good[26];
long long int maxi[26], gone[200][200], dp[200];
vector<int> give(long long int id) {
  id++;
  vector<int> outer;
  for (int i = 0; i < 26; i++) {
    if (!maxi[i]) {
      outer.push_back(0);
      continue;
    }
    long long int take = dp[i] / maxi[i];
    for (long long int j = 0; j < maxi[i]; j++) {
      if (take >= id) {
        outer.push_back(j);
        break;
      }
      id -= take;
    }
  }
  return outer;
}
int make_it() {
  memset(gone, 0, sizeof gone);
  int num = 1;
  for (int i = 26 - 1; i >= 0; i--) {
    if (!maxi[i]) {
      dp[i] = INF;
      continue;
    }
    num *= maxi[i];
    dp[i] = num;
  }
  for (int i = 0; i < num; i++) {
    for (int j = 0; j < num; j++) {
      vector<int> f = give(i);
      vector<int> s = give(j);
      bool can = false;
      int bonus = 0, bal = 0;
      ;
      for (int k = 0; k < 26; k++) {
        if (f[k] != s[k]) bal++;
        if (maxi[k] == 1) bonus++;
        if (f[k] - s[k] <= 1 && f[k] - s[k] >= 0) {
          continue;
        }
        if (f[k] - s[k] > 1) {
          can = true;
        }
        if (!f[k] && maxi[k] != (s[k] + 1)) {
          can = true;
        }
        if (s[k] - f[k] > 0 && f[k]) {
          can = true;
        }
      }
      if (!bal && bonus) {
        bonus--;
        bal++;
      } else
        bonus = 0;
      if (bal != 1) can = true;
      if (!can) {
        gone[j][i] = 1 + bonus;
      }
    }
  }
  return num;
}
long long int glob_ans = 0;
long long int n, c;
long long int gcd(long long int a, long long int b) {
  return b ? gcd(b, a % b) : a;
}
long long int lcm(long long int a, long long int b) {
  return a / gcd(a, b) * b;
}
int main() {
  cin >> n >> c;
  if (!n) {
    cout << 1;
    return 0;
  }
  if (!c) {
    cout << 0;
    return 0;
  }
  memset(maxi, 0, sizeof maxi);
  for (int i = 0; i < c; i++) {
    char nn;
    long long int a;
    cin >> nn >> a;
    good[nn - 'A'][a]++;
    if (maxi[nn - 'A'] == 0) {
      maxi[nn - 'A'] = a;
      continue;
    }
    maxi[nn - 'A'] = lcm(maxi[nn - 'A'], a);
  }
  vector<int> st;
  int num = make_it();
  st.assign(num, 0);
  st[0] = 1;
  vector<int> temp(num, 0);
  iskh.clear();
  iskh.resize(num, temp);
  for (int i = 0; i < num; i++) {
    for (int j = 0; j < num; j++) {
      iskh[i][j] = gone[i][j];
    }
  }
  vector<vector<int> > ans(1), q, out;
  ans[0] = st;
  binpow(n, q);
  matrix_multiply(ans, q, out);
  glob_ans = 0;
  for (int i = 0; i < num; i++) {
    vector<int> f = give(i);
    bool can = false;
    for (int j = 0; j < 26; j++) {
      if (maxi[j] == 0) continue;
      can = true;
      for (map<long long int, long long int>::iterator it = good[j].begin();
           it != good[j].end(); it++) {
        if (f[j] % (it->first) == 0) can = false;
      }
      if (can) break;
    }
    if (!can) glob_ans = (glob_ans + out[0][i]) % mod;
  }
  cout << glob_ans;
}