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;
const int P = 12345;
int c, tot, val[26], a[26];
int64_t n;
vector<int> g[26];
vector<vector<int>> sta;
map<vector<int>, int> ID;
void dfs(int d) {
  if (d == 26) {
    auto u = vector<int>(a, a + 26);
    ID[u] = tot++;
    sta.push_back(u);
    return;
  }
  if (!val[d]) return dfs(d + 1);
  for (int t = 0; t < val[d]; ++t) {
    a[d] = t;
    dfs(d + 1);
  }
}
struct Mat {
  int v[125][125];
  Mat() { memset(v, 0, sizeof v); }
  Mat operator*(const Mat& b) const {
    Mat c;
    for (int k = 0; k < tot; ++k)
      for (int i = 0; i < tot; ++i)
        for (int j = 0; j < tot; ++j)
          c.v[i][j] = ((int64_t)v[i][k] * b.v[k][j] + c.v[i][j]) % P;
    return c;
  }
  Mat operator^(int64_t nn) {
    Mat b, a = *this;
    for (int i = 0; i < tot; ++i) b.v[i][i] = 1;
    while (nn) {
      if (nn & 1) b = b * a;
      nn >>= 1, a = a * a;
    }
    return b;
  }
} f;
int main() {
  scanf("%lld%d", &n, &c);
  while (c--) {
    char x;
    int y;
    scanf(" %c%d", &x, &y);
    g[x - 'A'].push_back(y);
  }
  for (int i = 0; i < 26; ++i)
    if (g[i].size()) {
      sort(g[i].begin(), g[i].end());
      vector<int> tmp;
      val[i] = 1;
      for (int x = 0; x < g[i].size(); ++x)
        if (g[i][x] != -1) {
          val[i] *= g[i][x];
          tmp.push_back(g[i][x]);
          for (int y = x + 1; y < g[i].size(); ++y) {
            if (g[i][y] % g[i][x] == 0) g[i][y] = -1;
          }
        }
      swap(g[i], tmp);
    }
  dfs(0);
  for (int d = 0; d < sta.size(); ++d) {
    for (int i = 0; i < 26; ++i)
      if (g[i].size()) {
        auto u = sta[d];
        u[i] = (u[i] + 1) % val[i];
        if (!ID.count(u)) throw;
        ++f.v[ID[u]][d];
      }
  }
  f = f ^ n;
  int ans = 0;
  auto chk = [&](int d, int w) {
    for (auto& t : g[d])
      if (w % t == 0) return 1;
    return 0;
  };
  for (int i = 0; i < tot; ++i) {
    auto u = sta[i];
    int ok = 1;
    for (int d = 0; d < 26; ++d)
      if (u[d]) {
        if (!chk(d, u[d])) ok = 0;
      }
    if (ok) ans = (ans + f.v[i][0]) % P;
  }
  printf("%d\n", ans);
}