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;
long base;
vector<long> vec[2000];
long can[200][200];
long lcm[200];
long long n, k;
vector<vector<long> > mult(vector<vector<long> > a, vector<vector<long> > b) {
  vector<vector<long> > v;
  v.resize(a.size());
  for (int i = 0; i < v.size(); i++) v[i].resize(a[0].size());
  for (int i = 0; i < v.size(); i++)
    for (int j = 0; j < v.size(); j++) v[i][j] = 0;
  for (int k = 0; k < v.size(); k++)
    for (int i = 0; i < v.size(); i++)
      for (int j = 0; j < v.size(); j++)
        v[i][j] += a[i][k] * b[k][j], v[i][j] %= base;
  return v;
}
vector<vector<long> > one(long sz) {
  vector<vector<long> > v;
  v.resize(sz);
  for (int i = 0; i < v.size(); i++) v[i].resize(sz);
  for (int i = 0; i < v.size(); i++)
    for (int j = 0; j < v.size(); j++) v[i][j] = (i == j);
  return v;
}
vector<vector<long> > pw(vector<vector<long> > a, long long b) {
  if (b == 0) return one(a.size());
  if (b % 2) return mult(a, pw(a, b - 1));
  return pw(mult(a, a), b / 2);
}
vector<long> decomp(long state) {
  vector<long> res;
  for (int i = 25; i >= 0; --i)
    if (vec[i].size())
      res.push_back(state % lcm[i]), state /= lcm[i];
    else
      res.push_back(1);
  reverse(res.begin(), res.end());
  return res;
}
long mov(long a, long b) {
  vector<long> v1, v2;
  v1 = decomp(a);
  v2 = decomp(b);
  long free = 0;
  long found = 0;
  for (int i = 0; i < 26; i++) {
    if (vec[i].size() == 0) continue;
    if ((v1[i] + 1) % lcm[i] == v2[i]) {
      if (lcm[i] == 1)
        free++;
      else if (found == 0)
        found = 1;
      else
        return 0;
    } else if (v1[i] != v2[i])
      return 0;
  }
  if (found) return 1;
  return free;
}
string st;
long val;
long total;
long gcd(long a, long b) {
  while (a && b) a > b ? a %= b : b %= a;
  return a + b;
}
vector<vector<long> > res;
bool bad(long x, long ps) {
  for (int i = 0; i < vec[ps].size(); i++)
    if (x % vec[ps][i] == 0) return false;
  return true;
}
bool check(vector<long> v) {
  for (long i = 0; i < 26; i++) {
    if (vec[i].size() == 0) continue;
    if (bad(v[i], i)) return false;
  }
  return true;
}
void show(vector<vector<long> > v) {
  for (int i = 0; i < v.size(); i++) {
    for (int j = 0; j < v[i].size(); j++) cout << v[i][j] << " ";
    cout << endl;
  }
  cout << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  base = 12345;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    cin >> st >> val;
    vec[st[0] - 'A'].push_back(val);
    can[st[0] - 'A'][val] = 1;
  }
  total = 1;
  for (int i = 0; i < 26; i++)
    if (vec[i].size()) {
      lcm[i] = 1;
      for (int j = 0; j < vec[i].size(); j++) {
        long q = vec[i][j];
        lcm[i] = lcm[i] * q / gcd(lcm[i], q);
      }
      total *= lcm[i];
    }
  res.resize(total);
  for (int i = 0; i < res.size(); i++) res[i].resize(total);
  for (int i = 0; i < total; i++)
    for (int j = 0; j < total; j++) res[i][j] = mov(i, j);
  res = pw(res, n);
  long ans = 0;
  for (int i = 0; i < total; i++) {
    vector<long> v = decomp(i);
    if (check(v)) ans += res[0][i], ans %= base;
  }
  cout << ans << endl;
  cin.get();
  cin.get();
  return 0;
}