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 GCC optimize("O3,Ofast,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx,tune=native")
using namespace std;
const long long INF = 1e9 + 10;
const long long BINF = 1e18 + 10;
vector<long long> one;
long long lim = 1;
map<vector<long long>, long long> gt;
vector<long long> f[210];
long long md[26];
long long sum(vector<long long> a, vector<long long> b) {
  vector<long long> c;
  for (long long i = 0; i < ((long long)a.size()); i++) {
    c.push_back((a[i] + b[i]) % md[i]);
  }
  return gt[c];
}
vector<long long> mult(vector<long long> a, vector<long long> b,
                       long long mod) {
  vector<long long> res(((long long)a.size()));
  for (long long i = 0; i < lim; i++) {
    for (long long j = 0; j < lim; j++) {
      long long to = sum(f[i], f[j]);
      res[to] = (res[to] + a[i] * b[j]) % mod;
    }
  }
  return res;
}
vector<long long> b_pow(long long n, long long mod) {
  if (n == 1) {
    return one;
  }
  if (n & 1) {
    return mult(one, b_pow(n - 1, mod), mod);
  }
  vector<long long> mt = b_pow(n / 2, mod);
  return mult(mt, mt, mod);
}
vector<long long> have(26);
vector<vector<long long>> lol(26);
long long ff = 0;
void gen(long long ind, vector<long long> &cur) {
  if (ind == 26) {
    f[ff] = cur;
    gt[cur] = ff;
    one.push_back(0);
    ff++;
    return;
  }
  if (lol[ind].empty()) {
    gen(ind + 1, cur);
  }
  for (long long i = 0; i < have[ind]; i++) {
    cur.push_back(i);
    gen(ind + 1, cur);
    cur.pop_back();
  }
}
bool correct(vector<long long> x) {
  long long pnt = 0;
  for (auto it : x) {
    while (lol[pnt].empty()) {
      pnt++;
    }
    bool ok = false;
    for (auto it2 : lol[pnt]) {
      if (it % it2 == 0) {
        ok = true;
        break;
      }
    }
    if (!ok) {
      return false;
    }
    pnt++;
  }
  return true;
}
void source() {
  long long n, m;
  cin >> n >> m;
  if (n == 0) {
    cout << 1;
    exit(0);
  }
  if (m == 0) {
    cout << 0;
    exit(0);
  }
  for (long long i = 0; i < m; i++) {
    char c;
    long long val;
    cin >> c >> val;
    lim *= val;
    if (!have[c - 'A']) {
      have[c - 'A'] = val;
    } else {
      have[c - 'A'] *= val;
    }
    lol[c - 'A'].push_back(val);
  }
  long long cc = 0;
  for (long long i = 0; i < 26; i++) {
    if (!lol[i].empty()) {
      md[cc++] = have[i];
    }
  }
  vector<long long> em = {};
  gen(0, em);
  for (long long i = 0; i < cc; i++) {
    vector<long long> curr(cc);
    curr[i] = 1 % md[i];
    one[gt[curr]]++;
  }
  vector<long long> res = b_pow(n, 12345);
  long long ans = 0;
  for (long long i = 0; i < ff; i++) {
    if (correct(f[i])) {
      ans = (ans + res[i]) % 12345;
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  source();
  return 0;
}