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.io.FileReader;
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);
    }


    static class Matrix {
        long[][] grid;
        long[][] helper;

        public Matrix(int size, boolean E) {
            this.grid = new long[size][size];
            this.helper = new long[size][size];
            if (E) {
                for (int i = 0; i < grid.length; ++i) {
                    grid[i][i] = 1;
                }
            }
        }

        public Matrix pow(long power) {
            Matrix result = new Matrix(grid.length, true);
            Matrix stat = new Matrix(grid.length, false);
            for (int i = 0; i < grid.length; ++i) {
                stat.grid[i] = this.grid[i].clone();
            }
            while (power != 0) {
                if ((power & 1) == 1) {
                    result.multiply(stat);
                    --power;
                } else {
                    stat.multiply(stat);
                    power >>= 1;
                }
            }
            return result;
        }


        private void multiply(Matrix another) {
            for (long[] current : helper) {
                Arrays.fill(current, 0);
            }
            for (int i = 0; i < grid.length; ++i) {
                for (int j = 0; j < grid.length; ++j) {
                    for (int k = 0; k < grid.length; ++k) {
                        helper[i][j] += (grid[i][k] * another.grid[k][j]) % MODULO;
                    }
                    helper[i][j] %= MODULO;
                }
            }
            for (int i = 0; i < grid.length; ++i) {
                System.arraycopy(helper[i], 0, grid[i], 0, grid.length);
            }
        }
    }


    static class Rule {
        char letter;
        char value;

        public Rule(char letter, char value) {
            this.letter = letter;
            this.value = value;
        }
    }

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

    private boolean isFinal(int index) {
        for (char letter = 'A'; letter <= 'Z'; ++letter) {
            boolean was = false, isGood = false;
            for (int i = 0; i < rules.length; ++i) {
                if (rules[i].letter == letter) {
                    was = true;
                    isGood |= states[index][i] == 0;
                }
            }
            if (was && !isGood) {
                return false;
            }
        }
        return true;
    }


    private void solve() throws Throwable {
        long n = nextLong();
        int c = nextInt();
        int s = c;
        c = 0;
        rules = new Rule[s];
        int total = 1;
        for (int i = 0; i < s; ++ i) {
            rules[c] = new Rule(nextToken().charAt(0), (char) (nextInt()));
            boolean was = false;
            for (int q = 0; q < c; ++q) {
                if (rules[q].letter == rules[c].letter && rules[q].value == rules[c].value) {
                    was = true;
                    break;
                }
            }
            if (!was){
                total *= rules[c++].value;
            }
        }
        rules = Arrays.copyOf(rules, c);
        initial = new char[c];
        states = new char[total][];
        generate(0);
        Matrix operator = new Matrix(total, false);
        for (int from = 0; from < total; ++from) {
            for (char letter = 'A'; letter <= 'Z'; ++letter) {
                boolean isPresent = false;
                char []helper = states[from].clone();
                for (int i = 0; i < c; ++i) {
                    if (rules[i].letter == letter) {
                        isPresent = true;
                        helper[i] = (char)((helper[i] + 1) % rules[i].value);
                    }
                }
                if (isPresent) {
                    for (int to = 0; to < states.length; ++to) {
                        boolean ok = true;
                        for (int index = 0; index < c; ++index) {
                            ok &= states[to][index] == helper[index];
                        }
                        if (ok) {
                            operator.grid[from][to]++;
                            break;
                        }
                    }
                }
            }
        }

        Matrix finish = operator.pow(n);
        long result = 0;
        for (int current = 0; current < total; ++current) {
            if (isFinal(current)) {
                result = (result + finish.grid[0][current]) % MODULO;
            }
        }
        output.println(result);
    }

    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 int SIZE = 0;
    private static final int MODULO = 12345;
    private char[][] states;
    private char[] initial;
    private Rule[] rules;
    private BufferedReader input;
    private PrintWriter output;
    private StringTokenizer tokens = null;
}