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 long int modulo = 12345;
vector<vector<long long int> > operator*(vector<vector<long long int> > m1,
                                         vector<vector<long long int> > m2) {
  int n = int(m1.size());
  vector<vector<long long int> > r(n, vector<long long int>(n, 0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        r[i][j] = (r[i][j] + m1[i][k] * m2[k][j]) % modulo;
  return r;
}
vector<vector<long long int> > expo(vector<vector<long long int> > m,
                                    long long int e) {
  if (e == 0) {
    int n = int(m.size());
    vector<vector<long long int> > r(n, vector<long long int>(n, 0));
    for (int i = 0; i < n; i++) r[i][i] = 1;
    return r;
  }
  if (e % 2 == 0) return expo(m * m, e / 2);
  return m * expo(m * m, e / 2);
}
vector<long long int> operator*(vector<vector<long long int> > m,
                                vector<long long int> v) {
  int n = int(v.size());
  vector<long long int> r(n, 0);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) r[i] = (r[i] + m[i][j] * v[j]) % modulo;
  return r;
}
vector<int> maximo;
int codigo(vector<int> v) {
  int c = v[0];
  for (int i = 1; i < int(v.size()); i++) {
    c = c * maximo[i] + v[i];
  }
  return c;
}
vector<int> estado(int c) {
  vector<int> v;
  for (int i = int(maximo.size()) - 1; i >= 0; i--) {
    v.push_back(c % maximo[i]);
    c /= maximo[i];
  }
  reverse(v.begin(), v.end());
  return v;
}
bool divisible(int x, set<int> s) {
  for (set<int>::iterator it = s.begin(); it != s.end(); it++)
    if (x % (*it) == 0) return true;
  return false;
}
int producto(set<int> s) {
  int p = 1;
  for (set<int>::iterator it = s.begin(); it != s.end(); it++) p *= (*it);
  return p;
}
int main() {
  long long int n, cuantos;
  cin >> n >> cuantos;
  map<char, set<int> > m;
  for (int i = 0; i < cuantos; i++) {
    char c;
    int val;
    cin >> c >> val;
    m[c].insert(val);
  }
  vector<set<int> > v;
  for (map<char, set<int> >::iterator it = m.begin(); it != m.end(); it++)
    v.push_back(it->second);
  maximo = vector<int>(int(v.size()));
  int numestados = 1;
  for (int i = 0; i < int(v.size()); i++) {
    maximo[i] = producto(v[i]);
    numestados *= maximo[i];
  }
  vector<int> aceptador(numestados, 1);
  vector<vector<long long int> > matriz(numestados,
                                        vector<long long int>(numestados, 0));
  for (int i = 0; i < numestados; i++) {
    vector<int> e = estado(i);
    vector<int> nexte = e;
    for (int j = 0; j < int(maximo.size()); j++) {
      if (not divisible(e[j], v[j])) aceptador[i] = 0;
      nexte[j] = (nexte[j] + 1) % maximo[j];
      int c = codigo(nexte);
      matriz[c][i]++;
      nexte[j] = e[j];
    }
  }
  vector<long long int> ini(numestados, 0);
  ini[0] = 1;
  vector<long long int> fin = expo(matriz, n) * ini;
  long long int suma = 0;
  for (int i = 0; i < numestados; i++) {
    if (aceptador[i]) suma = (suma + fin[i]) % modulo;
  }
  cout << suma << endl;
}