#include <bits/stdc++.h>
using namespace std;
int n, m;
int k;
bool debug = false;
struct Matrix {
  int n, m;
  long long a[130][130];
  Matrix() {}
  Matrix(int n, int m) : n(n), m(m) {}
  void init(long long num) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) a[i][j] = num;
  }
  Matrix operator*(Matrix b) {
    Matrix o = Matrix(n, b.m);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < b.m; j++) {
        o.a[i][j] = 0;
        for (int k = 0; k < m; k++) {
          o.a[i][j] = (o.a[i][j] + (a[i][k] * b.a[k][j]) % 12345);
        }
      }
    }
    return o;
  }
  Matrix pow(long long p) {
    Matrix o = Matrix(n, m);
    Matrix b = *this;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) o.a[i][j] = (i == j);
    while (p > 0) {
      if (p & 1) o = o * b;
      b = b * b;
      p >>= 1;
    }
    return o;
  }
};
long long N;
set<int> c[26];
int cc[26], cnt;
vector<int> state[130];
map<vector<int>, int> vid;
void dfs(vector<int> v, int deep) {
  if (deep == 26) {
    state[cnt] = v;
    vid[v] = cnt;
    cnt++;
    return;
  }
  for (int i = 0; i < cc[deep]; i++) {
    v[deep] = i;
    dfs(v, deep + 1);
  }
}
int main() {
  cin >> N >> m;
  char x;
  for (int i = 0; i < m; i++) {
    cin >> x >> k;
    if (k != 0) c[x - 'A'].insert(k);
  }
  for (int i = 0; i < 26; i++) {
    cc[i] = 1;
    if (!c[i].empty())
      for (int y : c[i]) cc[i] *= y;
  }
  vector<int> v;
  for (int i = 0; i < 26; i++) v.push_back(0);
  dfs(v, 0);
  Matrix M = Matrix(130, 130);
  M.init(0);
  for (int i = 0; i < cnt; i++) {
    v = state[i];
    for (int j = 0; j < 26; j++)
      if (!c[j].empty()) {
        v[j] = (v[j] + 1) % cc[j];
        M.a[i][vid[v]]++;
        v[j] = (v[j] - 1 + cc[j]) % cc[j];
      }
  }
  Matrix P = M.pow(N);
  long long ans = 0;
  for (int i = 0; i < cnt; i++) {
    v = state[i];
    bool bl = 1;
    for (int j = 0; j < 26; j++) {
      bl = c[j].empty();
      for (int y : c[j]) {
        if (v[j] % y == 0) {
          bl = 1;
          break;
        }
      }
      if (!bl) break;
    }
    if (bl) ans = (ans + P.a[0][vid[v]]) % 12345;
  }
  cout << ans << endl;
  return 0;
}
