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;
int smemo[150][4000];
int M[30];
vector<long long> H;
set<int> mset[30];
int B[4000][30];
int numB;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int cur[30];
void makebset(int i) {
  if (i == 26) {
    for (int j = 0; j < 26; j++) B[numB][j] = cur[j];
    numB++;
    return;
  }
  for (int k = 0; k < M[i]; k++) {
    cur[i] = k;
    makebset(i + 1);
  }
  if (M[i] == 0) cur[i] = 0, makebset(i + 1);
}
bool equal(int* P, int* Q) {
  for (int i = 0; i < 26; i++)
    if (P[i] != Q[i]) return false;
  return true;
}
int subtract(int* P, int* Q) {
  int ans[26];
  for (int i = 0; i < 26; i++) {
    ans[i] = (P[i] - Q[i] + M[i]);
    if (M[i] > 0) ans[i] %= M[i];
  }
  for (int k = 0; k < numB; k++) {
    if (equal(ans, B[k])) return k;
  }
  throw "BAD!";
}
bool validone(int i, int b) {
  for (int j = 0; j < 26; j++) {
    if (B[b][j] == 0) continue;
    int x = B[b][j];
    if ((j == i) && (x != 1)) return false;
    if ((j != i) && (x != 0)) return false;
  }
  if (b == 0) return M[i] == 1;
  return true;
}
void makehset(long long N) {
  queue<long long> Q;
  Q.push(N);
  while (!Q.empty()) {
    long long i = Q.front();
    Q.pop();
    if (i == 0) continue;
    if (find(H.begin(), H.end(), i) != H.end()) continue;
    H.push_back(i);
    sort(H.begin(), H.end());
    Q.push(i / 2);
    Q.push(i - i / 2);
  }
  int S = H.size();
}
int S(long long n, int b) {
  int i = find(H.begin(), H.end(), n) - H.begin();
  int j = b;
  assert(n != 0);
  assert(H[i] == n);
  int& ans = smemo[i][j];
  if (ans != -1) return ans;
  if (n == 1) {
    ans = 0;
    for (int i = 0; i < 26; i++) {
      if (M[i] == 0) continue;
      if (validone(i, b)) ans++;
    }
    return ans;
  }
  ans = 0;
  for (int k = 0; k < numB; k++) {
    long long c = subtract(B[j], B[k]);
    ans += S(n / 2, k) * S(n - n / 2, c);
    ans %= 12345;
  }
  return ans;
}
bool countable(int b) {
  for (int j = 0; j < 26; j++) {
    if (M[j] == 0) continue;
    int val = (B[b][j] % M[j]);
    bool found = false;
    for (set<int>::iterator it = (mset[j]).begin(); it != (mset[j]).end();
         it++) {
      if (val == (*it)) found = true;
      if ((*it) == 0) continue;
      if ((val % (*it)) == 0) found = true;
    }
    if (!found) return false;
  }
  return true;
}
int main() {
  long long N;
  int C;
  cin >> N >> C;
  memset(M, 0, sizeof(M));
  for (int i = 0; i < C; i++) {
    string x;
    int v;
    cin >> x >> v;
    int& m = M[x[0] - 'A'];
    if (m == 0)
      m = v;
    else
      m = lcm(m, v);
    mset[x[0] - 'A'].insert(v);
  }
  for (int i = 0; i < 26; i++) mset[i].insert(0);
  memset(smemo, -1, sizeof(smemo));
  if (N == 0) {
    cout << 1 << endl;
    return 0;
  }
  makehset(N);
  numB = 0;
  makebset(0);
  int ans = 0;
  for (int k = 0; k < numB; k++) {
    if (!countable(k)) continue;
    ans += S(N, k);
    ans %= 12345;
  }
  cout << ans << endl;
}