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:
/**
 * Created by IntelliJ IDEA.
 * User: Taras_Brzezinsky
 * Date: 8/24/11
 * Time: 8:54 PM
 * To change this template use File | Settings | File Templates.
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.util.Arrays;

public class TaskD extends Thread {

    public TaskD() {
        this.input = new BufferedReader(new InputStreamReader(System.in));
        this.output = new PrintWriter(System.out);
        this.setPriority(Thread.MAX_PRIORITY);
    }

    private int getIndex(int[][] data, int[] values) {
        for (int i = 0; i < data.length; ++i) {
            if (Arrays.equals(data[i], values)) {
                return i;
            }
        }
        throw new IllegalArgumentException();
    }

    private void generate(int index) {
        if (index == helper.length) {
            states[SIZE++] = helper.clone();
        } else {
            for (int value = 0; value < v[index]; ++value) {
                helper[index] = value;
                generate(index + 1);
            }
        }
    }

    private int[][] pow(int[][] src, long power) {
        int[][] result = new int[src.length][src.length];
        for (int i = 0; i < result.length; ++i) {
            result[i][i] = 1;
        }
        for (; power != 0; power >>= 1) {
            if ((power & 1) == 1) {
                result = multiply(result, src);
            }
            src = multiply(src, src);
        }
        return result;
    }

    private int[][] multiply(int[][] left, int[][] right) {
        int[][] result = new int[left.length][left.length];
        for (int i = 0; i < left.length; ++i) {
            for (int j = 0; j < left.length; ++j) {
                for (int k = 0; k < left.length; ++k) {
                    result[i][j] += (left[i][k] * right[k][j]) % MODULO;
                }
                result[i][j] %= MODULO;
            }
        }
        return result;
    }

    private void solve() throws Throwable {
        long n = nextLong();
        int c = nextInt();
        l = new int[c];
        v = new int[c];
        int total = 1;
        for (int i = 0; i < c; ++i) {
            l[i] = nextToken().charAt(0) - 'A';
            used[l[i]] = true;
            total *= (v[i] = nextInt());
        }
        states = new int[total][];
        helper = new int[c];
        generate(0);
        int[][] grid = new int[total][total];
        for (int current = 0; current < total; ++current) {
            for (int i = 0; i < AlPHABET_SIZE; ++i) {
                helper = states[current].clone();
                if (used[i]) {
                    for (int ind = 0; ind < c; ++ind) {
                        if (l[ind] == i) {
                            helper[ind] = (helper[ind] + 1) % v[ind];
                        }
                    }
                    grid[current][getIndex(states, helper)]++;
                }
            }
        }
        int[][] result = pow(grid, n);
        int answer = 0;
        for (int currentState = 0; currentState < total; ++currentState) {
            Arrays.fill(good, false);
            for (int i = 0; i < c; ++i) {
                good[l[i]] |= states[currentState][i] == 0;
            }
            boolean can = true;
            for (int i = 0; i < AlPHABET_SIZE; ++i) {
                if (used[i] && !good[i]) {
                    can = false;
                    break;
                }
            }
            if (can) {
                answer = (answer + result[0][currentState]) % MODULO;
            }
        }
        output.println(answer);
    }

    public void run() {
        try {
            solve();
        } catch (Throwable e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
            System.exit(666);
        } finally {
            output.flush();
            output.close();
        }
    }


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

    private String nextToken() throws IOException {
        while (tokens == null || !tokens.hasMoreTokens()) {
            tokens = new StringTokenizer(input.readLine());
        }
        return tokens.nextToken();
    }

    private int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    private double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    private long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
    
    private static int[][] states;
    static private int[] helper, l, v;
    private static int SIZE = 0;
    private static final int MODULO = 12345, AlPHABET_SIZE = 26;
    static boolean[] used = new boolean[AlPHABET_SIZE], good = new boolean[AlPHABET_SIZE];
    private BufferedReader input;
    private PrintWriter output;
    private StringTokenizer tokens = null;
}