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.awt.Point;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.io.*;
import java.math.BigInteger;
import static java.math.BigInteger.*;
import java.util.*;

public class D{

    int mod=12345;
    void solve()throws Exception
    {
        int ones=0;
        HashMap<Integer,ArrayList<Integer>>map=new HashMap<Integer,ArrayList<Integer>>();
        long n=nextLong();
        int c=nextInt();
        int p=1;
        for(int i=0;i<c;i++)
        {
            int t=nextString().charAt(0)-'A';
            int m=nextInt();
            if(!map.containsKey(t))
                map.put(t,new ArrayList<Integer>());
            map.get(t).add(m);
        }
        int[][]ar=new int[map.size()][];
        int[]l=new int[ar.length];

        int ind=0;
        for(Integer x: map.keySet())
            {
                ArrayList<Integer>t=map.get(x);
                ar[ind]=new int[t.size()];
                int ind2=0;
                l[ind]=1;
                for(int i=0;i<t.size();i++)
                {
                    ar[ind][ind2]=t.get(i);
                    l[ind]=getLcm(l[ind],ar[ind][ind2]);
                    ind2++;
                }
                p*=l[ind];
                ind++;
            }
        long[][]mat=new long[p][p];
        for(int i=0;i<p;i++)
        {
            int[]state=new int[ar.length];
            int cur=i;
            for(int j=0;j<state.length;j++)
            {
                state[j]=cur%l[j];
                cur/=l[j];
            }
            for(int j=0;j<state.length;j++)
            {
                int[]ns=state.clone();
                ns[j]=(ns[j]+1)%l[j];
                cur=0;
                for(int k=state.length-1;k>=0;k--)
                {
                    cur=cur*l[k]+ns[k];
                }
                mat[i][cur]++;
            }

        }
        long[][]res=pow(mat,n);
        long sum=0;
        for(int i=0;i<p;i++)
        {
            int[]state=new int[ar.length];
            int cur=i;
            boolean ok=true;
            for(int j=0;j<state.length;j++)
            {
                state[j]=cur%l[j];
                cur/=l[j];
                boolean  any=false;
                for(int x: ar[j])
                    if(state[j]%x==0)
                        any=true;
                if(!any)
                    ok=false;

            }
            if(ok)
                sum=(sum+res[0][i])%mod;

        }
        System.out.println(sum);

    }


    private int getLcm(int a, int b) {
        return a/BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue()*b;
    }

    private long[][] pow(long[][] mat, long p) {
        if(p==0)
        {
            long[][]res=new long[mat.length][mat.length];
            for(int i=0;i<res.length;i++)
                res[i][i]=1;
            return res;
        }
        long[][]res=pow(mat,p/2);
        res=mul(res,res);
        if(p%2==1)
            res=mul(res,mat);
        return res;
    }

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

    ////////////
    BufferedReader reader;
    PrintWriter writer;
    StringTokenizer stk;
    void run()throws Exception
    {
        reader=new BufferedReader(new InputStreamReader(System.in));
        stk=null;
        writer=new PrintWriter(System.out);
        solve();
        reader.close();
        writer.close();
    }
    int nextInt()throws Exception
    {
        return Integer.parseInt(nextToken());
    }

    long nextLong()throws Exception
    {
        return Long.parseLong(nextToken());

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


    }

    String nextString()throws Exception
    {
        return nextToken();
    }
    String nextLine()throws Exception
    {
        return reader.readLine();
    }
    String nextToken()throws Exception
    {
        if(stk==null || !stk.hasMoreTokens())
        {
            stk=new StringTokenizer(nextLine());
            return nextToken();

        }
        return stk.nextToken();
    }

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



}