program EmberAsh;
var
a,b:array[0..10000]of longint;
i,j,k,n,m,ss,max,t1,t2:longint;
BEGIN
assign(input,'chashu.in');reset(input);
assign(output,'chashu.out');rewrite(output);
readln(n);
a[1]:=8;
b[1]:=1;
for i:=2 to n do
begin
t1:=a[i-1]*9+b[i-1];
a[i]:=t1 mod 12345;
t2:=b[i-1]*9+a[i-1];
b[i]:=t2 mod 12345;
end;
writeln(a[n]);
close(input);close(output);
END.