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:16777216")
using namespace std;
int a, b, c, d, i, j, m, k;
long long n;
char str[101];
bool one[101];
vector<int> cur[101], num[101];
vector<int> tot;
int mas[126][126], ans[126][126], tmp[126][126], tr[126][126];
map<vector<int>, int> q;
vector<int> cv[1001];
vector<int> now;
void rec(int v) {
  if (v >= (int)((tot).size())) {
    q[now] = k++;
    cv[k - 1] = now;
    return;
  }
  for (int _n((tot[v]) - 1), i(0); i <= _n; i++) {
    now.push_back(i);
    rec(v + 1);
    now.pop_back();
  }
}
inline void mul(int a[][126], int b[][126]) {
  for (int _n((k)-1), i(0); i <= _n; i++)
    for (int _n((k)-1), j(0); j <= _n; j++) tr[i][j] = b[j][i];
  for (int _n((k)-1), i(0); i <= _n; i++) {
    for (int _n((k)-1), j(0); j <= _n; j++) {
      long long t = 0;
      for (int _n((k)-1), z(0); z <= _n; z++) t += a[i][z] * tr[j][z];
      tmp[i][j] = t % 12345;
    }
  }
  for (int _n((k)-1), i(0); i <= _n; i++)
    for (int _n((k)-1), j(0); j <= _n; j++) a[i][j] = tmp[i][j];
}
int main() {
  scanf("%I64d%d", &n, &m);
  for (int _n((m)-1), i(0); i <= _n; i++) {
    scanf("%s%d", str, &a);
    if (a == 1)
      one[str[0] - 'A'] = 1;
    else
      cur[str[0] - 'A'].push_back(a);
  }
  for (int _n((26) - 1), i(0); i <= _n; i++) {
    if (one[i])
      ++c;
    else {
      for (int _n(((int)((cur[i]).size())) - 1), j(0); j <= _n; j++) {
        tot.push_back(cur[i][j]);
        num[i].push_back((int)tot.size() - 1);
      }
    }
  }
  rec(0);
  for (int _n((k)-1), i(0); i <= _n; i++) {
    for (int _n((26) - 1), j(0); j <= _n; j++) {
      if (one[j]) {
        ++mas[i][i];
      } else if ((int)((num[j]).size())) {
        vector<int> cur = cv[i];
        for (int _n(((int)((num[j]).size())) - 1), z(0); z <= _n; z++)
          cur[num[j][z]] = (cur[num[j][z]] + 1) % tot[num[j][z]];
        mas[i][q[cur]] = 1;
      }
    }
  }
  for (int _n((k)-1), i(0); i <= _n; i++) ans[i][i] = 1;
  while (n) {
    if (n % 2) mul(ans, mas);
    mul(mas, mas);
    n /= 2;
  }
  int res = 0;
  for (int _n((k)-1), i(0); i <= _n; i++) {
    vector<int> cur = cv[i];
    bool good = 1;
    for (int _n((26) - 1), j(0); j <= _n; j++) {
      if (one[j] || !(int)((num[j]).size())) continue;
      bool ok = 0;
      for (int _n(((int)((num[j]).size())) - 1), z(0); z <= _n; z++) {
        if (!cur[num[j][z]]) {
          ok = 1;
          break;
        }
      }
      if (!ok) {
        good = 0;
        break;
      }
    }
    if (good) res += ans[0][i];
  }
  printf("%d\n", res % 12345);
}