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>
using namespace std;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int mod = 12345;
long long n, c;
int place, a[26];
struct matrix {
  long long a[125][125];
  matrix() { memset(a, 0, sizeof(a)); }
} e;
matrix operator*(matrix x, matrix y) {
  matrix res;
  for (int i = 1; i <= place; i++)
    for (int j = 1; j <= place; j++)
      for (int h = 1; h <= place; h++)
        (res.a[i][j] += x.a[i][h] * y.a[h][j]) %= mod;
  return res;
}
matrix power(matrix x, long long y) {
  matrix res;
  for (int i = 1; i <= place; i++) res.a[i][i] = 1;
  while (y) {
    if (y & 1) res = res * x;
    x = x * x;
    y >>= 1;
  }
  return res;
}
struct data {
  int a[26];
  data() { memset(a, 0, sizeof(a)); }
  bool operator<(const data& other) const {
    for (int i = 0; i < 26; i++)
      if (a[i] != other.a[i]) return a[i] < other.a[i];
    return 0;
  }
};
map<data, int> hsh;
vector<int> ans;
vector<int> lim[26];
bool judge(data x) {
  for (int i = 0; i < 26; i++)
    if (a[i]) {
      int flag = 0;
      for (int j = 0; j < lim[i].size(); j++)
        if (x.a[i] % lim[i][j] == 0) flag = 1;
      if (!flag) return 0;
    }
  return 1;
}
void dfs(int step, data now) {
  if (step == 26) {
    hsh[now] = ++place;
    if (judge(now)) ans.push_back(place);
    return;
  }
  if (a[step] == 0) now.a[step] = 0, dfs(step + 1, now);
  for (int i = 0; i < a[step]; i++) {
    now.a[step] = i;
    dfs(step + 1, now);
  }
}
int main() {
  n = read(), c = read();
  for (int i = 1; i <= c; i++) {
    char x;
    scanf("%c", &x);
    int y = read(), z = x - 'A';
    if (a[z] == 0)
      a[z] = y;
    else
      a[z] *= y;
    lim[z].push_back(y);
  }
  if (n == 0) {
    printf("1\n");
    return 0;
  }
  if (c == 0) {
    printf("0\n");
    return 0;
  }
  data st;
  dfs(0, st);
  for (map<data, int>::iterator it = hsh.begin(); it != hsh.end(); it++) {
    data x = it->first;
    int y = it->second;
    for (int i = 0; i < 26; i++)
      if (a[i]) {
        int t = x.a[i];
        x.a[i]++;
        x.a[i] %= a[i];
        e.a[hsh[x]][y]++;
        x.a[i] = t;
      }
  }
  e = power(e, n);
  long long t = 0;
  for (int i = 0; i < ans.size(); i++) (t += e.a[ans[i]][1]) %= mod;
  cout << t << endl;
  return 0;
}