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:60000000")
using namespace std;
template <class T>
T sqr(T x) {
  return x * x;
}
double const pi = 3.1415926535897932384626433832795;
int const inf = (int)1e9 + 1;
long long const inf64 = (long long)4e18 + 1;
const string name = "d";
const int NMAX = 128;
const int P = 12345;
long long n;
int m, K, k[256], zn[NMAX], used[NMAX];
char ch[NMAX];
vector<int> sdfjh[NMAX];
vector<pair<char, int> > value[NMAX];
map<vector<pair<char, int> >, int> num;
int res[NMAX][NMAX], a[NMAX][NMAX];
long long q[NMAX][NMAX];
int gcd(int a, int b) {
  if (a * b == 0)
    return a + b;
  else
    return gcd(b % a, a);
}
vector<pair<char, int> > now;
void rec(int x) {
  if (x == 256) {
    num[now] = K;
    value[K] = now;
    K++;
    return;
  }
  if (!used[x])
    rec(x + 1);
  else {
    for (int i = 0; i < (int)k[x]; i++) {
      now.push_back(make_pair((char)x, i));
      rec(x + 1);
      now.pop_back();
    }
  }
}
int main() {
  for (int i = 0; i < (int)256; i++) {
    used[i] = 0;
    k[i] = 1;
  }
  cin >> n >> m;
  if (n == 0) {
    cout << 1 << endl;
    return 0;
  }
  if (m == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < (int)m; i++) {
    scanf(" %c%d", &ch[i], &zn[i]);
    sdfjh[ch[i]].push_back(zn[i]);
    k[ch[i]] = k[ch[i]] * zn[i] / gcd(k[ch[i]], zn[i]);
    used[ch[i]] = 1;
  }
  K = 0;
  now.clear();
  rec(0);
  m = value[0].size();
  memset(a, 0, sizeof(a));
  for (int i = 0; i < (int)K; i++)
    for (int j = 0; j < (int)m; j++) {
      vector<pair<char, int> > tmp = value[i];
      tmp[j].second = (tmp[j].second + 1) % k[tmp[j].first];
      a[i][num[tmp]]++;
    }
  memset(res, 0, sizeof(res));
  for (int i = 0; i < (int)K; i++) res[i][i] = 1;
  while (n > 0) {
    if (n & 1) {
      memset(q, 0, sizeof(q));
      for (int i = 0; i < (int)K; i++)
        for (int j = 0; j < (int)K; j++)
          for (int f = 0; f < (int)K; f++) q[i][j] += res[i][f] * a[f][j];
      for (int i = 0; i < (int)K; i++)
        for (int j = 0; j < (int)K; j++) res[i][j] = q[i][j] % P;
    }
    memset(q, 0, sizeof(q));
    for (int i = 0; i < (int)K; i++)
      for (int j = 0; j < (int)K; j++)
        for (int f = 0; f < (int)K; f++) q[i][j] += a[i][f] * a[f][j];
    for (int i = 0; i < (int)K; i++)
      for (int j = 0; j < (int)K; j++) a[i][j] = q[i][j] % P;
    n >>= 1;
  }
  int ans = 0;
  for (int i = 0; i < (int)K; i++) {
    int ok = 1;
    for (int j = 0; j < (int)m; j++) {
      int tmp = 0;
      char c = value[0][j].first;
      for (int f = 0; f < (int)sdfjh[c].size(); f++)
        if (value[i][j].second % sdfjh[c][f] == 0) tmp = 1;
      ok &= tmp;
    }
    ans = (ans + ok * res[0][i]) % P;
  }
  cout << ans << endl;
  return 0;
}