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 JAVA):

Solution:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: piyushd
 * Date: 3/29/11
 * Time: 7:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class TaskD {

    int[] product;
    int[][] I;
    final int MOD = 12345;

    int[] decode(int code) {
        int[] a = new int[26];
        for(int i = 0; i < 26; i++) {
            a[i] = code % product[i];
            code /= product[i];
        }
        return a;
    }

    int encode(int[] a) {
        int code = 0;
        for(int i = 25; i >= 0; i--) {
            code = code * product[i] + a[i];
        }
        return code;
    }

    int[][] matrixMult(int[][] a, int[][] b) {
        int[][] c = new int[a.length][a.length];
        for(int k = 0; k < a.length; k++) {
            for(int i = 0; i < a.length; i++) {
                for(int j = 0; j < a.length; j++) {
                    c[i][j] += a[i][k] * b[k][j];
                    c[i][j] %= MOD;
                }
            }
        }
        return c;
    }

    int[][] matrixExponentiation(int[][] a, long N) {
        if(N == 0) return this.I;

        if(N % 2 == 0) {
            int[][] x = matrixExponentiation(a, N / 2);
            return matrixMult(x, x);
        }

        return matrixMult(a, matrixExponentiation(a, N - 1));
    }

    void run() {
        long N = nextLong();
        int c = nextInt();

        ArrayList<Integer>[] multiplicities = new ArrayList[26];
        for(int i = 0; i < multiplicities.length; i++) multiplicities[i] = new ArrayList<Integer>();

        for(int i = 0; i < c; i++) {
            String character = next();
            int multiple = nextInt();
            multiplicities[character.charAt(0) - 'A'].add(multiple);
        }


        int M = 1;
        this.product = new int[26];
        for (int i = 0; i < 26; i++) {
            product[i] = 1;
            for (int x : multiplicities[i])
                product[i] *= x;
            M *= product[i];
        }

        int[][] a = new int[M][M];
        for(int i = 0; i < M; i++) {
            for(int j = 0; j < 26; j++) if(multiplicities[j].size() > 0) {
                int[] d = decode(i);
                d[j]++;
                d[j] %= product[j];
                int code = encode(d);
                a[i][code]++;
            }
        }

        this.I = new int[M][M];
        for(int i = 0; i < M; i++) I[i][i] = 1;

        int[][] na = matrixExponentiation(a, N);

        int ans = 0;
        for(int code = 0; code < M; code++) {
            int[] d = decode(code);

            boolean ok = true;
            for(int j = 0; j < 26 && ok; j++) if(multiplicities[j].size() > 0) {
                boolean got = false;
                for(int x : multiplicities[j])
                    if(d[j] % x == 0) got = true;
                ok = got;
            }

            if(ok) {
                ans = (ans + na[0][code]) % MOD;
            }
        }

        System.out.println(ans);
    }

    int nextInt(){
        try{
            int c = System.in.read();
            if(c == -1) return c;
            while(c != '-' && (c < '0' || '9' < c)){
                c = System.in.read();
                if(c == -1) return c;
            }
            if(c == '-') return -nextInt();
            int res = 0;
            do{
                res *= 10;
                res += c - '0';
                c = System.in.read();
            }while('0' <= c && c <= '9');
            return res;
        }catch(Exception e){
            return -1;
        }
    }

    long nextLong(){
        try{
            int c = System.in.read();
            if(c == -1) return -1;
            while(c != '-' && (c < '0' || '9' < c)){
                c = System.in.read();
                if(c == -1) return -1;
            }
            if(c == '-') return -nextLong();
            long res = 0;
            do{
                res *= 10;
                res += c-'0';
                c = System.in.read();
            }while('0' <= c && c <= '9');
            return res;
        }catch(Exception e){
            return -1;
        }
    }

    double nextDouble(){
        return Double.parseDouble(next());
    }

    String next(){
        try{
            StringBuilder res = new StringBuilder("");
            int c = System.in.read();
            while(Character.isWhitespace(c))
                c = System.in.read();
            do{
                res.append((char)c);
            }while(!Character.isWhitespace(c=System.in.read()));
            return res.toString();
        }catch(Exception e){
            return null;
        }
    }

    String nextLine(){
        try{
            StringBuilder res = new StringBuilder("");
            int c = System.in.read();
            while(c == '\r' || c == '\n')
                c = System.in.read();
            do{
                res.append((char)c);
                c = System.in.read();
            }while(c != '\r' && c != '\n');
            return res.toString();
        }catch(Exception e){
            return null;
        }
    }

    public static void main(String[] args){
        new TaskD().run();
    }
}