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:102400000,102400000")
using namespace std;
const long long INF = 12345;
const int N = 2e2 + 10;
class Matrix {
  int a[N][N];
  int n;

 public:
  Matrix ones(int n) {
    Matrix ret(n);
    for (int i = 0; i < n; i++) ret[i][i] = 1;
    return ret;
  }
  Matrix(int n = 0) { init(n); }
  void init(int n) {
    this->n = n;
    memset((a), (0), sizeof(a));
  }
  int *operator[](int i) { return a[i]; }
  Matrix operator+(Matrix &m) {
    Matrix ret(*this);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        a[i][j] += m[i][j];
        if (a[i][j] >= INF) a[i][j] -= INF;
      }
    }
  }
  Matrix operator*(Matrix &m) {
    Matrix ret(n);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          ret[i][j] = (ret[i][j] + 1LL * a[i][k] * m[k][j]) % INF;
        }
      }
    }
    return ret;
  }
  Matrix operator^(long long b) {
    Matrix ret = ones(n);
    Matrix a = *this;
    while (b) {
      if (b & 1) ret = ret * a;
      a = a * a;
      b >>= 1;
    }
    return ret;
  }
};
int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }
int v[N];
int a[N];
int w[N];
int flag[N][N];
int key[N];
int main() {
  map<char, int> mc;
  long long n;
  int m;
  cin >> n >> m;
  vector<pair<char, int>> vp;
  int tot = 1;
  for (int i = 0; i < m; i++) {
    char c;
    int x;
    cin >> c >> x;
    mc[c] = 1;
    flag[c][x] = 1;
  }
  for (int i = 'A'; i <= 'Z'; i++) {
    int cur = 1;
    for (int j = 1; j < N; j++) {
      if (flag[i][j] == 0) continue;
      for (int k = j + j; k < N; k += j) flag[i][k] = 0;
      cur *= j;
      if (vp.empty() || vp.back().first != i)
        vp.push_back({i, cur});
      else
        vp.back().second = cur;
      tot *= j;
    }
    for (int j = 1; j < N; j++) {
      if (flag[i][j] == 0) continue;
      for (int k = j; k < N; k += j) flag[i][k] = 1;
      flag[i][0] = 1;
    }
  }
  int sz = 0;
  for (auto &p : vp) v[sz++] = p.second;
  v[sz] = 1;
  w[0] = 1;
  for (int i = 1; i < sz; i++) w[i] = v[i - 1] * w[i - 1];
  vector<int> ans;
  for (int i = 0; i < tot; i++) {
    memset((key), (0), sizeof(key));
    int cur = i;
    for (int j = 0; j < sz; j++) {
      int x = cur % v[j];
      cur /= v[j];
      if (flag[vp[j].first][x]) key[vp[j].first] = 1;
    }
    if (count(key, key + N, 1) == mc.size()) ans.push_back(i);
  }
  Matrix b(tot);
  for (int i = 0; i < tot; i++) {
    int cur = i;
    for (int j = 0; j < sz; j++) {
      int x = cur % v[j];
      cur /= v[j];
      int y = (x + 1) % v[j];
      b[i][i - (x - y) * w[j]]++;
    }
  }
  b = b ^ n;
  int ret = 0;
  for (auto &x : ans) {
    ret += b[0][x];
    if (ret >= INF) ret -= INF;
  }
  printf("%d\n", ret);
  return 0;
}