type
  mat=array[0..130,0..130] of longint;

var
  ok:boolean;
  n:int64;
  m,nn,pre,i,next,now,k,j,ans:longint;
  tt,a,tmp:mat;
  ch:char;
  g,mo:array[1..26] of boolean;
  modnum,w:array[1..2000] of longint;

procedure zero(var a:mat);
  begin
    fillchar(a,sizeof(a),0);
  end;

procedure mul(var a,b:mat);
  var
    i,j,k:longint;
    tmp:int64;
  begin
    if a[1,1]=-1 then
      begin
        a:=b;
        exit;
      end;
    for i:=1 to nn do
      for j:=1 to nn do
        begin
          tmp:=0;
          for k:=1 to nn do
            tmp:=tmp+a[i,k]*b[k,j];
          tt[i,j]:=tmp mod 12345;
        end;
    a:=tt;
  end;

begin

  readln(n,m);
  if n=0 then
    begin
      writeln(1);
      halt;
    end;

  if m=0 then
    begin
      writeln(0);
      halt;
    end;

  fillchar(g,sizeof(g),false);
  nn:=1;
  for m:=1 to m do
    begin
      read(ch);
      w[m]:=ord(ch)-64;
      g[w[m]]:=true;
      read(ch);
      readln(modnum[m]);
      nn:=modnum[m]*nn;
    end;

  zero(a);
  for i:=1 to nn do
    begin
      for k:=1 to 26 do
        if g[k] then
          begin
            pre:=1; next:=0;
            for j:=1 to m do
              begin
                now:=((i-1) div pre) mod modnum[j];
                if w[j]=k then inc(now);
                if now=modnum[j] then now:=0;
                next:=next+now*pre;
                pre:=pre*modnum[j];
              end;
            inc(a[i,next+1]);
          end;
    end;

  tmp[1,1]:=-1;
  while n<>0 do
    if n and 1=1 then
      begin
        mul(tmp,a);
        dec(n);
      end else
      begin
        mul(a,a);
        n:=n>>1;
      end;

  ans:=0;

  for i:=1 to nn do
    begin
      fillchar(mo,sizeof(mo),false);
      pre:=1;
      for j:=1 to m do
        begin
          k:=((i-1) div pre) mod modnum[j];
          if k=0 then
            mo[w[j]]:=true;
          pre:=pre*modnum[j];
        end;

      ok:=true;
      for j:=1 to 26 do
        if g[j] and not mo[j] then ok:=false;

      if ok then inc(ans,tmp[1,i]);
    end;

  writeln(ans mod 12345);

end.

program CF107D;
const
 md=12345;
 maxsize=123;
 maxn=1000;
type
 mat=array[0..maxsize,0..maxsize]of longint;
var
 ch:char;
 n:int64;
 i,j,k,m,size,ans,x:longint;
 a,b:mat;
 flag:boolean;
 crime:array[1..maxn]of char;
 factor,pass,tmp:array[1..maxn]of longint;
 bel:array['A'..'Z']of boolean;
 rec:array['A'..'Z',0..maxn]of longint;

procedure mul(var a:mat;b:mat);
var
 i,j,k:longint;
 c:mat;
begin
 fillchar(c,sizeof(c),0);
 for i:=0 to size do
  for j:=0 to size do
   for k:=0 to size do c[i,j]:=(c[i,j]+a[i,k]*b[k,j]) mod md;
 a:=c;
end;

procedure calc(n:int64);
var
 i:longint;
begin
 fillchar(a,sizeof(a),0);
 for i:=0 to size do a[i,i]:=1;
 while n>0 do
  begin
   if n and 1=1 then mul(a,b);
   mul(b,b);n:=n>>1;
  end;
end;

begin
{$ifndef ONLINE_JUDGE}
 assign(input,'CF107D.in');
 reset(input);
 assign(output,'CF107D.out');
 rewrite(output);
{$endif}
 readln(n,m);
 size:=1;
 for i:=1 to m do
  begin
   readln(crime[i],factor[i]);size:=size*factor[i];
   inc(rec[crime[i],0]);rec[crime[i],rec[crime[i],0]]:=i;
  end;
 if n=0 then
  begin
   writeln(1);halt;
  end;
 if m=0 then
  begin
   writeln(0);halt;
  end;
 dec(size);
 fillchar(b,sizeof(b),0);
 for i:=0 to size do
  begin
   x:=i;
   for j:=1 to m do
    begin
     pass[j]:=x mod factor[j];x:=x div factor[j];
    end;
   tmp:=pass;
   for ch:='A' to 'Z' do
    if rec[ch,0]>0 then
     begin
      for j:=1 to rec[ch,0] do
       begin
        inc(pass[rec[ch,j]]);
        if pass[rec[ch,j]]=factor[rec[ch,j]] then pass[rec[ch,j]]:=0;
       end;
      x:=0;
      for j:=m downto 1 do x:=x*factor[j]+pass[j];
      pass:=tmp;inc(b[i,x]);
     end;
  end;

 calc(n);ans:=0;
 for i:=0 to size do
  begin
   x:=i;flag:=true;
   fillchar(bel,sizeof(bel),false);
   for j:=1 to m do
    begin
     if x mod factor[j]=0 then bel[crime[j]]:=true;
     x:=x div factor[j];
    end;
   for ch:='A' to 'Z' do
    if rec[ch,0]>0 then
     if not bel[ch] then
      begin
       flag:=false;break;
      end;
   if flag then ans:=(ans+a[i,0]) mod md;
  end;
 writeln(ans);
end.

program CF107D;
const
 md=12345;
 maxsize=123;
 maxn=1000;
type
 mat=array[0..maxsize,0..maxsize]of longint;
var
 ch:char;
 n:int64;
 i,j,k,m,size,ans,x:longint;
 a,b:mat;
 flag:boolean;
 crime:array[1..maxn]of char;
 factor,pass,tmp:array[1..maxn]of longint;
 bel:array['A'..'Z']of boolean;
 rec:array['A'..'Z',0..maxn]of longint;

procedure mul(var a:mat;b:mat);
var
 i,j,k:longint;
 c:mat;
begin
 fillchar(c,sizeof(c),0);
 for i:=0 to size do
  for j:=0 to size do
   for k:=0 to size do c[i,j]:=(c[i,j]+a[i,k]*b[k,j]) mod md;
 a:=c;
end;

procedure calc(n:int64);
var
 i:longint;
begin
 fillchar(a,sizeof(a),0);
 for i:=0 to size do a[i,i]:=1;
 while n>0 do
  begin
   if n and 1=1 then mul(a,b);
   mul(b,b);n:=n>>1;
  end;
end;

begin
 readln(n,m);
 size:=1;
 for i:=1 to m do
  begin
   readln(crime[i],factor[i]);size:=size*factor[i];
   inc(rec[crime[i],0]);rec[crime[i],rec[crime[i],0]]:=i;
  end;
 if n=0 then
  begin
   writeln(1);halt;
  end;
 if m=0 then
  begin
   writeln(0);halt;
  end;
 dec(size);
 fillchar(b,sizeof(b),0);
 for i:=0 to size do
  begin
   x:=i;
   for j:=1 to m do
    begin
     pass[j]:=x mod factor[j];x:=x div factor[j];
    end;
   tmp:=pass;
   for ch:='A' to 'Z' do
    if rec[ch,0]>0 then
     begin
      for j:=1 to rec[ch,0] do
       begin
        inc(pass[rec[ch,j]]);
        if pass[rec[ch,j]]=factor[rec[ch,j]] then pass[rec[ch,j]]:=0;
       end;
      x:=0;
      for j:=m downto 1 do x:=x*factor[j]+pass[j];
      pass:=tmp;inc(b[i,x]);
     end;
  end;

 calc(n);ans:=0;
 for i:=0 to size do
  begin
   x:=i;flag:=true;
   fillchar(bel,sizeof(bel),false);
   for j:=1 to m do
    begin
     if x mod factor[j]=0 then bel[crime[j]]:=true;
     x:=x div factor[j];
    end;
   for ch:='A' to 'Z' do
    if rec[ch,0]>0 then
     if not bel[ch] then
      begin
       flag:=false;break;
      end;
   if flag then ans:=(ans+a[i,0]) mod md;
  end;
 writeln(ans);
end.

const p=12345;
type arr=array[0..125,0..125] of longint;
     arr1=array[0..125] of longint;
     St=array[0..26,0..10] of longint;
var n:int64; m,tot:longint;
    t:array[0..26,0..125] of longint;
    a,tmp:St;
    tp:array[0..26] of longint;
    s:array[0..125] of St;
    g,tt,tt1:arr; ans,c:arr1;

procedure dfs(x,y:longint);
var i:longint;
begin
  if x=tp[0]+1 then
  begin
    inc(tot); s[tot]:=tmp;
  end
  else
  begin
    for i:=0 to a[tp[x],y]-1 do
    begin
      tmp[x,y]:=i;
      if y=tmp[x,0] then dfs(x+1,1) else dfs(x,y+1);
    end;
  end;
end;

function get(x,y:longint):longint;
var i,j,t,t1,res:longint; f:boolean;
begin
  t1:=0;
  for i:=1 to tp[0] do
  begin
    f:=true;
    for j:=1 to tmp[i,0] do
    begin
      if s[x][i][j]<>s[y][i][j] then
      begin
        f:=false; break;
      end;
    end;
    if not f then
    begin
      t:=i; inc(t1);
    end;
  end;
  if t1>1 then exit(0);
  if t1=1 then
  begin
    f:=true;
    for i:=1 to tmp[t,0] do
    begin
      if (s[x][t][i]+1) mod a[tp[t],i]<>s[y][t][i] then
      begin
        f:=false; break;
      end;
    end;
    if f then exit(1); exit(0);
  end;
  res:=0;
  for i:=1 to tp[0] do
  begin
    if tmp[i,0]>1 then continue;
    if a[tp[i],1]=1 then inc(res);
  end;
  exit(res);
end;

procedure init;
var i,j,x:longint; c1,c2:char;
begin
  readln(n,m);
  fillchar(t,sizeof(t),0);
  for i:=1 to m do
  begin
    readln(c1,c2,x);
    t[ord(c1)-64,x]:=1;
  end;
  tp[0]:=0;
  for i:=1 to 26 do
  begin
    a[i,0]:=0;
    for j:=1 to 123 do
    begin
      if t[i,j]=1 then
      begin
        inc(a[i,0]); a[i,a[i,0]]:=j;
      end;
    end;
    if a[i,0]>0 then
    begin
      inc(tp[0]); tp[tp[0]]:=i;
    end;
  end;
  for i:=1 to tp[0] do tmp[i,0]:=a[tp[i],0];
  tot:=0;
  dfs(1,1);
  fillchar(g,sizeof(g),0);
  for i:=1 to tot do
   for j:=1 to tot do g[i,j]:=get(i,j);
end;

function mul(a,b:arr):arr;
var i,j,k:longint;
begin
  fillchar(tt1,sizeof(tt1),0);
  for i:=1 to tot do
   for j:=1 to tot do
    for k:=1 to tot do tt1[i,j]:=(tt1[i,j]+a[i,k]*b[k,j]) mod p;
  exit(tt1);
end;

function mul2(a:arr1; b:arr):arr1;
var i,j:longint;
begin
  fillchar(c,sizeof(c),0);
  for i:=1 to tot do
   for j:=1 to tot do c[i]:=(c[i]+a[j]*b[j,i]) mod p;
  exit(c);
end;

function pow(a:arr; b:int64):arr;
var i:longint;
begin
  fillchar(tt,sizeof(tt),0);
  for i:=1 to tot do tt[i,i]:=1;
  while b>0 do
  begin
    if b mod 2=1 then tt:=mul(tt,a);
    a:=mul(a,a); b:=b div 2;
  end;
  exit(tt);
end;

function check(x:longint):boolean;
var i,j:longint; f:boolean;
begin
  for i:=1 to tp[0] do
  begin
    f:=false;
    for j:=1 to tmp[i,0] do
    begin
      if s[x][i][j]=0 then
      begin
        f:=true; break;
      end;
    end;
    if not f then exit(false);
  end;
  exit(true);
end;

procedure work;
var i:longint; res:longint;
begin
  fillchar(ans,sizeof(ans),0);
  ans[1]:=1;
  g:=pow(g,n);
  ans:=mul2(ans,g);
  res:=0;
  for i:=1 to tot do
  begin
    if check(i) then res:=res+ans[i];
  end;
  res:=res mod p;
  writeln(res);
end;

begin
  init;
  work;
end.

type
	mat=array[0..123,0..123] of longint;
var
ans,g,rs:mat;

procedure mult(const a,b:mat; all:longint);
var i,j,k:longint;
begin fillchar(rs,sizeof(rs),0);
	for k:=0 to all do
		for j:=0 to all do
			if b[k,j]>0 then
				for i:=0 to all do
					rs[i,j]:=(rs[i,j]+a[i,k]*b[k,j]) mod 12345;
end;

procedure mul(all:longint; y:int64);
var i:longint;
begin for i:=60 downto 0 do if y and (int64(1)<<i)>0 then break;
	ans:=g;
	for i:=i-1 downto 0 do
	begin mult(ans,ans,all); ans:=rs;
		if y and (int64(1)<<i)>0 then begin mult(ans,g,all); ans:=rs; end;
	end;
end;

var
count:array['A'..'Z'] of longint;
rec:array['A'..'Z',0..1010] of longint;
multi,ss,tmp:array[0..1010] of longint;
ok:array['A'..'Z'] of boolean;
crime:array[0..1010] of char;

procedure lemon;
var LenX:int64;
    n,i,all,j,x,k,final:longint;
    ch:char;
    flag:boolean;
begin readln(LenX,n); if LenX=0 then begin writeln(1); exit; end;
	all:=1;
	for i:=1 to n do
	begin readln(ch,multi[i]); crime[i]:=ch;
		inc(count[ch]); rec[ch,count[ch]]:=i;
		all:=all*multi[i];
	end;
	fillchar(g,sizeof(g),0);
	for i:=0 to all-1 do
	begin x:=i;
		for j:=1 to n do begin ss[j]:=x mod multi[j]; x:=x div multi[j]; end;
		tmp:=ss;
		for ch:='A' to 'Z' do
			if count[ch]>0 then
			begin for k:=1 to count[ch] do
				begin inc(ss[rec[ch,k]]);
					if ss[rec[ch,k]]=multi[rec[ch,k]] then ss[rec[ch,k]]:=0;
				end;
				x:=0;
				for j:=n downto 1 do x:=x*multi[j]+ss[j];
				inc(g[i,x]);
				ss:=tmp;
			end;
	end;
	mul(all-1,LenX); final:=0;
	for i:=0 to all-1 do
	begin x:=i; fillchar(ok,sizeof(ok),false);
		for j:=1 to n do begin if x mod multi[j]=0 then ok[crime[j]]:=true; x:=x div multi[j]; end;
		flag:=true;
		for ch:='A' to 'Z' do
			if count[ch]>0 then
				if not ok[ch] then
				begin flag:=false;
					break;
				end;
		if flag then final:=(final+ans[i,0]) mod 12345;
	end;
	writeln(final);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'107D.in');
	reset(input);
{$endif}
lemon;
end.

type
  re=record
       w,next:longint;
     end;

const
  max=12345;

type
  data=array[0..128,0..128]of longint;

var
  n:int64;
  m,tt,top,tot,ans:longint;
  a:array['A'..'Z']of longint;
  sum,size:array[0..26]of longint;
  g:array['A'..'Z']of longint;
  map:array[0..2000]of re;
  nam:array[0..26]of char;
  f,e:data;

procedure add(x,y:data;var t:data);
var
  i,j,k:longint;

begin
  fillchar(t,sizeof(t),0);
  for i:=0 to top-1 do
    for j:=0 to top-1 do
      for k:=0 to top-1 do t[i,j]:=(t[i,j]+x[i,k]*y[k,j])mod max;
end;

function jd(x:longint):boolean;
var
  i,t,j,k:longint;
  ch:char;

begin
  for i:=1 to tt do begin
    t:=(x div sum[i-1])mod size[i];
    ch:=nam[i];
    k:=g[ch];
    j:=0;
    while k<>0 do begin
      if t mod map[k].w=0 then begin
        j:=1;
        break;
      end;
      k:=map[k].next;
    end;
    if j=0 then exit(false);
  end;
  exit(true);
end;

procedure init;
var
  i,j,x,y,next:longint;
  ch:char;

begin
  fillchar(a,sizeof(a),255);
  tot:=0;
  fillchar(g,sizeof(g),0);
  for i:=1 to m do begin
    read(ch);
    readln(x);
    if a[ch]=-1 then a[ch]:=1;
    a[ch]:=a[ch]*x;
    inc(tot);
    map[tot].w:=x;
    map[tot].next:=g[ch];
    g[ch]:=tot;
  end;

  tt:=0;
  sum[0]:=1;
  for ch:='A' to 'Z' do if a[ch]>0 then begin
    inc(tt);
    sum[tt]:=sum[tt-1]*a[ch];
    size[tt]:=a[ch];
    nam[tt]:=ch;
  end;

  top:=sum[tt];

  if tt=0 then begin
    if n=0 then writeln(1)
    else writeln(0);
    exit;
  end;

  fillchar(f,sizeof(f),0);
  fillchar(e,sizeof(e),0);
  for i:=0 to top-1 do f[i,i]:=1;
  for i:=0 to top-1 do
    for j:=1 to tt do begin
      x:=(i div sum[j-1])mod size[j];
      y:=x+1;
      if y=size[j] then y:=0;
      next:=i-(x*sum[j-1])+(y*sum[j-1]);
      inc(e[i,next]);
    end;

  while n>0 do begin
    if n and 1=1 then add(f,e,f);
    n:=n shr 1;
    add(e,e,e);
  end;

  ans:=0;
  for i:=0 to top-1 do if jd(i) then ans:=(ans+f[0,i])mod max;
  writeln(ans);
end;

begin
  //while true do begin
  readln(n,m);
  init;
  //end;
end.

{R+,S+,Q+,I+,O-}
{$R-,S-,Q-,I-,O+}
{$M 10000000}
const md = 12345;
var
  ans,m,kv,i,j,n,k,ii: longint;
  ok: boolean;
  ch: char;
  s,step,q: int64;
  d: array [0..144,0..1010] of longint;
  good,can: array ['A'..'Z'] of boolean;
  tp: array [0..1010] of char;
  v,e,f,z: array [0..1010] of longint;
  a,b,c: array [0..144,0..144] of longint;

procedure rec(v:longint);
var
  i,j: longint;
begin
  if v = m+1 then
  begin
    inc(n);
    for i:=1 to m do d[n,i]:=e[i];
    exit;
  end;
  for j:=0 to z[v]-1 do
  begin
    e[v]:=j;
    rec(v+1);
  end;
end;

begin
//  assign(input,'in'); reset(input);
//  assign(output,'out'); rewrite(output);
  read(q,m);
  fillchar(can,sizeof(can),False);
  kv:=0;
  for i:=1 to m do
  begin
    read(ch);
    while not (ch in ['A'..'Z']) do read(ch);
    tp[i]:=ch;
    can[ch]:=True;
    read(z[i]);
    if z[i] > 1 then
    begin
      inc(kv);
      v[kv]:=i;
    end;
  end;
  n:=0;
  rec(1);
  fillchar(b,sizeof(b),0);
  for i:=1 to n do
    for ch:='A' to 'Z' do
      if can[ch] then
      begin
        for j:=1 to kv do
          if tp[v[j]] = ch then
          begin
            f[j]:=d[i,v[j]]+1;
            if f[j] = z[v[j]] then f[j]:=0;
          end
          else f[j]:=d[i,v[j]];
        for ii:=1 to n do
        begin
          ok:=True;
          for j:=1 to kv do
            if f[j] <> d[ii,v[j]] then
            begin
              ok:=False;
              break;
            end;
          if ok then inc(b[i,ii]);
        end;
      end;
  fillchar(a,sizeof(a),0);
  for i:=1 to n do a[i,i]:=1;
  step:=int64(1) shl 62;
  while step > 0 do
  begin
    for i:=1 to n do
      for j:=1 to n do
      begin
        s:=0;
        for k:=1 to n do s:=s+a[i,k]*a[k,j];
        c[i,j]:=s mod md;
      end;
    a:=c;
    if step and q <> 0 then
    begin
      for i:=1 to n do
        for j:=1 to n do
        begin
          s:=0;
          for k:=1 to n do s:=s+a[i,k]*b[k,j];
          c[i,j]:=s mod md;
        end;
      a:=c;
    end;
    step:=step shr 1;
  end;
  ans:=0;
  for i:=1 to n do
  begin
    fillchar(good,sizeof(good),False);
    for j:=1 to m do
      if d[i,j] = 0 then good[tp[j]]:=True;
    ok:=True;
    for ch:='A' to 'Z' do
      if not good[ch] and can[ch] then
      begin
        ok:=False;
        break;
      end;
    if ok then ans:=(ans+a[1,i]) mod md;
  end;
  writeln(ans);
  close(input);
  close(output);
end.

program cf107d;
const
  mode=12345;
type
  tmx=array[0..123,0..123]of longint;

var
  i,j,m,num,tot,ans:longint;
  n:int64;
  c:char;
  mo:array['A'..'Z']of longint;
  u:array[0..100]of longint;
  cd:array[0..1000]of char;
  cn:array[0..1000]of longint;
  v:array['A'..'Z']of longint;
  ki:array['A'..'Z']of boolean;
  tma,tmb,tms:tmx;
  kind:boolean;
  ch:char;

procedure zh(x:longint);
var
  c:char;
begin
  for c:='Z' downto 'A' do
    if mo[c]<>0 then begin
      v[c]:=x mod mo[c];
      x:=x div mo[c];
    end;
end;
function hash:longint;
var
  c:char;
  AN:LONGINT;
begin
AN:=0;
  for c:='A' to 'Z' do
    if mo[c]<>0 then
      AN:=AN*mo[c]+v[c];
  HASH:=AN;
end;

procedure deal(x:longint);
var
  c:char;
  y:longint;
begin
  zh(x);
  for c:='A' to 'Z' do
    if mo[c]<>0 then begin
      v[c]:=(v[c]+1)mod mo[c];
      y:=hash;
      inc(tms[x,y]);
      v[c]:=(v[c]-1+mo[c])mod mo[c];
    end;
end;

procedure jzc(var c,a,b:tmx);
var
  i,j,k:longint;
begin
  for i:=0 to tot do
    for j:=0 to tot do
      c[i,j]:=0;
  for i:=0 to tot do
    for j:=0 to tot do
      for k:=0 to tot do
        c[i,j]:=(a[i,k]*b[k,j]+c[i,j])mod mode;
end;

procedure ksm(n:int64);
var
  i:longint;
begin
  u[0]:=0;
  while n<>1 do begin
    inc(u[0]);
    u[u[0]]:=n and 1;
    n:=n shr 1;
  end;
  tma:=tms;
  for i:=u[0] downto 1 do begin
    jzc(tmb,tma,tma);
    if u[i]=1 then jzc(tma,tmb,tms)
    else tma:=tmb;
  end;
end;

begin
  readln(n,m);
  if n=0 then begin
    writeln(1);
    halt;
  end;

  fillchar(mo,sizeof(mo),0);

  for i:=1 to m do begin
    read(ch);
    readln(num);
    cd[i]:=ch;
    cn[i]:=num;
    if mo[ch]=0 then
      mo[ch]:=num
    else mo[ch]:=mo[ch]*num;
  end;

  tot:=1;
  for c:='A' to 'Z' do
    if mo[c]<>0 then tot:=tot*mo[c];
  dec(tot);

  fillchar(tms,sizeof(tms),0);
  for i:=0 to tot do deal(i);

  ksm(n);

  //----------------------print--------------------\
  ans:=0;
  for i:=0 to tot do begin
    for c:='A' to 'Z' do if mo[c]<>0 then
      ki[c]:=false
    else ki[c]:=true;

    ZH(I);
    for j:=1 to m do
      if v[cd[j]] mod cn[j]=0 then ki[cd[j]]:=true;
    kind:=true;

    for c:='A' to 'Z' do if not(ki[c]) then kind:=false;
    if kind then ans:=(ans+tma[0,i])mod mode;
  end;
  writeln(ans mod mode);
end.

   {1,7,15}

const md = 12345;
type mat = array[0..200,0..200]of longint;
var
ok:boolean;
uu,ee:array[char]of boolean;
vv,q,vvv,ne,ed:mat;//array[0..200,0..200]of longint;
chh:array[0..1000]of char;
u:array['A'..'Z'] of boolean;
ans,k,w,j,st,e,c,i:longint;
n:int64;
ch:char;
t,f:array[0..1000]of longint;

procedure run1(v,sost:longint);
var i:longint;
 begin
  if v>c then
    begin
     q[sost,sost]:=e;
     fillchar(ee,sizeof(ee),false);
     for i:=1 to c do
       if t[i]=0 then
         begin
          ee[chh[i]]:=true;
         end;

    inc(w);
    vv[0,sost]:=1;

    for ch:='A' to 'Z' do
      if uu[ch] and (not ee[ch]) then vv[0,sost]:=0;

  //  for j:=1 to c do write(t[j],' ');writeln(sost,' ',vv[0,sost]);

     for ch:='A' to 'Z' do
      if uu[ch] then
      if u[ch] then
       begin
        for j:=1 to c do
         if chh[j]=ch then t[j]:=(t[j]+1)mod f[j];
        st:=0;
        for j:=1 to c do
          if u[chh[j]] then st:=st*f[j]+t[j];
        q[sost,st]:=q[sost,st]+1;
     //   writeln(' ',st);

        for j:=1 to c do
         if chh[j]=ch then t[j]:=(t[j]-1+f[j])mod f[j];

       end;
     exit;
    end;

  if u[chh[v]] then
    begin
     for i:=0 to f[v]-1 do
      begin
       t[v]:=i;
       run1(v+1,sost*f[v]+i);
      end;
    end else run1(v+1,sost);
 end;

 begin
  readln(n,c);
  fillchar(u,sizeof(u),true);
  e:=0;
  for i:=1 to c do
    begin
     readln(ch,f[i]);
     uu[ch]:=true;
     chh[i]:=ch;
     if f[i]=1 then
       begin
        if u[ch] then e:=e+1;
        u[ch]:=false;
       end;
    end;
  run1(1,0);

  w:=w-1;
  for i:=0 to w do ed[i,i]:=1;

//  n:=n-1;
  while n>0 do
    begin
     if n mod 2=1 then
       begin
        fillchar(ne,sizeof(ne),0);
        for i:=0 to w do
          for j:=0 to w do
            for k:=0 to w do
              ne[i,j]:=(ne[i,j]+ed[i,k]*q[k,j])mod md;
        ed:=ne;
       end;

        fillchar(ne,sizeof(ne),0);
        for i:=0 to w do
          for j:=0 to w do
            for k:=0 to w do
              ne[i,j]:=(ne[i,j]+q[i,k]*q[k,j])mod md;
        q:=ne;

     n:=n div 2;
    end;

  fillchar(ne,sizeof(ne),0);
  vvv[0,0]:=1;
  for i:=0 to 0 do
    for j:=0 to w do
      for k:=0 to w do
        ne[i,j]:=ne[i,j]+vvv[i,k]*ed[k,j];
                        {
  for i:=0 to w do
    begin
     for j:=0 to w do
       write(ed[i,j],' ');
     writeln;
    end;
}
  ans:=0;
  for i:=0 to w do
   if vv[0,i]>0 then
    ans:=ans+ne[0,i];
  writeln(ans mod md);
 end.

type
  mat=array[0..130,0..130] of longint;

var
  ok:boolean;
  n:int64;
  m,nn,pre,i,next,now,k,j,ans:longint;
  tt,a,tmp:mat;
  ch:char;
  g,mo:array[1..26] of boolean;
  modnum,w:array[1..2000] of longint;

procedure zero(var a:mat);
  begin
    fillchar(a,sizeof(a),0);
  end;

procedure mul(var a,b:mat);
  var
    i,j,k:longint;
    tmp:int64;
  begin
    if a[1,1]=-1 then
      begin
        a:=b;
        exit;
      end;
    for i:=1 to nn do
      for j:=1 to nn do
        begin
          tmp:=0;
          for k:=1 to nn do
            tmp:=tmp+a[i,k]*b[k,j];
          tt[i,j]:=tmp mod 12345;
        end;
    a:=tt;
  end;

begin
  readln(n,m);
  fillchar(g,sizeof(g),false);
  nn:=1;
  for m:=1 to m do
    begin
      read(ch);
      w[m]:=ord(ch)-64;
      g[w[m]]:=true;
      read(ch);
      readln(modnum[m]);
      nn:=modnum[m]*nn;
    end;

  zero(a);
  for i:=1 to nn do
    begin
      for k:=1 to 26 do
        if g[k] then
          begin
            pre:=1; next:=0;
            for j:=1 to m do
              begin
                now:=((i-1) div pre) mod modnum[j];
                if w[j]=k then inc(now);
                if now=modnum[j] then now:=0;
                next:=next+now*pre;
                pre:=pre*modnum[j];
              end;
            inc(a[i,next+1]);
          end;
    end;

  tmp[1,1]:=-1;
  while n<>0 do
    if n and 1=1 then
      begin
        mul(tmp,a);
        dec(n);
      end else
      begin
        mul(a,a);
        n:=n>>1;
      end;

  ans:=0;

  for i:=1 to nn do
    begin
      fillchar(mo,sizeof(mo),false);
      pre:=1;
      for j:=1 to m do
        begin
          k:=((i-1) div pre) mod modnum[j];
          if k=0 then
            mo[w[j]]:=true;
          pre:=pre*modnum[j];
        end;

      ok:=true;
      for j:=1 to 26 do
        if g[j] and not mo[j] then ok:=false;

      if ok then inc(ans,tmp[1,i]);
    end;

  writeln(ans mod 12345);
end.

program CF107D;
const
 md=12345;
 maxsize=123;
 maxn=1000;
type
 mat=array[0..maxsize,0..maxsize]of longint;
var
 ch:char;
 n:int64;
 i,j,k,m,size,ans,x:longint;
 a,b:mat;
 flag:boolean;
 crime:array[1..maxn]of char;
 factor,pass,tmp:array[1..maxn]of longint;
 bel:array['A'..'Z']of boolean;
 rec:array['A'..'Z',0..maxn]of longint;

procedure mul(var a:mat;b:mat);
var
 i,j,k:longint;
 c:mat;
begin
 fillchar(c,sizeof(c),0);
 for i:=0 to size do
  for j:=0 to size do
   for k:=0 to size do c[i,j]:=(c[i,j]+a[i,k]*b[k,j]) mod md;
 a:=c;
end;

procedure calc(n:int64);
var
 i:longint;
begin
 fillchar(a,sizeof(a),0);
 for i:=0 to size do a[i,i]:=1;
 while n>0 do
  begin
   if n and 1=1 then mul(a,b);
   mul(b,b);n:=n>>1;
  end;
end;

begin
 {$ifndef ONLINE_JUDGE}
 assign(input,'CF107D.in');
 reset(input);

 readln(n,m);
 if n=0 then
  begin
   writeln(1);halt;
  end;
 size:=1;
 for i:=1 to m do
  begin
   readln(crime[i],factor[i]);size:=size*factor[i];
   inc(rec[crime[i],0]);rec[crime[i],rec[crime[i],0]]:=i;
  end;

 dec(size);
 fillchar(b,sizeof(b),0);
 for i:=0 to size do
  begin
   x:=i;
   for j:=1 to m do
    begin
     pass[j]:=x mod factor[j];x:=x div factor[j];
    end;
   tmp:=pass;
   for ch:='A' to 'Z' do
    if rec[ch,0]>0 then
     begin
      for j:=1 to rec[ch,0] do
       begin
        inc(pass[rec[ch,j]]);
        if pass[rec[ch,j]]=factor[rec[ch,j]] then pass[rec[ch,j]]:=0;
       end;
      x:=0;
      for j:=m downto 1 do x:=x*factor[j]+pass[j];
      pass:=tmp;inc(b[i,x]);
     end;
  end;

 calc(n);ans:=0;
 for i:=0 to size do
  begin
   x:=i;flag:=true;
   fillchar(bel,sizeof(bel),false);
   for j:=1 to m do
    begin
     if x mod factor[j]=0 then bel[crime[j]]:=true;
     x:=x div factor[j];
    end;
   for ch:='A' to 'Z' do
    if rec[ch,0]>0 then
     if not bel[ch] then
      begin
       flag:=false;break;
      end;
   if flag then ans:=(ans+a[i,0]) mod md;
  end;
 writeln(ans);
 {$endif}
end.

program CF107D;
const
 md=12345;
 maxsize=123;
 maxn=1000;
type
 mat=array[0..maxsize,0..maxsize]of longint;
var
 ch:char;
 n:int64;
 i,j,k,m,size,ans,x:longint;
 a,b:mat;
 flag:boolean;
 crime:array[1..maxn]of char;
 factor,pass,tmp:array[1..maxn]of longint;
 bel:array['A'..'Z']of boolean;
 rec:array['A'..'Z',0..maxn]of longint;

procedure mul(var a:mat;b:mat);
var
 i,j,k:longint;
 c:mat;
begin
 fillchar(c,sizeof(c),0);
 for i:=0 to size do
  for j:=0 to size do
   for k:=0 to size do c[i,j]:=(c[i,j]+a[i,k]*b[k,j]) mod md;
 a:=c;
end;

procedure calc(n:int64);
var
 i:longint;
begin
 fillchar(a,sizeof(a),0);
 for i:=0 to size do a[i,i]:=1;
 while n>0 do
  begin
   if n and 1=1 then mul(a,b);
   mul(b,b);n:=n>>1;
  end;
end;

begin
 {$ifndef ONLINE_JUDGE}
 assign(input,'CF107D.in');
 reset(input);

 readln(n,m);
 size:=1;
 for i:=1 to m do
  begin
   readln(crime[i],factor[i]);size:=size*factor[i];
   inc(rec[crime[i],0]);rec[crime[i],rec[crime[i],0]]:=i;
  end;
 if n=0 then
  begin
   writeln(1);halt;
  end;
 if m=0 then
  begin
   writeln(0);halt;
  end;
 dec(size);
 fillchar(b,sizeof(b),0);
 for i:=0 to size do
  begin
   x:=i;
   for j:=1 to m do
    begin
     pass[j]:=x mod factor[j];x:=x div factor[j];
    end;
   tmp:=pass;
   for ch:='A' to 'Z' do
    if rec[ch,0]>0 then
     begin
      for j:=1 to rec[ch,0] do
       begin
        inc(pass[rec[ch,j]]);
        if pass[rec[ch,j]]=factor[rec[ch,j]] then pass[rec[ch,j]]:=0;
       end;
      x:=0;
      for j:=m downto 1 do x:=x*factor[j]+pass[j];
      pass:=tmp;inc(b[i,x]);
     end;
  end;

 calc(n);ans:=0;
 for i:=0 to size do
  begin
   x:=i;flag:=true;
   fillchar(bel,sizeof(bel),false);
   for j:=1 to m do
    begin
     if x mod factor[j]=0 then bel[crime[j]]:=true;
     x:=x div factor[j];
    end;
   for ch:='A' to 'Z' do
    if rec[ch,0]>0 then
     if not bel[ch] then
      begin
       flag:=false;break;
      end;
   if flag then ans:=(ans+a[i,0]) mod md;
  end;
 writeln(ans);
 {$endif}
end.

program CF107D;
const
 md=12345;
 maxsize=123;
 maxn=1000;
type
 mat=array[0..maxsize,0..maxsize]of longint;
var
 ch:char;
 n:int64;
 i,j,k,m,size,ans,x:longint;
 a,b:mat;
 flag:boolean;
 crime:array[1..maxn]of char;
 factor,pass,tmp:array[1..maxn]of longint;
 bel:array['A'..'Z']of boolean;
 rec:array['A'..'Z',0..maxn]of longint;

procedure mul(var a:mat;b:mat);
var
 i,j,k:longint;
 c:mat;
begin
 fillchar(c,sizeof(c),0);
 for i:=0 to size do
  for j:=0 to size do
   for k:=0 to size do c[i,j]:=(c[i,j]+a[i,k]*b[k,j]) mod md;
 a:=c;
end;

procedure calc(n:int64);
var
 i:longint;
begin
 fillchar(a,sizeof(a),0);
 for i:=0 to size do a[i,i]:=1;
 while n>0 do
  begin
   if n and 1=1 then mul(a,b);
   mul(b,b);n:=n>>1;
  end;
end;

begin
 {$ifndef ONLINE_JUDGE}
 assign(input,'CF107D.in');
 reset(input);
 assign(output,'CF107D.out');
 rewrite(output);

 readln(n,m);
 if n=0 then
  begin
   writeln(1);close(input);close(output);halt;
  end;
 size:=1;
 for i:=1 to m do
  begin
   readln(crime[i],factor[i]);size:=size*factor[i];
   inc(rec[crime[i],0]);rec[crime[i],rec[crime[i],0]]:=i;
  end;

 dec(size);
 fillchar(b,sizeof(b),0);
 for i:=0 to size do
  begin
   x:=i;
   for j:=1 to m do
    begin
     pass[j]:=x mod factor[j];x:=x div factor[j];
    end;
   tmp:=pass;
   for ch:='A' to 'Z' do
    if rec[ch,0]>0 then
     begin
      for j:=1 to rec[ch,0] do
       begin
        inc(pass[rec[ch,j]]);
        if pass[rec[ch,j]]=factor[rec[ch,j]] then pass[rec[ch,j]]:=0;
       end;
      x:=0;
      for j:=m downto 1 do x:=x*factor[j]+pass[j];
      pass:=tmp;inc(b[i,x]);
     end;
  end;

 calc(n);ans:=0;
 for i:=0 to size do
  begin
   x:=i;flag:=true;
   fillchar(bel,sizeof(bel),false);
   for j:=1 to m do
    begin
     if x mod factor[j]=0 then bel[crime[j]]:=true;
     x:=x div factor[j];
    end;
   for ch:='A' to 'Z' do
    if rec[ch,0]>0 then
     if not bel[ch] then
      begin
       flag:=false;break;
      end;
   if flag then ans:=(ans+a[i,0]) mod md;
  end;
 writeln(ans);
 close(input);close(output);
 {$endif}
end.

type
	mat=array[0..123,0..123] of longint;
var
ans,g,rs:mat;

procedure mult(const a,b:mat; all:longint);
var i,j,k:longint;
begin fillchar(rs,sizeof(rs),0);
	for k:=0 to all do
		for j:=0 to all do
			if b[k,j]>0 then
				for i:=0 to all do
					rs[i,j]:=(rs[i,j]+a[i,k]*b[k,j]) mod 12345;
end;

procedure mul(all:longint; y:int64);
var i:longint;
begin for i:=60 downto 0 do if y and (int64(1)<<i)>0 then break;
	ans:=g;
	for i:=i-1 downto 0 do
	begin mult(ans,ans,all); ans:=rs;
		if y and (int64(1)<<i)>0 then begin mult(ans,g,all); ans:=rs; end;
	end;
end;

var
count:array['A'..'Z'] of longint;
rec:array['A'..'Z',0..1010] of longint;
multi,ss,tmp:array[0..1010] of longint;
ok:array['A'..'Z'] of boolean;
crime:array[0..1010] of char;

procedure lemon;
var LenX:int64;
    n,i,all,j,x,k,final:longint;
    ch:char;
    flag:boolean;
begin readln(LenX,n); if LenX=0 then begin writeln(1); exit; end;
	all:=1;
	for i:=1 to n do
	begin readln(ch,multi[i]); crime[i]:=ch;
		inc(count[ch]); rec[ch,count[ch]]:=i;
		all:=all*multi[i];
	end;
	fillchar(g,sizeof(g),0);
	for i:=0 to all-1 do
	begin x:=i;
		for j:=1 to n do begin ss[j]:=x mod multi[j]; x:=x div multi[j]; end;
		tmp:=ss;
		for ch:='A' to 'Z' do
			if count[ch]>0 then
			begin for k:=1 to count[ch] do
				begin inc(ss[rec[ch,k]]);
					if ss[rec[ch,k]]=multi[rec[ch,k]] then ss[rec[ch,k]]:=0;
				end;
				x:=0;
				for j:=n downto 1 do x:=x*multi[j]+ss[j];
				g[i,x]:=1;
				ss:=tmp;
			end;
	end;
	mul(all-1,LenX); final:=0;
	for i:=0 to all-1 do
	begin x:=i; fillchar(ok,sizeof(ok),false);
		for j:=1 to n do begin if x mod multi[j]=0 then ok[crime[j]]:=true; x:=x div multi[j]; end;
		flag:=true;
		for ch:='A' to 'Z' do
			if count[ch]>0 then
				if not ok[ch] then
				begin flag:=false;
					break;
				end;
		if flag then final:=(final+ans[i,0]) mod 12345;
	end;
	writeln(final);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'107D.in');
	reset(input);
{$endif}
lemon;
end.

type
	mat=array[0..123,0..123] of longint;
var
ans,g,rs:mat;

procedure mult(const a,b:mat; all:longint);
var i,j,k:longint;
begin fillchar(rs,sizeof(rs),0);
	for k:=0 to all do
		for j:=0 to all do
			if b[k,j]>0 then
				for i:=0 to all do
					rs[i,j]:=(rs[i,j]+a[i,k]*b[k,j]) mod 12345;
end;

procedure mul(all:longint; y:int64);
var i:longint;
begin for i:=60 downto 0 do if y and (int64(1)<<i)>0 then break;
	ans:=g;
	for i:=i-1 downto 0 do
	begin mult(ans,ans,all); ans:=rs;
		if y and (int64(1)<<i)>0 then begin mult(ans,g,all); ans:=rs; end;
	end;
end;

var
count:array['A'..'Z'] of longint;
rec:array['A'..'Z',0..1010] of longint;
multi,ss,tmp:array[0..1010] of longint;
ok:array['A'..'Z'] of boolean;
crime:array[0..1010] of char;

procedure lemon;
var LenX:int64;
    n,i,all,j,x,k,final:longint;
    ch:char;
    flag:boolean;
begin readln(LenX,n); if n=0 then begin writeln(1); exit; end;
	all:=1;
	for i:=1 to n do
	begin readln(ch,multi[i]); crime[i]:=ch;
		inc(count[ch]); rec[ch,count[ch]]:=i;
		all:=all*multi[i];
	end;
	fillchar(g,sizeof(g),0);
	for i:=0 to all-1 do
	begin x:=i;
		for j:=1 to n do begin ss[j]:=x mod multi[j]; x:=x div multi[j]; end;
		tmp:=ss;
		for ch:='A' to 'Z' do
			if count[ch]>0 then
			begin for k:=1 to count[ch] do
				begin inc(ss[rec[ch,k]]);
					if ss[rec[ch,k]]=multi[rec[ch,k]] then ss[rec[ch,k]]:=0;
				end;
				x:=0;
				for j:=n downto 1 do x:=x*multi[j]+ss[j];
				g[i,x]:=1;
				ss:=tmp;
			end;
	end;
	mul(all-1,LenX); final:=0;
	for i:=0 to all-1 do
	begin x:=i; fillchar(ok,sizeof(ok),false);
		for j:=1 to n do begin if x mod multi[j]=0 then ok[crime[j]]:=true; x:=x div multi[j]; end;
		flag:=true;
		for ch:='A' to 'Z' do
			if count[ch]>0 then
				if not ok[ch] then
				begin flag:=false;
					break;
				end;
		if flag then final:=(final+ans[i,0]) mod 12345;
	end;
	writeln(final);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'107D.in');
	reset(input);
{$endif}
lemon;
end.

type
	mat=array[0..123,0..123] of longint;
var
ans,g,rs:mat;

procedure mult(const a,b:mat; all:longint);
var i,j,k:longint;
begin fillchar(rs,sizeof(rs),0);
	for k:=0 to all do
		for j:=0 to all do
			if b[k,j]>0 then
				for i:=0 to all do
					rs[i,j]:=(rs[i,j]+a[i,k]*b[k,j]) mod 12345;
end;

procedure mul(all:longint; y:int64);
var i:longint;
begin for i:=60 downto 0 do if y and (int64(1)<<i)>0 then break;
	ans:=g;
	for i:=i-1 downto 0 do
	begin mult(ans,ans,all); ans:=rs;
		if y and (int64(1)<<i)>0 then begin mult(ans,g,all); ans:=rs; end;
	end;
end;

var
count:array['A'..'Z'] of longint;
rec:array['A'..'Z',0..1010] of longint;
multi,ss,tmp:array[0..1010] of longint;
ok:array['A'..'Z'] of boolean;
crime:array[0..1010] of char;

procedure lemon;
var LenX:int64;
    n,i,all,j,x,k,final:longint;
    ch:char;
    flag:boolean;
begin readln(LenX,n);
	all:=1;
	for i:=1 to n do
	begin readln(ch,multi[i]); crime[i]:=ch;
		inc(count[ch]); rec[ch,count[ch]]:=i;
		all:=all*multi[i];
	end;
	fillchar(g,sizeof(g),0);
	for i:=0 to all-1 do
	begin x:=i;
		for j:=1 to n do begin ss[j]:=x mod multi[j]; x:=x div multi[j]; end;
		tmp:=ss;
		for ch:='A' to 'Z' do
			if count[ch]>0 then
			begin for k:=1 to count[ch] do
				begin inc(ss[rec[ch,k]]);
					if ss[rec[ch,k]]=multi[rec[ch,k]] then ss[rec[ch,k]]:=0;
				end;
				x:=0;
				for j:=n downto 1 do x:=x*multi[j]+ss[j];
				g[i,x]:=1;
				ss:=tmp;
			end;
	end;
	mul(all-1,LenX); final:=0;
	for i:=0 to all-1 do
	begin x:=i; fillchar(ok,sizeof(ok),false);
		for j:=1 to n do begin if x mod multi[j]=0 then ok[crime[j]]:=true; x:=x div multi[j]; end;
		flag:=true;
		for ch:='A' to 'Z' do
			if count[ch]>0 then
				if not ok[ch] then
				begin flag:=false;
					break;
				end;
		if flag then final:=(final+ans[i,0]) mod 12345;
	end;
	writeln(final);
end;

begin
{$ifndef ONLINE_JUDGE}
	assign(input,'107D.in');
	reset(input);
{$endif}
lemon;
end.


                          const eps=1e-10;
                          modul=1000000007;
                    besk=1000000000000000;
                     label 3,1,2,33,44,66;
                 type
                     mas=array[-200000..2*1100000+1] of int64;
                     zap=record p1,p2,p3 :int64;end; zap1=^zap;

var  pos1,pos2,ii1,jj1,pos,kol1,left,ii,stepp,mm,lavel,l,kol,dl,i1,j1,imin,step,i,j,k,m,n1,k1,w,mesto,jj,tt,pl1,pl2,pl3:longint;
t1,t2,n,kk,t,ll,rr,q,pp,p1,p2,p3,p4,sum,qq,ott,nn,p,ot,pmin,tmax,minf,gr1,gr2,tt1,nnn,d,raz1,raz2,v,s,st,gr:int64;
 flag1:boolean;
 zann:array[-10000..1000000] of boolean;
 pt:zap;
 cha:char;
 ttt:extended;
 matr:array[-1..6001,-1..6001] of integer;
 newoch,och:array[0..9,0..1000002] of longint;
 zan:array[-1..1001,-1..1001] of boolean;
newdloch,dloch,xleft,yleft,xright,yright,olda,bbb,zz,z,b1,a1:array[-100000..300000] of int64;
ddx,ddy,dd:array [0..500000,-1..9] of int64;
pole:array[-10..1000,-10..1000] of char;
newpole:array[-1..1001,-1..1001] of longint;
bbbb,cccc:array[0..100000] of extended;
 ss2,ss,ss1:string;
aa,bb,cc,t22,t3:int64;
xlmax,xlmin,xrmax,xrmin,ylmax,ylmin,yrmax,yrmin,xx,yy,x,y,mnog,stepen:array[-400000..400000] of int64;
ppp, c,a,b:mas; flag:boolean;  pl:longint;
function srav(i,j:longint):boolean;
begin if (a[i]<a[j]){or((a[i]=a[j])and(a1[i]<a1[j]))} then srav:=true else srav:=false;  end;
procedure sl (k,l,m:longint);  {k- dlina, l,m - nachalo dvuh blokov}
begin
i:=l;j:=m; flag:=true; step:=0;while flag do begin
if srav(i,j) then begin begin b[l+step]:=a[i];{b1[l+step]:=a1[i];}end;
inc(step);inc(i);
{writeln('step=',step,' i=',i);} end;
if not srav(i,j) then begin begin b[l+step]:=a[j];{b1[l+step]:=a1[j];}end;
inc(step);inc(j);end;
if  ((i>=l+k) or (j>=m+k)) then flag:=false;
end;
if(i>=l+k) then repeat b[l+step]:=a[j];{b1[l+step]:=a1[j];}inc(step);inc(j);until (j>=m+k);
if(j>=m+k) then repeat b[l+step]:=a[i];{b1[l+step]:=a1[i];}inc(step);inc(i);until (i>=l+k);
end;
procedure sort(var c:mas);

begin

k:=1; repeat
l:=1;m:=k+l; repeat sl(k,l,m); l:=l+2*k;m:=m+2*k; until (l>=n); k:=k*2;
for i:=1 to n do begin a[i]:=b[i];{a1[i]:=b1[i];} end;
until (k>=n);
end;

 function  max(a,b:int64):int64;
 begin max:=a; if b>a then max:=b; end;
  function  min(a,b:int64):int64;
 begin min:=a; if b<a then min:=b; end;
function gcd(a,b:int64):int64; var p,t:int64;
begin if (a>0)and(b>0) then
   begin while (b>0) do begin t:=a mod b;a:=b;b:=t;    end;
    p:=a;
   end else if a=0 then p:=b else p:=a;
gcd:=p;
end;

function degg(modul,a:int64;k:int64):int64; var p:int64;
begin
 if k<=0 then degg:=1 else if odd(k) then degg:=(degg(modul,a,k-1)*a) mod modul  else
   begin  p:=degg(modul,a,k div 2); degg:=(p*p) mod modul;    end;

end;

 function evkl(a,b:int64):zap1; var olda,oldb,d,x,y,r,q,i,j,k,x1,x2,y1,y2:int64;
  tt:zap; tt1:zap1;
begin
olda:=a; oldb:=b;
  if a<b then begin r:=a;a:=b;b:=r; end;
  x2:=1;x1:=0;y2:=0;y1:=1;
  while b>0 do
    begin q:=a div b; r:=a mod b; x:=x2-q*x1;y:=y2-q*y1;
          a:=b;b:=r;x2:=x1;x1:=x;y2:=y1;y1:=y;
    end;
    d:=a; x:=x2;y:=y2;
  {    writeln('finish  a= ',a,' x= ',x,' y= ',y);
  }  if (olda*x+oldb*y)<>a then begin {writeln('change');} q:=x; x:=y; y:=q;  end;
     tt.p1:=d;tt.p2:=x;tt.p3:=y; tt1:=@tt;
   evkl:=tt1;
end;

 function prov(razm:longint):boolean;
 var ii,jj,i,j,t:longint;ot:boolean;
begin
  ot:=true;
  t:=n div razm;
  for ii:=1 to t do for jj:=1 to t do
    begin
       for i:=1 to razm do for j:=1 to razm do
       if matr[(ii-1)*razm+i,(jj-1)*razm+j]<>matr[(ii-1)*razm+1,(jj-1)*razm+1]
       then ot:=false;

    end;

  prov:=ot;
end;

function anss(n:int64):int64;
var ot,p,pp:int64;
begin
   p:=(n-1) div 2;
   if odd(n) then ot:=degg(modul,2,p) else ot:=3*degg(modul,2,p-1) mod modul;
   if n=2 then ot:=1;
     anss:=ot;
end;

begin
  { assign (input,'E:\Projects\input.txt');reset(input);
      assign (output,'E:\Projects\output.txt'); rewrite(output);
   }

    readln(ss1);
    readln(ss2);
    pos:=0;
    p1:=length(ss1);
    p2:= length(ss2);
    for i:=1 to p2 do
    begin
      repeat inc(pos) until ss1[pos]=ss2[i];
      x[i]:=pos;
    end;
   x[p2+1]:=p1+1;
   y[p2+1]:=p1+1;

    pos:=p1+1;

     for i:=p2 downto 1 do
    begin
      repeat dec(pos) until ss1[pos]=ss2[i];
      y[i]:=pos;
    end;

   { for i:=0 to p2+1 do write(x[i],' '); writeln;
    for i:=0 to p2+1 do write(y[i],' '); writeln;
    }
    for i:=1 to p2+1 do ot:=max(ot,x[i]-x[i-1]);
     for i:=1 to p2+1 do ot:=max(ot,y[i]-y[i-1]);
     for i:=1 to p2+1 do ot:=max(ot,y[i]-x[i-1]);

     writeln(ot-1);

 1: close (output);
end.

const
  maxl = 200002;
var
  suf, pre : array[0..maxl-1] of longint;
  s, t : ansistring;
  i, slen, tlen, j, tmp, ans : longint;

function max(a, b : longint) : longint;
begin
  if a > b then exit(a);
  exit(b);
end;

begin
  readln(s);
  readln(t);
  slen := length(s);
  tlen := length(t);
  j := 1;
  for i := 1 to slen do
  begin
    if (j <= tlen) and (s[i] = t[j]) then
      inc(j);
    pre[i] := j;
  end;
  j := tlen;
  for i := slen downto 1 do
  begin
    if (j >= 1) and (s[i] = t[j]) then
      dec(j);
    suf[i] := j;
  end;
  suf[slen+1] := tlen;
  ans := 0; j := 1; tmp := 1;
  for i := 1 to slen + 1 do
  begin
    while (j <= slen + 1) and (tmp >= suf[j] + 1) do inc(j);
    ans := max(ans, j - i - 1);
    tmp := pre[i];
  end;
  writeln(ans);
end.

var f1,f2:text;
s1,s2:ansistring;
i,j,n,m:longint;
f:array[0..1000,0..1000] of longint;
t:array[0..1000] of longint;
function max(a,b:longint):longint;
begin
if a<b then exit(b); exit(a);
end;
   begin
        assign(f1,'');
        reset(f1);
        assign(f2,'');
        rewrite(f2);
        readln(f1,s1);
        readln(f1,s2);
        n:=length(s1);
        m:=length(s2);
        for i:=1 to n do
        f[i,0]:=i;
        for j:=1 to m do
        t[j]:=99999;
        for j:=1 to m do
        for i:=t[j-1]+1 to n do
        begin
                if s1[i]=s2[j] then
                begin
                if t[j]=99999 then t[j]:=i;
                f[i][j]:=max(f[i-1][j-1],i-t[j]);
                if f[i][j]<f[i-1][j] then f[i][j]:=f[i-1][j];
                end else
                f[i,j]:=max(f[i-1][j],i-t[j]);
        end;
      {  for j:=1 to m do
        begin
        for i:=1 to n do
        write(f2,f[i,j],' ');
        writeln(f2);
        end; }
        writeln(f2,f[n,m]);
        close(f1);
        close(f2);
        end.

                          const eps=1e-10;
                          modul=1000000007;
                    besk=1000000000000000;
                     label 3,1,2,33,44,66;
                 type
                     mas=array[-200000..2*1100000+1] of int64;
                     zap=record p1,p2,p3 :int64;end; zap1=^zap;

var  pos1,pos2,ii1,jj1,pos,kol1,left,ii,stepp,mm,lavel,l,kol,dl,i1,j1,imin,step,i,j,k,m,n1,k1,w,mesto,jj,tt,pl1,pl2,pl3:longint;
t1,t2,n,kk,t,ll,rr,q,pp,p1,p2,p3,p4,sum,qq,ott,nn,p,ot,pmin,tmax,minf,gr1,gr2,tt1,nnn,d,raz1,raz2,v,s,st,gr:int64;
 flag1:boolean;
 zann:array[-10000..1000000] of boolean;
 pt:zap;
 cha:char;
 ttt:extended;
 matr:array[-1..6001,-1..6001] of integer;
 newoch,och:array[0..9,0..1000002] of longint;
 zan:array[-1..1001,-1..1001] of boolean;
newdloch,dloch,xleft,yleft,xright,yright,olda,bbb,zz,z,b1,a1:array[-100000..300000] of int64;
ddx,ddy,dd:array [0..500000,-1..9] of int64;
pole:array[-10..1000,-10..1000] of char;
newpole:array[-1..1001,-1..1001] of longint;
bbbb,cccc:array[0..100000] of extended;
 ss2,ss,ss1:string;
aa,bb,cc,t22,t3:int64;
xlmax,xlmin,xrmax,xrmin,ylmax,ylmin,yrmax,yrmin,xx,yy,x,y,mnog,stepen:array[-400000..400000] of int64;
ppp, c,a,b:mas; flag:boolean;  pl:longint;
function srav(i,j:longint):boolean;
begin if (a[i]<a[j]){or((a[i]=a[j])and(a1[i]<a1[j]))} then srav:=true else srav:=false;  end;
procedure sl (k,l,m:longint);  {k- dlina, l,m - nachalo dvuh blokov}
begin
i:=l;j:=m; flag:=true; step:=0;while flag do begin
if srav(i,j) then begin begin b[l+step]:=a[i];{b1[l+step]:=a1[i];}end;
inc(step);inc(i);
{writeln('step=',step,' i=',i);} end;
if not srav(i,j) then begin begin b[l+step]:=a[j];{b1[l+step]:=a1[j];}end;
inc(step);inc(j);end;
if  ((i>=l+k) or (j>=m+k)) then flag:=false;
end;
if(i>=l+k) then repeat b[l+step]:=a[j];{b1[l+step]:=a1[j];}inc(step);inc(j);until (j>=m+k);
if(j>=m+k) then repeat b[l+step]:=a[i];{b1[l+step]:=a1[i];}inc(step);inc(i);until (i>=l+k);
end;
procedure sort(var c:mas);

begin

k:=1; repeat
l:=1;m:=k+l; repeat sl(k,l,m); l:=l+2*k;m:=m+2*k; until (l>=n); k:=k*2;
for i:=1 to n do begin a[i]:=b[i];{a1[i]:=b1[i];} end;
until (k>=n);
end;

 function  max(a,b:int64):int64;
 begin max:=a; if b>a then max:=b; end;
  function  min(a,b:int64):int64;
 begin min:=a; if b<a then min:=b; end;
function gcd(a,b:int64):int64; var p,t:int64;
begin if (a>0)and(b>0) then
   begin while (b>0) do begin t:=a mod b;a:=b;b:=t;    end;
    p:=a;
   end else if a=0 then p:=b else p:=a;
gcd:=p;
end;

function degg(modul,a:int64;k:int64):int64; var p:int64;
begin
 if k<=0 then degg:=1 else if odd(k) then degg:=(degg(modul,a,k-1)*a) mod modul  else
   begin  p:=degg(modul,a,k div 2); degg:=(p*p) mod modul;    end;

end;

 function evkl(a,b:int64):zap1; var olda,oldb,d,x,y,r,q,i,j,k,x1,x2,y1,y2:int64;
  tt:zap; tt1:zap1;
begin
olda:=a; oldb:=b;
  if a<b then begin r:=a;a:=b;b:=r; end;
  x2:=1;x1:=0;y2:=0;y1:=1;
  while b>0 do
    begin q:=a div b; r:=a mod b; x:=x2-q*x1;y:=y2-q*y1;
          a:=b;b:=r;x2:=x1;x1:=x;y2:=y1;y1:=y;
    end;
    d:=a; x:=x2;y:=y2;
  {    writeln('finish  a= ',a,' x= ',x,' y= ',y);
  }  if (olda*x+oldb*y)<>a then begin {writeln('change');} q:=x; x:=y; y:=q;  end;
     tt.p1:=d;tt.p2:=x;tt.p3:=y; tt1:=@tt;
   evkl:=tt1;
end;

 function prov(razm:longint):boolean;
 var ii,jj,i,j,t:longint;ot:boolean;
begin
  ot:=true;
  t:=n div razm;
  for ii:=1 to t do for jj:=1 to t do
    begin
       for i:=1 to razm do for j:=1 to razm do
       if matr[(ii-1)*razm+i,(jj-1)*razm+j]<>matr[(ii-1)*razm+1,(jj-1)*razm+1]
       then ot:=false;

    end;

  prov:=ot;
end;

function anss(n:int64):int64;
var ot,p,pp:int64;
begin
   p:=(n-1) div 2;
   if odd(n) then ot:=degg(modul,2,p) else ot:=3*degg(modul,2,p-1) mod modul;
   if n=2 then ot:=1;
     anss:=ot;
end;

begin
  { assign (input,'E:\Projects\input.txt');reset(input);
      assign (output,'E:\Projects\output.txt'); rewrite(output);
   }

    readln(ss1);
    readln(ss2);
    pos:=0;
    p1:=length(ss1);
    p2:= length(ss2);
    for i:=1 to p2 do
      repeat inc(pos) until ss1[pos]=ss2[i];

     { writeln('pos= ',pos );
     } t1:=p1-pos;

    pos:=p1+1;

     for i:=p2 downto 1 do
      repeat dec(pos) until ss1[pos]=ss2[i];

      t2:=pos-1;

     { writeln('t1= ',t1,'  t2= ',t2);
     } writeln(max(t1,t2));

 1: close (output);
end.

                          const eps=1e-10;
                          modul=1000000007;
                    besk=1000000000000000;
                     label 3,1,2,33,44,66;
                 type
                     mas=array[-200000..2*1100000+1] of int64;
                     zap=record p1,p2,p3 :int64;end; zap1=^zap;

var  pos1,pos2,ii1,jj1,pos,kol1,left,ii,stepp,mm,lavel,l,kol,dl,i1,j1,imin,step,i,j,k,m,n1,k1,w,mesto,jj,tt,pl1,pl2,pl3:longint;
t1,t2,n,kk,t,ll,rr,q,pp,p1,p2,p3,p4,sum,qq,ott,nn,p,ot,pmin,tmax,minf,gr1,gr2,tt1,nnn,d,raz1,raz2,v,s,st,gr:int64;
 flag1:boolean;
 zann:array[-10000..1000000] of boolean;
 pt:zap;
 cha:char;
 ttt:extended;
 matr:array[-1..6001,-1..6001] of integer;
 newoch,och:array[0..9,0..1000002] of longint;
 zan:array[-1..1001,-1..1001] of boolean;
newdloch,dloch,xleft,yleft,xright,yright,olda,bbb,zz,z,b1,a1:array[-100000..300000] of int64;
ddx,ddy,dd:array [0..500000,-1..9] of int64;
pole:array[-10..1000,-10..1000] of char;
newpole:array[-1..1001,-1..1001] of longint;
bbbb,cccc:array[0..100000] of extended;
 ss2,ss,ss1:string;
aa,bb,cc,t22,t3:int64;
xlmax,xlmin,xrmax,xrmin,ylmax,ylmin,yrmax,yrmin,xx,yy,x,y,mnog,stepen:array[-400000..400000] of int64;
ppp, c,a,b:mas; flag:boolean;  pl:longint;
function srav(i,j:longint):boolean;
begin if (a[i]<a[j]){or((a[i]=a[j])and(a1[i]<a1[j]))} then srav:=true else srav:=false;  end;
procedure sl (k,l,m:longint);  {k- dlina, l,m - nachalo dvuh blokov}
begin
i:=l;j:=m; flag:=true; step:=0;while flag do begin
if srav(i,j) then begin begin b[l+step]:=a[i];{b1[l+step]:=a1[i];}end;
inc(step);inc(i);
{writeln('step=',step,' i=',i);} end;
if not srav(i,j) then begin begin b[l+step]:=a[j];{b1[l+step]:=a1[j];}end;
inc(step);inc(j);end;
if  ((i>=l+k) or (j>=m+k)) then flag:=false;
end;
if(i>=l+k) then repeat b[l+step]:=a[j];{b1[l+step]:=a1[j];}inc(step);inc(j);until (j>=m+k);
if(j>=m+k) then repeat b[l+step]:=a[i];{b1[l+step]:=a1[i];}inc(step);inc(i);until (i>=l+k);
end;
procedure sort(var c:mas);

begin

k:=1; repeat
l:=1;m:=k+l; repeat sl(k,l,m); l:=l+2*k;m:=m+2*k; until (l>=n); k:=k*2;
for i:=1 to n do begin a[i]:=b[i];{a1[i]:=b1[i];} end;
until (k>=n);
end;

 function  max(a,b:int64):int64;
 begin max:=a; if b>a then max:=b; end;
  function  min(a,b:int64):int64;
 begin min:=a; if b<a then min:=b; end;
function gcd(a,b:int64):int64; var p,t:int64;
begin if (a>0)and(b>0) then
   begin while (b>0) do begin t:=a mod b;a:=b;b:=t;    end;
    p:=a;
   end else if a=0 then p:=b else p:=a;
gcd:=p;
end;

function degg(modul,a:int64;k:int64):int64; var p:int64;
begin
 if k<=0 then degg:=1 else if odd(k) then degg:=(degg(modul,a,k-1)*a) mod modul  else
   begin  p:=degg(modul,a,k div 2); degg:=(p*p) mod modul;    end;

end;

 function evkl(a,b:int64):zap1; var olda,oldb,d,x,y,r,q,i,j,k,x1,x2,y1,y2:int64;
  tt:zap; tt1:zap1;
begin
olda:=a; oldb:=b;
  if a<b then begin r:=a;a:=b;b:=r; end;
  x2:=1;x1:=0;y2:=0;y1:=1;
  while b>0 do
    begin q:=a div b; r:=a mod b; x:=x2-q*x1;y:=y2-q*y1;
          a:=b;b:=r;x2:=x1;x1:=x;y2:=y1;y1:=y;
    end;
    d:=a; x:=x2;y:=y2;
  {    writeln('finish  a= ',a,' x= ',x,' y= ',y);
  }  if (olda*x+oldb*y)<>a then begin {writeln('change');} q:=x; x:=y; y:=q;  end;
     tt.p1:=d;tt.p2:=x;tt.p3:=y; tt1:=@tt;
   evkl:=tt1;
end;

 function prov(razm:longint):boolean;
 var ii,jj,i,j,t:longint;ot:boolean;
begin
  ot:=true;
  t:=n div razm;
  for ii:=1 to t do for jj:=1 to t do
    begin
       for i:=1 to razm do for j:=1 to razm do
       if matr[(ii-1)*razm+i,(jj-1)*razm+j]<>matr[(ii-1)*razm+1,(jj-1)*razm+1]
       then ot:=false;

    end;

  prov:=ot;
end;

function anss(n:int64):int64;
var ot,p,pp:int64;
begin
   p:=(n-1) div 2;
   if odd(n) then ot:=degg(modul,2,p) else ot:=3*degg(modul,2,p-1) mod modul;
   if n=2 then ot:=1;
     anss:=ot;
end;

begin
  { assign (input,'E:\Projects\input.txt');reset(input);
      assign (output,'E:\Projects\output.txt'); rewrite(output);
   }

    readln(ss1);
    readln(ss2);
    pos:=0;
    p1:=length(ss1);
    p2:= length(ss2);
    for i:=1 to p2 do
    begin
      repeat inc(pos) until ss1[pos]=ss2[i];
      x[i]:=pos;
    end;
   x[p2+1]:=p1+1;
   y[p2+1]:=p1+1;

    pos:=p1+1;

     for i:=p2 downto 1 do
    begin
      repeat dec(pos) until ss1[pos]=ss2[i];
      y[i]:=pos;
    end;

   { for i:=0 to p2+1 do write(x[i],' '); writeln;
    for i:=0 to p2+1 do write(y[i],' '); writeln;
    }
    for i:=1 to p2+1 do ot:=max(ot,x[i]-x[i-1]);
     for i:=1 to p2+1 do ot:=max(ot,y[i]-y[i-1]);

     writeln(ot-1);

 1: close (output);
end.


var
x,h:int64;
i:longint;
function nok(a,b:int64):int64;
var
c,x,y:int64;
begin
x:=a;
y:=b;
while b<>0 do
begin
c:=a mod b;
a:=b;
b:=c;
end;
nok:=x*y div a;
end;
begin
readln(x);
if x=1 then
begin
writeln(1,' ',1);
exit;
end;
for i:= 1 to trunc(sqrt(x))+1 do
begin
if (x mod i = 0) and (i<>x div i) then
if nok(x div i,i)=x then
h:=i;
end;
writeln(x div h,' ',h{,' ',nok(x div h,h)});
end.

program canh_toan;
uses math;
var x:int64;
function gcd(a,b:int64):int64;
var r:int64;
begin
  while (a mod b<>0) do
    begin
      r:=a mod b;
      a:=b;
      b:=r;
    end;
  exit(b);
end;
function lcm(a,b:int64):int64;
begin
  exit((a*b) div gcd(a,b));
end;
procedure main();
var i:longint;
    j,a,b:int64;
begin
read(x);
for i:=trunc(sqrt(x)) downto 1 do
 if (x mod i=0) then
    begin
      j:=x div i;
      if (lcm(i,j)=x) then
         begin
           a:=i;
           b:=j;
           break;
         end;
    end;
write(a,' ',b);
end;
BEGIN
  main();
END.

var
  x, a1,a2,minn:int64;
  i:longint;
function max(a, b:int64):int64;
begin
  if a>b then max:=a else max:=b;
end;
function nod(a,b:int64):int64;
begin
  if (a=0)or(b=0) then nod:=b
  else nod:=nod(b mod a, a);
end;
begin
  readln(x);
  minn:=100000000000000;
  for i:=1 to trunc(sqrt(x)) do
  begin
    if (x mod i=0)and(max(i, x div i)<minn)and(nod(i, x div i)=1) then
    begin
      a1:=i; a2:=x div i;
      minn:=max(i, x div i);
    end;
  end;
  writeln(a1,' ',a2);
end.

program C1285;
const fi='';
      fo='';
var g,f:text;i,n:longint;
    x:qword;
function lcm(a,b:qword):qword;
var tmp,a1,b1,ans:qword;
begin
        a1:=a;
        b1:=b;
        while b>0 do
                begin
                        a:=a mod b;
                        tmp:=a;
                        a:=b;
                        b:=tmp;
                end;
        ans:=(a1 div a)*b1;
        exit(ans);
end;
begin
        assign(g,fi);reset(g);
        assign(f,fo);rewrite(f);
        readln(g,x);
        for i:=trunc(sqrt(x)) downto 1 do
                begin
                        if (x mod i=0) and (x mod (x div i)=0) and (lcm(i,(x div i))=x)then
                                begin
                                        write(f,i,' ',(x div i));
                                        break;
                                end;
                end;
        close(g);close(f);
end.

          const eps=1e-14;
          besk=2000000000000000000; label 4,1,3,2,44,22;
          type mas=array[-1000000..1200000+1] of int64;
          integer=longint;     zap=record  x,y,z:int64; end;
           zap1=^zap;
            var qqq,oldpos,jj,kolvzv,sdvig,dl,len,i1,j1,mask,stepp,n2,i,j,k,l,step,k1,mesto,num:longint;
           ss3,ss,ss1,ss2:string;
           pqqq:zap;  bet,al:extended;
           drans,dr3,dr1,dr2,pt,ott1,ott2:zap;
          ver:array[-1..500,-1..5] of extended;
           verugad:array[-2..200,-2..200,-2..200] of extended;
          stepen,delit,pq,pp1,pp2,period, dolg,ot1,ot2,oldgr,predsum,pmin,tmin,pos,aa,bb,kool,sum,number,imin,kolreber,kolstan,qq,kol,chisl,znam,n,m,ttt,numpos,vx,vy,ans,up,down,ymin,ymax,imax,t3,qq1,qq2,q1,q2,ost,koll,w,dx,dy,time,p3,p4,qqqq,ii,tt,pmax,pmax2,p1,p2,ot,ott,hh,pos0,pos1,pos2,pp,mm,maxot,tmax,nn,kk,modul,t0,n1,q,t,p:int64; flag1:boolean;
          fact,z,stan,a2,invx,invy,summ,place,str,b2,z1,sum1,sum0,d0,d1,ccc,c,d,a,b:mas; flag2,flag:boolean;  pl:longint;
          ans1,posmax,zapas,razn,oldll,oldrr,ppp,shag,posx,posy,minx,miny,maxx,maxy,kolplus,kolminus,razr,jmax,gr,grr,ener,dob,cc,dist,tt3,tt1,tt2,ugg,aa1,aa2,r,ug:int64;
          zzan,prost,marked,zan:array[-1000000..1000000] of boolean;
           razl,start,finish,left,right,zz,ll,rr,zpl,zmi,path,lx,a3,a4,predok,a1,b1,strok:array[-1000000..1500000] of int64;
           ppole1,ppole2,ppole:array[-1000000..3000000] of char;
          bufer, str1,str2,stroka:array[-1000..1000000] of char;
         dinam:array[-100000..1000000] of extended;
         t1,t2,al1,bet1,cosa,sina,cosb,sinb,rx,ry,sdvigx,sdvigy,px,py,h1,h2,h,l1,l2,otmax,v:int64;
          dd1,dd2,dd,ssumitog,profil:array[-100..110,-100..110,-100..110] of int64;
          sss1,sss2,sp,sss:array[-10..2000000] of string;
          newpole,pole:array[-1..2100,-1..2700] of char;
          verch,verzn:array[-10..1100000] of int64;
          ddd,ddinv:array[-1..560000,-1..4] of int64;
          poss:int64;
          obmen,ch:char;

          ff,flagg1:boolean;
          oldx,oldy,numx,numy,xleft,xright,s,znach,nomer,newplace,newstr,next,prev,xx,yy,aold,x,y:array[-10000..1700002] of int64;
          dp:array[0..3,-1..910,0..12] of int64;
           function srav(i,j:longint):boolean; label 44;
                begin
                 if (a[i]<a[j]) then begin srav:=true; goto 44;  end;
                if a[i]>a[j] then begin  srav:=false; goto 44;   end;
                 if a[i]=a[j] then if a1[i]<a1[j] then srav:=true else srav:=false;
                     44:
                end;

              procedure sl (k,l,m:integer);  {k- dlina, l,m - nachalo dvuh blokov}
                 begin
                i:=l;j:=m; flag:=true; step:=0;while flag do begin
               if (srav(i,j)) then begin begin b[l+step]:=a[i];b1[l+step]:=a1[i];b2[l+step]:=a2[i];end;
                inc(step);inc(i);
                {writeln('step=',step,' i=',i);} end;
               if (not srav(i,j)) then begin begin b[l+step]:=a[j];b1[l+step]:=a1[j];b2[l+step]:=a2[j];end;
               inc(step);inc(j);end;
               if  ((i>=l+k) or (j>=m+k)) then flag:=false;
                  end;
                   if(i>=l+k) then repeat b[l+step]:=a[j];b1[l+step]:=a1[j];b2[l+step]:=a2[j];inc(step);inc(j);until (j>=m+k);
                    if(j>=m+k) then repeat b[l+step]:=a[i];b1[l+step]:=a1[i];b2[l+step]:=a2[i];inc(step);inc(i);until (i>=l+k);
                   end;
             procedure sort(var c:mas);
                    begin

                       k:=1; repeat
                       l:=1;m:=k+l; repeat sl(k,l,m); l:=l+2*k;m:=m+2*k; until (l>=n); k:=k*2;
                      for i:=1 to n do begin a[i]:=b[i];a1[i]:=b1[i];a2[i]:=b2[i]; end;
                         until (k>=n);

                      end;

             function min (a,b:int64):int64;begin min:=b;if a<b then min:=a;  end;
             function max (a,b:int64):int64;begin max:=b;if a>b then max:=a;  end;

        {   function evkl(a,b:int64):zap1; var d,x,y,r,q,i,j,k,x1,x2,y1,y2:int64;
           tt:zap; tt1:zap1;  fl:boolean;
        begin
           fl:=false;
           if a<b then begin r:=a;a:=b;b:=r; fl:=true; end;
           x2:=1;x1:=0;y2:=0;y1:=1;
           while b>0 do
              begin q:=a div b; r:=a mod b; x:=x2-q*x1;y:=y2-q*y1;
                   a:=b;b:=r;x2:=x1;x1:=x;y2:=y1;y1:=y;
              end;
            if not fl then begin d:=a; x:=x2;y:=y2; tt.p1:=d;tt.p2:=x;tt.p3:=y; tt1:=@tt; end else
                     begin d:=a; x:=x2;y:=y2; tt.p1:=d;tt.p2:=y;tt.p3:=x; tt1:=@tt; end;
                  evkl:=tt1;
        end;

         }
     function phi(a,b:double):double; label 222;var p:double;
       begin
        if (a>0)and (b>=0) then begin p:=arctan(b/a);goto 222; end;
        if (a<0) then begin p:=pi+arctan(b/a);goto 222; end;
        if (a>0)and (b<0) then begin p:=2*pi+arctan(b/a);goto 222; end;
        if (a=0)and (b>0) then begin p:=pi/2;goto 222; end;
        if (a=0)and (b<0) then begin p:=3*pi/2;goto 222; end;
        222: phi:=p;
      end;

  function nod(a,b:int64):int64; var p,t:int64;
     begin
         a:=abs(a); b:=abs(b);
         if (a>0)and(b>0) then
         begin while (b>0) do begin t:=a mod b;a:=b;b:=t;    end;
          p:=a;
          end else if a=0 then p:=b else p:=a;
         nod:=p;
     end;
      function arccos(tt:extended):extended;
         begin
          if abs(tt)>1-1e-13 then
          if tt>0 then  arccos:=0 else arccos:=pi;
          if tt=0 then arccos:=pi/2 else
          if abs(tt)<=1-1e-13 then if tt>=0 then arccos:=arctan(sqrt(1-tt*tt)/tt)
          else arccos:=pi+arctan(sqrt(1-tt*tt)/tt);

         end;

              function nok(a,b:int64):int64;
               begin  nok:=(a*b) div nod(a,b);  end;

             function degg(a:int64;k:int64):int64; var p:int64;    label 4;
                  begin
                   if k<=0 then degg:=1 else if odd(k) then degg:=(degg(a,k-1)*a) mod modul  else
                     begin  p:=degg(a,k div 2); if p>200000 then
                       begin degg:=200000; goto 4;  end; degg:=(p*p) mod modul;    end;
    4:
                 end;

             function zaprmax(left,right:longint):int64;
                begin
                      if left>=right then zaprmax:=y[left] else
                 begin
                  if not odd(left) and odd(right) then zaprmax:=zaprmax(left shr 1,right shr 1) else
                      begin
                      if odd(left) then zaprmax:=max(zaprmax(left+1,right),y[left]);
                      if not odd(right) then zaprmax:=max(zaprmax(left,right-1),y[right]);
                      end;

                  end;

                 end;

              function zaprmin(left,right:longint):int64;
                begin
                if left>=right then zaprmin:=y[left] else
                   begin
                 if not odd(left) and odd(right) then zaprmin:=zaprmin(left shr 1,right shr 1) else
                    begin
                     if odd(left) then  zaprmin:=min(zaprmin(left+1,right),y[left]);
                     if not odd(right) then zaprmin:=min(zaprmin(left,right-1),y[right]);
                     end;
                   end;

                                end;

                procedure udal(ii:longint);
              begin  next[prev[ii]]:=next[ii];prev[next[ii]]:=prev[ii];  end;

           function nomerr(gr:int64):int64;
            var ot,aa,bb,cc:int64; fl:boolean;
                 begin
                aa:=1; bb:=p;  cc:=aa; fl:=false;
                  while abs(aa-bb)>1 do
                     begin
                       cc:=(aa+bb) shr 1;
                        writeln('cc=++ ',cc);
                      if y[cc]<gr then aa:=cc else bb:=cc;

                     end;
                      writeln(' cc= ',cc);
                   {  cc:=max(cc-3,0);
                       }  repeat inc(cc) until y[cc]>=gr;
                          if y[cc]=gr then fl:=true else fl:=false;
                          if fl then nomerr:=a1[cc] else nomerr:=-1000;
                           nomerr:=cc;
                 end;

           function pprost(pp:int64):boolean;
            var i,j,t:longint; tt:extended; fl:boolean;
               begin
               tt:=pp;
               tt:=sqrt(tt);
               fl:=true;
               for i:=2 to round(tt) do if pp mod i=0 then  fl:=false;
               pprost:=fl;
               end;

           function gl(c:char):boolean;
                  begin
              if (c='a')or (c='o')or(c='e')or(c='i')or(c='u') then gl:=true else gl:=false;
                  end;

    function slog(d1,d2:zap):zap1;
    var t,tt,p,q:int64; ans:zap;
    begin
      p:=d1.x*d2.y+d1.y*d2.x;
      q:=d1.y*d2.y;
      t:=nod(p,q);
      p:=p div t; q:=q div t;
      ans.x:=p mod modul;ans.y:=q mod modul;

        slog:=@ans;
    end;

    function umn(d1,d2:zap):zap1;
    var t,tt,p,q:int64; ans:zap;
    begin
      p:=d1.x*d2.x;
      q:=d1.y*d2.y;
      t:=nod(p,q);
      p:=p div t; q:=q div t;
      ans.x:=p mod modul;ans.y:=q mod modul;

        umn:=@ans;
    end;

        begin
     {   assign (input,'D:\Projects\input.txt');reset(input);
        assign (output,'D:\Projects\output.txt'); rewrite(output);
      }
       readln(tt);
 bet:=tt; bet:=sqrt(bet);
   for i:=1 to round(bet) do if tt mod i=0 then
     begin
        inc(qq);
        x[qq]:=i;
        y[qq]:=tt div i;

     end;

  { for i:=1 to qq do writeln(x[i],' ',y[i]);
   }
   ot:=besk;
   for  i:=1 to qq do
     begin
        t:=nod(x[i],y[i]);

        if t=1 then
     begin
         pp:=max(x[i],y[i]);
        if pp<ot then
          begin
            pp:=max(x[i],y[i]);
            ii:=i;
          end;
      end;

     end;

   writeln(x[ii],' ',y[ii]);

            1: close (output);
        end.

var d,i,j,p: longint;
x,max,k1,k2: int64;
begin
readln(x);
max:=x;
for i:=1 to trunc(sqrt(x)) do
begin
if x mod i = 0 then begin k2:=x div i; for j:=2 to i do if (i mod j=0) and (k2 mod j=0) then begin d:=1; break end;
       if (d=0) and (k2<max) then max:=k2;
       end;
p:=0;
d:=0;
end;
writeln (x div max);
writeln (max);
end.

var x,ans:int64;
    i:longint;
    f:boolean;
function max(a,b:int64):int64;
begin
  if a>b then max:=a else max:=b;
end;
procedure swap(var a,b:int64);
var c:int64;
begin
  c:=a;
  a:=b;
  b:=c;
end;
function nod(a,b:int64):int64;
begin
  if b=0 then nod:=a
  else nod:=nod(b,a mod b);
end;
function nok(a,b:int64):int64;
begin
  if a<b then swap(a,b);
  nok:=(a*b)div nod(a,b);
end;
begin
  read(x);
  ans:=1;
  for i:=2 to round(sqrt(x)+1)do
    begin
    if (x mod i=0)and(nok(i,x div i)=x)and(max(i,x div i)<max(ans,x div ans))then ans:=i;
    end;
  writeln(ans,' ',x div ans);
end.

var a,q:int64;
function nod(a,b:int64):int64;
 var r:int64;
begin
 r:=a mod b;
 while r>0 do
 begin
  a:=b;b:=r;r:=a mod b
 end;
 nod:=b
end;
begin
read(a);if a=1 then write('1 1') else begin
q:=trunc(sqrt(a))+1;
if q*q =a   then
 q:=q-1;
while (a mod q>0)or(nod (a div q , q)>1)  do
 q:=q-1;
write(a div q,' ',q);end
end.

var x,a,b:int64;
    m:array[1..100]of longint;
    kol,i,j:longint;
    f:boolean;
begin
  read(x);
  kol:=0;
  while x>1 do
    begin
    f:=true;
    for i:=2 to round(sqrt(x)+1)do
      begin
      if x mod i = 0 then
        begin
        f:=false;
        x:=x div i;
        inc(kol);
        m[kol]:=i;
        break;
        end;
      end;
    if f then
      begin
      inc(kol);
      m[kol]:=x;
      x:=1;
      end;
    end;
  if kol=1 then
    begin
    write(m[1],' ',1);
    halt;
    end;
  a:=m[1];
  b:=m[kol];
  i:=2;
  j:=kol-1;
  while i<=j do
    begin
    if a<b then
      begin
      a:=a*m[i];
      inc(i);
      end
    else
      begin
      b:=b*m[j];
      dec(j);
      end;
    end;
  write(a,' ',b);
end.

var x,a,b:int64;
    m:array[1..100]of longint;
    q:array[1..100]of int64;
    kol,i,j,kkol:longint;
    f:boolean;
begin
  read(x);
  if x=1 then
    begin
    write('1 1');
    halt;
    end;
  kol:=0;
  while x>1 do
    begin
    f:=true;
    for i:=2 to round(sqrt(x)+1)do
      begin
      if x mod i = 0 then
        begin
        f:=false;
        x:=x div i;
        inc(kol);
        m[kol]:=i;
        break;
        end;
      end;
    if f then
      begin
      inc(kol);
      m[kol]:=x;
      x:=1;
      end;
    end;
  kkol:=0;
  i:=1;
  while i<=kol do
    begin
    if m[i]=m[i+1]then
      begin
      inc(kkol);
      q[kkol]:=m[i];
      for j:=i to kol-1 do
        if m[j]=m[j+1]then begin q[kkol]:=q[kkol]*m[j]; i:=j+1;end else break;
      end
    else
      begin
      inc(kkol);
      q[kkol]:=m[i];
      end;
    inc(i);
    end;
  for i:=1 to kkol do
    for j:=1 to kkol-1 do
      if q[j]>q[j+1]then
        begin
        a:=q[j];
        q[j]:=q[j+1];
        q[j+1]:=a;
        end;
  if kkol=1 then
    begin
    write(q[1],' ',1);
    halt;
    end;
  a:=q[1];
  b:=q[kkol];
  i:=2;
  j:=kkol-1;
  while i<=j do
    begin
    if a<b then
      begin
      a:=a*q[i];
      inc(i);
      end
    else
      begin
      b:=b*q[j];
      dec(j);
      end;
    end;
  write(a,' ',b);
end.

var a,q:int64;
begin
read(a);if a=1 then write('1 1') else begin
q:=trunc(sqrt(a));
if q*q=a then q:=q-1;
while ((a mod q>0)or(q mod(a div q)=0))and(q>1) do q:=q-1;
write(a div q,' ',q);end
end.

var a,q:int64;
begin
read(a);if a=1 then write('1 1') else begin
q:=trunc(sqrt(a))+1;
if q*q =a   then
 q:=q-1;
while ((a mod q>0)or( (a div q)mod q=0))and(q>1) do
 q:=q-1;
write(a div q,' ',q);end
end.

var
  x, a1,a2,minn:int64;
  i:longint;
function max(a, b:int64):int64;
begin
  if a>b then max:=a else max:=b;
end;
function nod(a,b:int64):int64;
begin
  if a=0 then nod:=b
  else nod:=nod(b mod a, a);
end;
begin
  readln(x);
  minn:=10000000000;
  for i:=1 to trunc(sqrt(x)) do
  begin
    if (x mod i=0)and(max(i, x div i)<minn)and(nod(i, x div i)=1) then
    begin
      a1:=i; a2:=x div i;
      minn:=max(i, x div i);
    end;
  end;
  writeln(a1,' ',a2);
end.

program C1285;
const fi='';
      fo='';
var g,f:text;i,j,n:longint;
    x:int64;
function lcm(a,b:longint):longint;
var tmp,a1,b1,ans:longint;
begin
        a1:=a;
        b1:=b;
        while b>0 do
                begin
                        a:=a mod b;
                        tmp:=a;
                        a:=b;
                        b:=tmp;
                end;
        ans:=(a1 div a)*b1;
        exit(ans);
end;
begin
        assign(g,fi);reset(g);
        assign(f,fo);rewrite(f);
        readln(g,x);
        for i:=trunc(sqrt(x)) downto 1 do
                begin
                        if (x mod i=0) and (x mod (x div i)=0) and (lcm(i,(x div i))=x)then
                                begin
                                        write(f,i,' ',(x div i));
                                        break;
                                end;
                end;
        close(g);close(f);
end.

const fi='';
      fo='';
var g,f:text;i,j,n:longint;
    x:qword;
function lcm(a,b:longint):qword;
var tmp,a1,b1,ans:qword;
begin
        a1:=a;
        b1:=b;
        while b>0 do
                begin
                        a:=a mod b;
                        tmp:=a;
                        a:=b;
                        b:=tmp;
                end;
        ans:=(a1 div a)*b1;
        exit(ans);
end;
begin
        assign(g,fi);reset(g);
        assign(f,fo);rewrite(f);
        readln(g,x);
        for i:=trunc(sqrt(x)) downto 1 do
                begin
                        if (x mod i=0) and (x mod (x div i)=0) and (lcm(i,(x div i))=x)then
                                begin
                                        write(f,i,' ',(x div i));
                                        break;
                                end;
                end;
        close(g);close(f);
end.

var
x,h:int64;
i:longint;
begin
readln(x);
if x=1 then
begin
writeln(1,' ',1);
exit;
end;
for i:= 1 to trunc(sqrt(x))+1 do
if (x mod i = 0) and (i<>x div i) then
h:=i;
writeln(x div h,' ',h);
end.

var a,b,d,n,i,j,k1,k2,p,max: longint;
begin
read(n);
max:=n;
for i:=1 to trunc(sqrt(n)) do
begin
if n mod i = 0 then begin k1:=i; k2:=n div i; p:=1; end;
if p=1 then
       begin for j:=2 to k1 do if (k1 mod j=0) and (k2 mod j=0) then d:=1;
       if (d=0) and (k2<max) then max:=k2;
       end;
p:=0;
d:=0;
end;
writeln (n div max, max);
end.

var a,b,d,i,j,k1,k2,p: longint;
x,max: int64;
begin
readln(x);
max:=x;
for i:=1 to trunc(sqrt(x)) do
begin
if x mod i = 0 then begin k1:=i; k2:=x div i; p:=1; end;
if p=1 then
       begin for j:=2 to k1 do if (k1 mod j=0) and (k2 mod j=0) then d:=1;
       if (d=0) and (k2<max) then max:=k2;
       end;
p:=0;
d:=0;
end;
writeln (x div max);
writeln (max);
end.