uses
  math;
const
  d:array[1..2,1..4] of longint=((+1,-1,0,0),(0,0,+1,-1));
  l=20;
var
  f:array[1..l,1..l,0..1 shl 8] of longint;
  q:array[1..3,1..1000000] of longint;
  cost,x,y:array[1..l] of longint;
  a:array[1..l,1..l] of char;
  ans,cur,sx,sy,n,m,i,j,k,cnt,nx,ny,nk,st,fn,p:longint;
  c:char;

procedure update(i,j,k,x:longint);
begin
  if (f[i,j,k]=maxlongint) then
  begin
    q[1,fn]:=i;
    q[2,fn]:=j;
    q[3,fn]:=k;
    inc(fn);
  end;
  if (f[i,j,k]<x) then exit;
  f[i,j,k]:=x;
end;

begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do read(a[i,j]);
    readln;
  end;
  for i:=1 to n do for j:=1 to m do
  begin
    if not((48<=ord(a[i,j])) and (ord(a[i,j])<=57)) then continue;
    inc(cnt);
    x[ord(a[i,j])-48]:=i;
    y[ord(a[i,j])-48]:=j;
  end;
  for i:=1 to cnt do readln(cost[i]);
  for i:=1 to n do for j:=1 to m do
  begin
    if (a[i,j]<>'B') then continue;
    inc(cnt);
    x[cnt]:=i;
    y[cnt]:=j;
    cost[cnt]:=-maxlongint;
  end;
  for i:=1 to n do for j:=1 to m do if (a[i,j]='S') then begin sx:=i; sy:=j; end;
  for i:=1 to n do for j:=1 to m do for k:=0 to 1 shl 8 do f[i,j,k]:=maxlongint;
  st:=1; fn:=1;
  update(sx,sy,0,0);
  while (st<>fn) do
  begin
    i:=q[1,st]; j:=q[2,st]; k:=q[3,st]; inc(st);
    for cur:=1 to 4 do
    begin
      nx:=i+d[1,cur];
      ny:=j+d[2,cur];
      if not((1<=nx) and (nx<=n) and (1<=ny) and (ny<=m)) then continue;
      if not((a[nx,ny]='.') or (a[nx,ny]='S')) then continue;
      nk:=k;
      for p:=1 to cnt do if (max(nx,i)=x[p]) and (j=ny) and (j<y[p])
      then
      nk:=nk xor (1 shl (p-1));
      update(nx,ny,nk,f[i,j,k]+1);
    end;
  end;
  ans:=0;
  for k:=0 to 1 shl 8 do
  begin
    if (f[sx,sy,k]=maxlongint) then continue;
    cur:=0;
    c:='+';
    for p:=1 to cnt do if ((k and (1 shl (p-1)))<>0) then
    begin
      if (cost[p]=-maxlongint) then c:='-' else cur:=cur+cost[p];
    end;
    if (c='-') then continue;
    cur:=cur-f[sx,sy,k];
    if (ans<cur) then ans:=cur;
  end;
  writeln(ans);
end.

const
  inf=1000000;
  c:array[1..4,1..2] of longint=((0,1),(0,-1),(1,0),(-1,0));
var
  n,m,i,j,ts,tot,sx,sy:longint;
  map:Array[1..20,1..20] of char;
  b:array[1..8,1..2] of longint;
  v:array[1..8] of longint;
  f:array[1..20,1..20,0..256] of longint;
  que:Array[1..20*20*256,1..5] of longint;

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

procedure work;
var
  head,tail,i,j,k,x,y,z,x1,y1,z1,ans,tmp:longint;
begin
  for i:=1 to n do
    for j:=1 to m do
      for k:=0 to 1 shl tot do
        f[i,j,k]:=inf;
  head:=0; tail:=1; ans:=0; f[sx,sy,0]:=0;
  que[1,1]:=sx; que[1,2]:=sy;
  while head<tail do
  begin
    inc(head); x:=que[head,1]; y:=que[head,2]; z:=que[head,3];
    for k:=1 to 4 do
    begin
      x1:=x+c[k,1]; y1:=y+c[k,2]; z1:=z;
      if (x1<1) or (x1>n) or (y1<1) or (y1>m) or (map[x1,y1]<>'.') then continue;
      if k=1 then
        for i:=1 to tot do
          if (b[i,1]>x) and (b[i,2]=y) then
            z1:=z1 xor (1 shl (i-1));
      if k=2 then
        for i:=1 to tot do
          if (b[i,1]>x1) and (b[i,2]=y1) then
            z1:=z1 xor (1 shl (i-1));
      if f[x1,y1,z1]=inf then
      begin
        f[x1,y1,z1]:=que[head,4]+1;
        inc(tail); que[tail,1]:=x1; que[tail,2]:=y1; que[tail,3]:=z1; que[tail,4]:=f[x1,y1,z1];
      end;
    end;
  end;
  for i:=0 to 1 shl ts-1 do
  begin
    tmp:=-f[sx,sy,i];
    for j:=1 to ts do
      if i and (1 shl (j-1))<>0 then inc(tmp,v[j]);
    ans:=max(ans,tmp);
  end;
  writeln(ans);
end;

begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      read(map[i,j]);
      if map[i,j]='S' then
      begin
        sx:=i; sy:=j; map[i,j]:='.';
      end;
    end;
    readln;
  end;
  for i:=1 to n do
    for j:=1 to m do
      if map[i,j] in ['1'..'8'] then
      begin
        b[ord(map[i,j])-48,1]:=i;
        b[ord(map[i,j])-48,2]:=j;
        if ord(map[i,j])-48>ts then ts:=ord(map[i,j])-48;
      end;
  tot:=ts;
  for i:=1 to n do
    for j:=1 to m do
      if map[i,j]='B' then
      begin
        inc(tot);
        b[tot,1]:=i; b[tot,2]:=j;
      end;
  for i:=1 to ts do readln(v[i]);
  work;
end.

type
  node=record
    x,y:longint;
  end;
  node2=record
    x,y,p:longint;
  end;
var
  g:array[0..21,0..21]of char;
  f:array[1..21,1..21,0..300]of longint;
  q:array[1..21*21*300*5]of node2;
  w:array[1..21]of longint;
  v:array[1..21]of node;
  bomb:array[1..21]of node;
  line:array[1..21]of node;
  tot,cnt1,cnt2,sx,sy,n,m:longint;
  num:array[1..21]of longint;
procedure init;
var
  i,j:longint;
begin
  readln(n,m);
  cnt1:=0;
  cnt2:=0;
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      read(g[i,j]);
      if g[i,j]='S'then
      begin
        sx:=i;
        sy:=j;
      end
      else
      if g[i,j]='B'then
      begin
        inc(cnt2);
        bomb[cnt2].x:=i;
        bomb[cnt2].y:=j;
      end
      else
      if g[i,j]in['1'..'8']then
      begin
        inc(cnt1);
        val(g[i,j],num[cnt1]);
        v[cnt1].x:=i;
        v[cnt1].y:=j;
      end;
    end;
    readln;
  end;
  for i:=1 to cnt1 do readln(w[i]);
  tot:=0;
  for i:=1 to cnt1 do
  begin
    inc(tot);
    line[tot].x:=v[i].x;
    line[tot].y:=v[i].y;
  end;
  for i:=1 to cnt2 do
  begin
    inc(tot);
    line[tot].x:=bomb[i].x;
    line[tot].y:=bomb[i].y;
  end;
end;
function getvalue(x:longint):longint;
var
  i,t:longint;
begin
  t:=0;
  for i:=1 to cnt1 do
    if x and (1 shl (i-1))<>0 then t:=t+w[num[i]];
  for i:=cnt1+1 to cnt1+cnt2 do
    if x and (1 shl (i-1))<>0 then exit(-2147483647);
  exit(t);
end;
function get(x,t:node2):longint;
var
  i,now:longint;
begin
  now:=x.p;
  for i:=1 to cnt1+cnt2 do
  begin
    if x.x<=line[i].x then continue;
    if (x.y<t.y)and(t.y=line[i].y)then now:=now xor (1 shl (i-1));
    if (x.y>t.y)and(x.y=line[i].y)then now:=now xor (1 shl (i-1));
  end;
  exit(now);
end;
procedure main;
var
  t,x:node2;
  sum,p,tt,head,tail,i,ans:longint;
begin
  head:=0;
  tail:=1;
  fillchar(f,sizeof(f),255);
  q[1].x:=sx;
  q[1].y:=sy;
  q[1].p:=0;
  f[sx,sy,0]:=0;
  while head<tail do
  begin
    inc(head);
    x:=q[head];
    for i:=1 to 4 do
    begin
      t:=x;
      case i of
      1:inc(t.x);
      2:dec(t.x);
      3:inc(t.y);
      4:dec(t.y);
      end;
      if (t.x=0)or(t.y=0)or(t.x>n)or(t.y>m)
        or((g[t.x,t.y]<>'S')and(g[t.x,t.y]<>'.'))then continue;
      t.p:=get(x,t);
      if f[t.x,t.y,t.p]<>-1 then continue;
      f[t.x,t.y,t.p]:=f[x.x,x.y,x.p]+1;
      inc(tail);
      q[tail]:=t;
    end;
  end;
  ans:=0;
  for i:=0 to 1 shl (cnt1+cnt2)-1 do
    if f[sx,sy,i]<>-1 then
    begin
      sum:=getvalue(i);
      if sum<0 then continue;
      sum:=sum-f[sx,sy,i];
      if sum>ans then ans:=sum;
    end;
  writeln(ans);
end;
begin

  init;
  main;

end.

const
  way:array[1..4,1..2]of longint=((1,0),(-1,0),(0,1),(0,-1));

type
  re=record
       x,y,k,t:longint;
     end;

var
  n,m,tot,sx,sy,ans:longint;
  a:array[1..20,1..20]of char;
  c:array[1..8]of re;
  income:array[1..8]of longint;
  f:array[1..20,1..20,0..255] of longint;
  q:array[1..200000]of re;

function jd(x,y:longint):boolean;
begin
  if (x<1) or (x>n) or (y<1) or (y>m) then exit(false);
  if (a[x,y]<>'.') and (a[x,y]<>'S') then exit(false);
  exit(true);
end;

procedure bfs;
var
  head,tail,i,j,x,y,k,t2:longint;
  e:re;

begin
  head:=0;
  tail:=1;
  q[1].x:=sx;
  q[1].y:=sy;
  q[1].k:=0;
  q[1].t:=0;

  repeat
    inc(head);
    e:=q[head];
    for i:=1 to 4 do if jd(e.x+way[i,1],e.y+way[i,2]) then begin
      x:=e.x+way[i,1];
      y:=e.y+way[i,2];
      k:=e.k;
      for j:=1 to tot do begin
        if (i=4) and (y=c[j].y) and (x<c[j].x) then k:=k xor c[j].t;
        if (i=3) and (e.y=c[j].y) and (x<c[j].x) then k:=k xor c[j].t;
      end;
      if f[x,y,k]=-1 then begin
        f[x,y,k]:=e.t+1;
        inc(tail);
        q[tail].x:=x;
        q[tail].y:=y;
        q[tail].k:=k;
        q[tail].t:=f[x,y,k];
      end;
    end;
  until head=tail;
  tail:=tail;
end;

procedure init;
var
  i,j,t,t2,k:longint;

begin
  fillchar(f,sizeof(f),255);
  tot:=0;
  t:=0;
  for i:=1 to n do begin
    for j:=1 to m do begin
      read(a[i,j]);
      if (a[i,j]='B') or ((a[i,j]>='0')and (a[i,j]<='9')) then begin
        inc(tot);
        c[tot].x:=i;
        c[tot].y:=j;
        if a[i,j]='B' then c[tot].k:=0
        else c[tot].k:=ord(a[i,j])-48;
        c[tot].t:=1 shl (tot-1);
        if c[tot].k>t then t:=c[tot].k;
      end
      else if a[i,j]='S' then begin
        sx:=i;
        sy:=j;
      end;
    end;
    readln;
  end;
  for i:=1 to t do readln(income[i]);
  f[sx,sy,0]:=0;
  bfs;
  ans:=0;
  for i:=0 to 255 do if f[sx,sy,i]>-1 then begin
      t:=0;
      t2:=0;
      for j:=1 to tot do if i and c[j].t>0 then begin
        if c[j].k=0 then begin
          t2:=-1;
          break;
        end
        else t:=t+income[c[j].k];
      end;
      if t2=-1 then continue;
      t:=t-f[sx,sy,i];
      if t>ans then ans:=t;
    end;
  writeln(ans);
end;

begin
  readln(n,m);
  init;
end.


program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils,math;
 var a,b,mi,ma,k:int64;
begin
readln(a,b);
k:=0;
while (a<>b) do
begin
mi:=min(a,b);
ma:=max(a,b);
if (ma mod mi = 0) then
begin
a:=mi;
b:=a;
k:=k+(ma div mi)-1;
end
else
begin
a:=mi;
b:=ma mod mi;
k:=k+(ma div mi);
end;
end;
writeln(k+1);
end.

program zd1;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;
var
  a,b,c,d,ot:int64;
begin
  readln(a,b);
  ot := 0;
  while (a <> b) do
    begin
      ot := ot + a div b;
      a := a mod b;
      c := max(a,b);
      d := min(a,b);
      a := c;
      b := d;
      if b = 0 then break;
    end;
  if (a = b) and (b <> 0) then inc(ot);
  writeln(ot);
  readln;
  readln;

end.

var a,b,dem,tam:qword;
begin
 readln(a,b);
 dem:=0;
 while a<>0 do
  begin
   inc(dem,a div b);
   a:=a mod b;
   if a<>0 then
    begin
     tam:=a;
     a:=b;
     b:=tam;
    end;
  end;
 writeln(dem);
end.

program abc;
var a,b,count:int64;
begin
readln(a,b);
count:=0;
while (a<>0) and (b<>0) do
  begin
    if a>b then
      begin
        count:=count  + a div b;
        a:=a  mod b;
      end
    else
     begin
        count:=count + b div a;
        b:=b mod a;
      end
  end;
writeln(count);
end.

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  a, b, count : Int64;

begin
  ReadLn(a, b);
  while a <> b do
  begin
    if a > b then
    begin
      if a mod b = 0 then
      begin
        count:=count + a div b;
        break;
      end else
      begin
        count:=count + a div b ;
        a:=a mod b;
      end;
    end else
    begin
       if b mod a = 0 then
       begin
         count:=count + b div a;
         break;
       end else
       begin
       count:=count + b div a;
        b:=b mod a;
       end;
     end;
  end;
  WriteLn(count);
end.

program paper;

var a,b,c,ans:int64;

begin
    readln(a,b);
    ans:=0;
    while ((a>0)and(b>0)) do begin
        if b>a then begin
            c:=a;
            a:=b;
            b:=c;
        end;
        c:=a div b;
        ans:=ans+c;
        a:=a mod b;
    end;
    writeln(ans);
end.

var a,b,c,r:int64;
begin
 reaD(A,b);
 C:=a div b;;
 r:=a mod b;
 while r>0 do
 begin

 a:=b;b:=r;C:=a div b+c;
 r:=a mod b;
 end;
 write(c);
end.

var i,j,k:longint;
   a,b,res:int64;
  procedure max(var a,b:int64);
  var temp:int64;
  begin
    if b>a then begin temp:=a;a:=b;b:=temp;end;
  end;

begin
  readln(a,b);
  while (a<>0)and(b<>0) do
  begin
    max(a,b);
    res:=res+a div b;
    a:=a mod b;
  end;
  writeln(res);
   readln;
end.

var a,b,x:int64;k:int64;
begin
readln(a,b);
while (a<>0) or (b<>0) do
begin
if (a=0) or (b=0) then break
else
if a<b then begin
k:=k+b div a;
b:=b-a*(b div a);
end
else begin
k:=k+a div b;
a:=a-b*(a div b);
end;
end;
writeln(k);
end.

program zad_A;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a, b, res : int64;

procedure swap(var a, b : int64);
  var
    c : int64;
  begin
    c := a;
    a := b;
    b := c;
  end;
begin
  readln(a, b);
  res := 0;
  while(b > 0) do begin
    inc(res, a div b);
    a := a mod b;
    swap(a, b);
  end;

  writeln(res);

  readln(a);
end.

program a1;
var a, b, r: int64;
begin
r:=0;
readln (a, b);
while (a>=1) and (b>=1) and (a<>b) do begin
 if (a>b) then begin
  r:=r+a div b;
  a:=a mod b;
 end
  else
 begin
  r:=r+b div a;
  b:=b mod a;
 end;
 end;
if (a=b) then r:=r+1;
writeln (r);
end.

var      kq,a,b,t:int64;

begin
         readln(a,b);
         kq:=0;
         while b<>0 do
         begin
                inc(kq,a div b);
                a:=a mod b;
                t:=a;
                a:=b;
                b:=t;
         end;
         writeln(kq);
end.

program z1;
 var a,i,e,n:int64;
begin
 readln(a,i);
 while(a<>0)and(i<>0)do
  begin
   n:=n+(a div i);
   a:=a mod i;
   e:=i;
   i:=a;
   a:=e;
  end;
 writeln(n);
end.

program project;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;

var
   a, b, kol :int64;

begin

    readln(a, b);
    kol := 0;
    while (a <> b) and (a > 0) and (b > 0) do
    begin
        if (a > b) then
        begin
            kol := kol + (a div b);
            a := a mod b;
        end
        else
        begin
            kol := kol + (b div a);
            b := b mod a;
        end;
    end;
    if (a <> 0) and (a = b) then
        inc(kol);

    writeln(kol);
end.

program Pr_A;

{$APPTYPE CONSOLE}
var a, b, w, k: int64;

begin
  //assign (input, 'input.txt'); reset (input);
  //assign (output, 'output.txt'); rewrite (output);

  read (a, b);
  k := 1;
  while (a <> 0) and (b <> 0) do
    begin
      if a >= b then
        begin
          k := k + a div b;
          a := a mod b;
        end
      else
        begin
          k := k + b div a;
          b := b mod a;
        end;
    end;

  writeln (k - 1);
  //close (input);
  //close (output);
end.

var a,b,kq:int64;
begin
    readln(a,b);
    kq:=0;
    repeat
    if a>b then
    begin
        kq:=kq+a div b;
        a:=a mod b;
    end
    else if a<b then
    begin
        kq:=kq+b div a;
        b:=b mod a;
    end;
    until((a=b) or (a=0) or (b=0));
    writeln(kq);
end.

program Project296;

{$APPTYPE CONSOLE}

uses
  SysUtils,math;
var a,b,c,d,res:int64;
begin
 // reset(input,'input.txt');
 // rewrite(output,'output.txt');
  read(a,b);
  res:=0;
  While (a>0)and(b>0) do
  begin
   c:=max(a,b);
   d:=min(a,b);
   res:=(c div d)+res;
   a:=c mod d;
   b:=d;
  end;
  if a>0
  then inc(res);
  write(res);
end.

var
  a, b, k, n: int64;
begin
  read(a, b);
  while (a <> 0) and (b <> 0) do
  begin
    if a > b then
    begin
      k := a div b;
      a := a - b * k;
    end
    else
    if b > a then
    begin
      k := b div a;
      b := b - a * k;
    end;
    n := n + k;
  end;
  writeln(n);
end.

var a,b,k:int64;

procedure swap(var a,b:int64);
var x:int64;
begin
 x:=a;
 a:=b;
 b:=x;
end;

begin

readln(a,b);
k:=0;

while b>0 do
begin
 k:=k+a div b;
 a:=a mod b;
 swap(a,b);
end;

writeln(k);

end.

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  a,b,s: int64;
begin
  readln(a,b);
  s:= 0;
  while(a<>0) and (b<>0) do
    begin
      if (a > b)  then begin if(a div b = 0) then begin inc(s); a:= a - b; end else s:= s + a div b; a:= a mod b end
                  else begin if(b div a = 0) then begin inc(s); b:= b - a; end else s:= s + b div a; b:= b mod a end
    end;
  writeln(s);
  readln;
  readln;
end.

var
a,b,k:int64;
begin
readln(a,b);
k:=0;
while (a>0) and (b>0) do
if a>b then begin k:=k+a div b; a:=a mod b; end
else begin k:=k+b div a; b:=b mod a; end;
writeln(k);
end.

var a,b,s,w,k:int64;
begin
read(a,b);
while (a>0)and(b>0) do
begin
if (a>b)then begin
s:=a div b;
a:=a mod b;
end else if (b>a)then begin s:=b div a;
b:=b mod a;
end;
inc(k,s);
end;
write(k);
end.

program CF296A;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var a, b, c, count: int64;
begin
  readln(a,b);
  count:=0;
  repeat
    if a mod b = 0 then
    begin
      count:=count + a div b;
      break;
    end
    else if a > b then
    begin
      count:=count + a div b;
      a:=a mod b;
    end
    else if b > a then
    begin
      count:=count + b div a;
      b:= b mod a;
    end;
    if (b = 0) or (a = 0) then
      break;
  until a = b;
  writeln(count);
  readln;
end.

program beath;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;

var
 a,a1,b,b1,n:int64;

begin
 readln(a,b);
 n:=0;
   while (a <> 0) and (b <> 0) do begin

 if (a = b) then begin
  n:=n+1;
  a:=0;
  b:=0;
  end;

 if a>b then
  begin
    a1:=a mod b;
  n:=n+( a div b);

a:=a1;

end else

if b>a then begin
b1:=b mod a;
n:=n+( b div a);
 b:=b1;
 end;

  end;
 writeln(n);
end.

Program game;
var
a, B, ind:int64;
begin
   read(a);
   read(b);
   ind:=0;
   while (a<>0) and (b<>0) do
   begin
      if a>b then
      begin
         ind:=ind+(a div b);
         a:=a mod b;
      end
      else
      begin
         ind:=ind+(b div a);
         b:=b mod a;
      end;

   end;
   write(ind);
end.

program n1;
var e,a,b,k:int64;
begin
readln (a,b);
k:=k+a div b;

while a mod b <>0 do begin
e:=a mod b;
a:=b;
b:=e;
k:=k+a div b;
end;
writeln (k);

end.

program project;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;

var
   a, b, kol :int64;

begin
    //reset(input, 'bestbuy.in');
    //rewrite(output, 'bestbuy.out');
    readln(a, b);
    kol := 0;
    while (a <> b) and (a > 0) and (b > 0) do
    begin
        if (a > b) then
        begin
            kol := kol + (a div b);
            a := a mod b;
        end
        else
        begin
            kol := kol + (b div a);
            b := b mod a;
        end;
    end;
    if (a <> 0) and (a = b) then
        inc(kol);

    writeln(kol);
    readln;
end.

var q,c,res:int64;
begin
read(q,c);
res:=0;
while ((q>0)and(c>0)) do
begin
if q>c then begin inc(res,q div c); q:=q mod c; end
else begin inc(res,c div q); c:=c mod q; end;
end;
writeln(res);
end.

Var
  a, b, i:Int64;
  flag:boolean;
begin
  Readln(a, b); i:=0;
  Repeat
    if (a>b) then
    begin
      flag:=(a mod b=0);
      i:=i+(a div b);
      a:=a mod b;
    end else
    begin
      flag:=(b mod a=0);
      i:=i+(b div a);
      b:=b mod a;
    end;
  Until flag;
  writeln(i);
end.

var     a, b, d: qword;

procedure input;
begin
        readln(a, b);
end;

procedure swap(var a, b: qword);
var tmp: qword;
begin
        if b > a then
                begin
                        tmp:= a;
                        a:= b;
                        b:= tmp;
                end;
end;

procedure output;
begin
        write(d);
        halt;
end;

procedure proc(a, b: qword);
begin
        if a = 0 then output;
        swap(a, b);
        inc(d, a div b);
        a:= a mod b;
        proc(a, b);
end;

begin
        input;
        d:= 0;
        swap(a, b);
        inc(d, a div b);
        a:= a mod b;
        proc(a, b);
end.

var  ans,a,b,r:int64;
begin
readln(a,b);
while (b<>0) do
 begin
  ans:=ans+ a div b;
  r:= a mod b;
  a:=b;
  b:=r;
 end;
 writeln(ans);
end.

var
  a,b:int64;
  sum:int64;
begin
  readln(a,b);
  sum:=0;
  while true do
    begin
      if b=0 then break;
      inc(sum,a div b);
      a:=a mod b;
      if a=0 then break;
      inc(sum,b div a);
      b:=b mod a;
    end;
  writeln(sum);
end.

{$MODE OBJFPC}
uses math;
const max1=100000;
type  vector=array[0..max1] of integer;
var n,m,k:integer;
    a,b,res,vo:int64;
    fi,fo:textfile;
procedure enter;
  var i,j:integer;
      temp:int64;
    begin
      res:=0;
      readln(fi,a,b);
      a:=max(a,b);
      b:=min(a,b);
      if a=b*2 then
        begin
          writeln(fo,2);
          exit;
        end;
      res:=0;
    while a<>b*2 do
      begin
        res:=res+a div b;
        vo:=a;
        a:=b;
        b:=vo mod b;
        if b=0 then
          begin
            writeln(Fo,res);
            exit;
          end;
      end;
      writeln(Fo,res+2);
    end;
procedure solve;
  var i,j:integer;
    begin
    end;
procedure print;
  var i,j:integer;
    begin
    end;
begin
  assignfile(fi,'');reset(fi);
  assignfile(fo,'');rewrite(Fo);
  enter;
  solve;
  print;
  closefile(fi);
  closefile(Fo);
end.

VAR  a,b,res,tmp:int64;
BEGIN
     READLN(a,b);
     WHILE b<>0 DO
          BEGIN
               res:=res+(a div b);
               tmp:=b;
               b:=a mod b;
               a:=tmp;
          END;
     WRITELN(res);
     READLN;
END.

var d,t:int64;
    a,b,x,y,tmp:int64;

procedure  open;
    begin
//  assign(input,'527A.inp');
    reset(input);
    end;

procedure finish ;
    begin
//  assign(output,'527A.out');
    rewrite(output);
    end;

procedure rd;
    begin
    read(a,b);
    end;

procedure solve;
    begin
    rd;
       // d:=1;
        {if (a=1) and (b=1) then
                begin
                write(1);
                exit;
                end;
        if (b=1) then
                begin
                write(a);
                exit;
                end;  }
    while (b>0) do
        begin
        //inc(d);
        {if (a-b>b) then
            begin
                        x:=a;
                        y:=b;
                        a:=x-y;
            end
        else
            begin
                        x:=a;
                        y:=b;
                        a:=b;
                        b:=x-y;
            end;  }
                d:=(a div b) + d;
                tmp:=b;
                b:=a mod b;
                a:=tmp;
        end;
       // if (a=b) then
            write(d);
    end;

    BEGIN
    open;
    finish;
    solve;
//  close(input);
//  close(output);
    END.

program codeforces_527A;
const Inputfile  = ''{'playpaper.inp'};
      Outputfile = ''{'playpaper.out'};

var a,b,dem : int64;

procedure nhap;
var fi : text;
begin
assign(fi,Inputfile);
reset(fi);
readln(fi,a,b);
close(fi);
end;

procedure xuli;
var tam : int64;
begin
while (b > 0) do
 begin
  dem := dem + (a div b);
  tam := a;
  a := b;
  b := tam mod b;
 end;
end;

procedure xuat;
var fo : text;
begin
assign(fo,Outputfile);
rewrite(fo);
write(fo,dem);
close(fo);
end;

BEGIN
nhap;
xuli;
xuat;
END.

{$mode objfpc}
uses math;
var
  a, b, r, c: int64;
begin
  readln(a,b);
  while b <> 0 do
    begin
      c := c + a div b;
      r := a mod b;
      a := b;
      b := r;
    end;
  writeln(c);
end.

var
 n,m,k:int64;
 begin
  readln(n,m);
  repeat
   if (n=0) or (m=0) then
    begin
     writeln(k);
     exit;
    end;
   if n=m then
   begin
     inc(k);
     writeln(k);
     exit;
    end;
   if n >m then
    begin
     k := k + (n div m);
     n := n mod m;
    end else
    begin
     k := k + (m div n);
     m := m mod n;
    end;
  until 1 >2;
 end.

var
  a, b, c, d: int64;

begin
  readln(a, b);
  c := 0;
  while (a <> 0) and (b <> 0) do begin
    inc(c, a div b);
    d := a;
    a := b;
    b := d mod b;
  end;
  writeln(c);
end.

Var i,n,m,k,a,b:Int64;
  Begin
    Read(a,b);
    While (a<>0) and (b<>0) do
      Begin
      if a>b then Begin i:=i+(a div b); a:=a mod b; end
             else Begin i:=i+(b div a); b:=b mod a; end;
      end;
    Write(i);
  End.

procedure kotak(a,b : int64);
var
        total,temp : int64;
begin
total:=0;
repeat
        total := total + a div b;
        temp := a mod b;
        a := b;
        b := temp
    until b = 0;
    writeln(total);

end;
        var
        a,b : int64;
begin
        readln(a,b);
        kotak(a,b);
end.

var a,b:int64;
Procedure solution;
var k,tmp:int64;
begin
    k:=0;
    while b>0 do
    begin
          k:=k+a div b;
          tmp:=a;
          a:=b; b:=tmp mod b;
    end;
    write(k);
end;
begin
   readln(a,b);
   solution;
   readln;
end.

var
  a,b,ans,t : int64;

begin
  readln(a,b);
  while (a > 0) and (b > 0) do
    begin
      ans:=ans+a div b;
      a:=a-b*(a div b);
      if a < b then begin t:=a; a:=b; b:=t; end;
    end;
  writeln(ans);
end.

{$mode objfpc}
var
  a,b,kq1,kq,t : int64;
begin
  readln(a,b);
  repeat
     kq1 := a div b;
     kq := kq + kq1;
     a := a - b * kq1;
     t := a;
     a := b;
     b := t;
  until (a = 0) or (b=0);
 writeln(kq);
end.

var
a, b : int64;
cnt : int64;

begin
readln(a, b);
cnt := 0;
while ((a > 0) and (b > 0)) do
  begin
  if a >= b then
    begin
    cnt := cnt + a div b;
    a := a mod b;
    end
  else
    begin
    cnt := cnt + b div a;
    b := b mod a;
    end;
  end;
writeln(cnt);
readln; readln
end.

var a,b,s:int64;
begin
  read(a,b);
  while (a<>0)and(b<>0) do
    if a>b then begin s:=s+a div b; a:=a mod b end else begin s:=s+b div a; b:=b mod a end;
  write(s)
end.

{$MODE OBJFPC}
var
    a, b, res: int64;

begin
    read(a,b);
    res := 0;
    if a mod b = 0
    then begin
        write(a div b);
        exit;
    end;
    if b mod a = 0 then begin
        write(b div a);
        exit;
    end;
    repeat
        if a > b then begin
            inc(res,a div b);
            a := a mod b;
        end else
        if a < b then begin
            inc(res,b div a);
            b := b mod a;
        end;
        if a mod b = 0
        then begin
            write(res + a div b);
            exit;
        end;
        if b mod a = 0 then begin
            write(res + b div a);
            exit;
        end;
    until false;
end.

uses math;
var n,m,ans,x:int64;
begin
 readln(n,m);
 if m=1 then
  begin
   writeln(n);
   halt;
  end;
 while (n<>m) do
  begin
   { write(n,' ',m,'|'); }
   x:=max(n,m) div min(n,m)-1;
   if x=0 then x:=1;
   ans:=ans+x;
   if n>m then n:=n-m*x
    else m:=m-n*x;
  end;
 writeln(ans+1);
end.

var a,b,c,d:int64;
begin
readln(a,b);d:=b;
while (true) and (a>=1) and (b>=1) do begin
if (a>=b) then begin c:=c+(a div b); a:=a-b*(a div b); end
else if (b>=a) then begin c:=c+(b div a); b:=b-a*(b div a); end
else break;
end;
writeln(c);
readln;
end.

var a, b, cnt: int64;

procedure swap( var a, b: int64 );
var tmp: int64;
begin
    tmp := a;
    a := b;
    b := tmp;
end;

begin
    readln( a, b );
    cnt := 0;
    while ( (a > 0) and (b > 0) ) do
    begin
        inc( cnt, a div b );
        a := a mod b;
        swap( a, b );
    end;
    writeln( cnt );
end.

var     f: text;
        a,b,s,d: int64;
begin
        assign(f,''); reset(f);
        readln(f,a,b);
        d:=1;
        repeat
                if (a=1) or (b=1) then
                        begin
                                if a>b then d:=d+a-1
                                else d:=d+b-1;
                                break;
                        end;
                if (a mod b=0) or (b mod a=0) then
                        begin
                                if a>b then d:=d+a div b-1
                                else d:=d+b div a-1;
                                break;
                        end;
                if a>b then
                        begin
                                d:=d+a div b;
                                a:=a mod b;
                        end
                else
                        begin
                                d:=d+b div a;
                                b:=b mod a;
                        end;
        until a=b;
        write(d);
        readln;
end.

{$MODE OBJFPC}
Var
  a, b:QWord;
  Count, tg: QWord;
Procedure Swap(x, y: QWord);
Var tg: QWord;
Begin
  tg:=a;
  a:=b;
  b:=tg;
End;
Begin
  Readln(a,b);
    If a mod b=0 then Writeln(a div b)
    Else
    Begin
      While a mod b<>0 do
      Begin
        Count:=Count+a div b;
        a:=a mod b;
        If a<b then Swap(a,b);
      End;
      Writeln(Count+a div b);
    End;
End.

Var
    Ans, a, b: Int64;

Begin
    Read(a, b);
    While (a <> b) and (a <> 0) and (b <> 0) do Begin
        Ans += max(a, b) div min(a, b);
        If a > b then
            a := a mod b
        Else
            b := b mod a
    End;
    Write(Ans)
End.

var
a,b,o:int64;
begin
read(a,b); o:=0;
while (a>0) and (b>0) do
if a>=b then begin
inc(o,a div b);
a:=a mod b; end
else begin inc(o,b div a);
b:=b mod a; end;
write(o); end.

var
        a,b,n,tmp,i:int64;
begin
        readln(a,b);
        while (a<>b) do
        begin
                if (a<b) then
                begin
                        tmp:=a;
                        a:=b;
                        b:=tmp;
                end;
                i:=i+(a div b);
                if (a mod b<>0) then
                        a:=a-((a div b)*b)
                else
                begin
                        a:=b;
                        i:=i-1;
                end;
                if (a<1) then
                begin
                        a:=1;
                        i:=i-1;
                end;
        end;
        writeln(i+1);
end.

function max(a:int64; b:int64) : int64;
begin
     if a>b then max:=a else max:=b;
end;
function min(a:int64; b:int64) : int64;
begin
     if a<b then min:=a else min:=b;
end;
var
     a,b,k,tmp:int64;
begin
     k:=0;
     read(a,b);
     while (a<>0) and (b<>0)do
     begin
          tmp:=a;
          a:=max(a,b);
          b:=min(b,tmp);
          k:=k+a div b;
          tmp:=a;
          a:=b;
          b:=tmp mod b;
     end;
     write(k);

end.

Program Andronic;
var  a,b:qword; n,i:qword;
begin
     read(a,b);
     n:=0;
         repeat begin
                           n:=n+(a div b);
                           i:=b;
                           b:=a mod b;
                           a:=i;
                           end until b=0;

     writeln(n);
     readln;
end.

var a,b,res,p:int64;
 begin
  read(a,b);
  While  (b>0)  do
   begin
    res:=res+a div b;
    p:=b;
    b:=a mod b;
    a:=p;
   end;
  writeln(res);
 end.

var a,b,c,tmp:int64;
begin
	readln(a,b);
	c := 0;
	while (a mod b <> 0) do
	begin
		c := c + (a div b);
		tmp := a;
		a := b;
		b := tmp mod b;
	end;
	c := c + (a div b);
	writeln(c);
end.

var a,b,k,c:int64;
begin
{assign(input,'input.txt');
reset(input);
assign(output,'output.txt');
rewrite(output);}

read(a,b);
k:=0;

while (a>0) and (b>0) do
begin
if (a>b) then begin

   k:=a div b + k;
   a:=a mod b;

end else begin
    k:=b div a + k;
    b:=b mod a;
end;

end;
write(k);
{close(input);
close(output);}
end.

var
  a,b,t,c:int64;
begin
  readln(a,b);
  t:=0;
  while(a>0)and(b>0) do
    begin
      t:=t+a div b;
      c:=a mod b;
      a:=b;
      b:=c;
    end;
  writeln(t);
end.

var a,b,ans:int64;
begin
readln(a,b);

while (a<>b) and (a<>0) and (b<>0) do
 begin
  if a<b then
          begin
           inc(ans,b div a);
           dec(b, a*(b div a));
          end
         else
          begin
           inc(ans,a div b);
           dec(a, b*(a div b));
          end;
 end;
writeln(ans);
end.

const
  fi='';
  fo='';
var f: text;
    a,b,d: int64;
 procedure nhapfile;
  begin
   assign(f,fi); reset(f);
   readln(f,a,b);
   close(f);
  end;

 procedure xuli;
  begin
   d:=0;
   while (a-b)<>0 do
    begin
     if a>b then
      begin
       d:=d+(a div b);
       if (a mod b)<>0 then
       a:=a mod b
       else break;
      end;
     if a<b then
      begin
       d:=d+(b div a);
       if (b mod a)<>0 then
        b:=b mod a
       else break;
      end;
    end;
  end;

 procedure xuatfile;
  begin
  assign(f,fo); rewrite(f);
  write(f,d);
  close(f);
  end;

BEGIN
 NHAPFILE;
 XULI;
 XUATFILE;
END.

var
 a,b:qword;
function ans(a,b:qword):qword;
begin
 if (a=0)or(b=0)then exit(0);
 if a>b then
  ans:=ans(a mod b,b)+(a div b)
 else
  ans:=ans(a,b mod a)+(b div a);
end;
begin
 readln(a,b);
 writeln(ans(a,b));
end.

var
a,b,ans:int64;

begin
read(a,b);
While (a<>0) and (b<>0) do
begin
if a>b then
begin
inc(ans,a div b);
a:=a mod b; end else
begin
inc(ans,b div a);
b:=b mod a;
end;
end;
Writeln(ans);
end.

{$MODE OBJFPC}
program dream;
uses math;
var    a,b,temp,dem:int64;
begin
       readln(a,b);
       temp:=max(a,b);
       b:=min(a,b);
       a:=temp;
       repeat
             inc(dem,a div b);
             a:=a mod b;
             temp:=max(a,b);
             b:=min(a,b);
             a:=temp;
       until (a=0) or (b=0);
       writeln(dem)
end.

program A_527;

var
  a,b,ans,z:int64;

begin
  read(a,b);
  if b>a
      then begin
        a:=z;
        a:=b;
        b:=z;
      end;
  while a mod b<>0 do
  begin
    ans:=ans+a div b;
    a:=a mod b;
    z:=a;
    a:=b;
    b:=z;
  end;
  ans:=ans+a div b;
  write(ans);
end.

var
        p,a,b:int64;
begin
        readln(a,b);
        while (a<>0) and (b<>0) do begin
                if a>b then begin
                        inc(p,a div b);
                        a:=a mod b;
                end else begin
                        inc(p,b div a);
                        b:=b mod a;
                end;
        end;
        writeln(p);
end.

var
    n,a,b,c:int64;
    begin {assign(input, 'input.txt'); reset(input);
          assign(output, 'output.txt'); rewrite(output);}
          readln(a,b);
          n:=0;
          repeat
                 n:=n+(a div b);
                 c:=a mod b;
                 a:=b;
                 b:=c;

          until (a=b) or (c=0);
          writeln(n);
          {close(output);}
end.

program z;
var a,b,rslt:int64;
begin
read(a,b);

        while (a <> 0) and (b <> 0) do
        begin
                if a>b then
                begin
                inc(rslt,a div b);
                a:=a mod b;
                end
                else
                begin
                inc(rslt,b div a);
                b:=b mod a;
                end;
        end;

write(rslt);
end.

var a,b,ans,t:int64;
begin
read(a,b);
while (a<>0) and (b<>0) do
   begin
   inc(ans,a div b);
   t:=b;
   b:=a mod b;
   a:=t;
   end;
write(ans);
end.

var
  a, b, an, x: uint64;

begin
  read(a, b);
  repeat
    if a > b then
    begin
      x := a;
      a := a mod b;
      an := an + x div b;
    end
    else
    if b > a then
    begin
      x := b;
      b := b mod a;
      an := an + x div a;
    end;
    if a = 0 then
      a := 1
    else
    if b = 0 then
      b := 1;
  until a = b;
  write(an);
end.

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  a, b, count : Int64;

begin
  ReadLn(a, b);
  while a <> b do
  begin
    if a > b then
    begin
      if a mod b = 0 then
      begin
        count:=count + a;
        break;
      end else
      begin
        count:=count + a div b ;
        a:=a mod b;
      end;
    end else
    begin
       if b mod a = 0 then
       begin
         count:=count + b;
         break;
       end else
       begin
       count:=count + b div a;
        b:=b mod a;
       end;
     end;
  end;
  WriteLn(count);
end.

var
a,b,k:int64;
begin
readln(a,b);
k:=0;
if (a>b) and (a mod b=0) then k:=a div b
else if (b>a) and (b mod a=0) then k:=b div a
else
while (a>0) and (b>0) do
if a>b then begin k:= a div b; a:=a mod b; end
else begin k:=b div a; b:=b mod a; end;
writeln(k);
end.

procedure kotak(a,b : int64);
var
        total,temp : int64;
begin
temp:=0;
repeat
        total := total + a div b;
        temp := a mod b;
        a := b;
        b := temp
    until b = 0;
    writeln(total);

end;
        var
        a,b : int64;
begin
        readln(a,b);
        kotak(a,b);
end.

uses math;
var a,b,ans,i:int64;
begin
read(a,b);
while (a>1) and (b>1) do
begin
if a>b then begin
            inc(ans,a div b);
            a:=a mod b;
            end else begin
                     inc(ans,b div a);
                     b:=b mod a;
                     end;
end;
inc(ans,max(a,b));
write(ans);
end.

var ans,a,b:int64;
begin
           ans:=1;
           read(a,b);
           while a<>b do
           begin
                if a=1 then begin ans:=ans+b-1; break; end else if b=1 then begin ans:=ans+b-1; break; end else
                if b>a then b:=b-a else a:=a-b;
                inc(ans);
           end; writeln(ans);
end.

uses math;

var a, b, cnt: int64;

procedure swap( var a, b: int64 );
var tmp: int64;
begin
    tmp := a;
    a := b;
    b := tmp;
end;

begin
    readln( a, b );
    if ( a < b ) then
        swap( a, b );
    cnt := 1;
    while ( (a > 1) and (b > 1) ) do
    begin
        inc( cnt );
        a := a - b;
        if ( a < b ) then
            swap( a, b );
    end;
    writeln( cnt + a - b );
end.

var     f: text;
        a,b,s,d: int64;
begin
        assign(f,''); reset(f);
        readln(f,a,b);
        d:=0; s:=abs(a-b);
        while s<>0 do
                begin
                        inc(d);
                        if a>b then a:=s
                        else b:=s;
                        s:=abs(a-b);
                        if a=1 then
                                begin
                                        d:=d+b;
                                        break;
                                end
                        else if b=1 then
                                begin
                                        d:=d+a;
                                        break;
                                end;
                end;
        write(d);
        readln;
end.

Program Andronic;
var  a,b:qword; n,i:qword;
begin
     read(a,b);
     n:=0;
     IF (a mod b)=0 then n:=(a div b);
     while (a mod b)<>0 do begin
                           n:=n+(a div b);
                           i:=b;
                           b:=a mod b;
                           a:=i;
                           end;
     IF (a mod b)=0 then n:=n+(a div b);
     writeln(n);
     readln;
end.

uses math;
var i,k,n,m,l,a,b,c,d,p,a1,a2:int64;
 begin
  readln(a,b);

   n:=min(a,b);
   m:=max(a,b);
   if m mod n = 0 then WriteLn(m div n);
   while (n<>1) and (m<>1) do
    begin
     inc(k,m div n);
     m:=m mod n;
     a1:=min(n,m);
     a2:=max(n,m);
     n:=a1;
     m:=a2;
    end;
   WriteLn(k+max(n,m));
 end.

var
 a,b:qword;
function ans(a,b:qword):qword;
begin
 if a*b=0 then exit(0);
 if a>b then
  ans:=ans(a mod b,b)+(a div b)
 else
  ans:=ans(a,b mod a)+(b div a);
end;
begin
 readln(a,b);
 writeln(ans(a,b));
end.

{$MODE OBJFPC}
uses math;
var a,b,dem:int64;

procedure kt(a,b:int64);
begin
     if (a=0) or (b=0) then exit;
     dem:=max(a,b) div min(a,b);
     if a>b then a:=a mod b else
     b:=b mod a;
     if a=b then exit else kt(min(a,b),max(a,b)-min(a,b))

end;

begin
     readln(a,b); dem:=max(a,b) div min(a,b);
     if a>b then a:=a mod b else
     b:=b mod a;
     kt(a,b);
     writeln(dem)
end.

{$MODE OBJFPC}
uses math;
var a,b,dem:int64;

procedure kt(a,b:int64);
begin
     if (a=0) or (b=0) then exit;
     dem:=max(a,b) div min(a,b)+dem;
     if a>b then a:=a mod b else
     b:=b mod a;
     if a=b then exit else kt(min(a,b),max(a,b)-min(a,b))

end;

begin
     readln(a,b); dem:=max(a,b) div min(a,b);
     if a>b then a:=a mod b else
     b:=b mod a;
     kt(a,b);
     writeln(dem)
end.

Var a, b, k, c:int64;
begin
  readln(a, b);
  if b=1 then k:=0 else k:=1;
  while ((a mod b)<>0) do
  begin
    a:=a mod b;
    if (a<b) then begin c:=a; a:=b; b:=c end;
    Inc(k);
  end;
  write(k+(a div b));
end.

Var a, b, k, c:int64;
begin
  readln(a, b);
  if b=1 then k:=0 else k:=1;
  while (b<>1) do
  begin
    a:=a mod b;
    if (a<b) then begin c:=a; a:=b; b:=c end;
    Inc(k);
  end;
  write(k+(a div b));
end.

Var a, b, k, c:int64;
begin
  readln(a, b);
  if ((a mod b)<>0) then k:=1 else k:=0;
  while ((a mod b)<>0) do
  begin
    a:=a mod b;
    if (a<b) then begin c:=a; a:=b; b:=c end;
    Inc(k);
  end;
  write(k+(a div b));
end.

type mas = array of integer;
var a:array of Mas;
    i, j, n, l,q,m, k, sum:integer;
begin
  writeln('������� ����������� �������: ');
  readln(n, m);
  SetLength(a, n);
  For i:=0 to n-1 do
    Setlength(a[i], m);
  for i:=0 to n-1 do
    For j:=0 to m-1 do
      Read(a[i, j]);

  writeln('������� ������� �������: ');
  Readln(k);
  for q:=1 to k do
  for i:=0 to n-1 do
  for l:=0 to m do
  begin
    sum:=0;
    For j:=0 to m-1 do
      sum:=sum+a[i, j]*a[l, j];

    a[i, j]:=sum;
  end;
  for i:=0 to n-1 do
  begin
    For j:=0 to m-1 do
      write(a[i, j], ' ');
    writeln;
  end;
end.

Var
  a, b, c, i:Int64;
begin
  Readln(a, b); i:=0;
  Repeat
    if (a mod b=0) then begin a:=a div b; i:=i+a;  b:=a end
    else
    begin
      if (a>b) then a:=a-b else b:=b-a;
      Inc(i);
    end;
  Until (a=b);
  writeln(i);
end.

Var a, b, k, c:int64;
begin
  readln(a, b);
  if ((a mod b)<>0) then k:=1 else k:=0;
  while ((a mod b)<>0) do
  begin
    a:=a mod b;
    if (a<b) then begin c:=a; a:=b; b:=c end;
    Inc(k);
  end;
  c:=0;
  if ((a div b)>b) then c:=a div b else c:=b;
  write(k+c);
end.

Var a, b, k, c:int64;
begin
  readln(a, b);
  if ((a mod b)<>0) then k:=1 else k:=0;
  while ((a mod b)<>0) do
  begin
    if (a-b=b) then break;
    a:=a mod b;
    if (a<b) then begin c:=a; a:=b; b:=c end;
    Inc(k);
  end;  c:=a div b;
  write(k+c);
end.

program paper;

var a,b,c,ans:int64;

begin
    readln(a,b);
    ans:=0;
    while a*b>0 do begin
        if b>a then begin
            c:=a;
            a:=b;
            b:=c;
        end;
        c:=a div b;
        ans:=ans+c;
        a:=a mod b;
    end;
    writeln(ans);
end.

Var a,b,x,k:longint;
Begin
        readln(a,b);
        k:=0;
        while (a<>0)and (b<>0) do
        begin
        if a>b then begin
                        k:=k+a div b;
                        a:=a-b*(a div b);
                        end
                        else begin
                        k:=k+b div a;
                        b:=b-a*(b mod a);
                        end;
        end;

        writeln(k);
End.

Var a,b,x,k:longint;
Begin
        readln(a,b);
        k:=0;
        while (a<>0)and (b<>0) do
        begin

        if a>b then begin
                        k:=k+a div b;
                        a:=a-b*(a div b);
                        end
                        else begin
                        k:=k+b div a;
                        b:=b-a*(b div a);
                        end;
        inc(k);
        end;

        writeln(k-1);
End.

program z1;
 var a,i,e,n:int64;
begin
 readln(a,i);
 while(a*i<>0)do
  begin
   n:=n+(a div i);
   a:=a mod i;
   e:=i;
   i:=a;
   a:=e;
  end;
 writeln(n);
end.

var
  a, b, k, n: int64;
begin
  read(a, b);
  while ((a * b) <> 0) do
  begin
    if a > b then
    begin
      k := a div b;
      a := a - b * k;
    end
    else
    if b > a then
    begin
      k := b div a;
      b := b - a * k;
    end;
    n := n + k;
  end;
  writeln(n);
end.

var
  a, b, k, n: int64;

begin
  read(a, b);
  while (a * b > 0) do
  begin
    if a > b then
    begin
      k := a div b;
      a := a - b * k;
    end
    else
    if b > a then
    begin
      k := b div a;
      b := b - a * k;
    end;
    n := n + k;
  end;
  writeln(n);
end.

var a,b,ans:int64;
begin
    readln(a,b);    ans:=0;
    repeat
        if a mod b=0 then begin     write(a div b); halt;   end;
        if b mod a=0 then begin     write(b div a); halt;   end;
        while a>b do begin  dec(a,b);   inc(ans);   end;
        while b>a do begin  dec(b,a);   inc(ans);   end;
    until a=b;
    writeln(ans+1);
end.

var  ans,a,b:{real}extended{int64};
begin
readln(a,b);
while (a<>0) and (b<>0) do
 begin
  ans:=ans+1;
  if a >=b then a:=a-b
           else b:=b-a;
 end;
 writeln(ans);
end.

{$MODE OBJFPC}
uses math;
const max1=100000;
type  vector=array[0..max1] of integer;
var n,m,k:integer;
    a,b,res:int64;
    fi,fo:textfile;
procedure enter;
  var i,j:integer;
      temp:int64;
    begin
      res:=0;
      readln(fi,a,b);
      a:=max(a,b);
      b:=min(a,b);
      if a=b*2 then
        begin
          writeln(fo,2);
          exit;
        end;
      res:=a div b;
      a:=b;
      b:=a mod b;
      if b=0 then
        begin
          writeln(Fo,res);
          exit;
        end;
      while a<>b*2 do
        begin
          inc(res);
          temp:=a;
          a:=max(b,temp-b);
          b:=min(b,temp-b);
        end;
      writeln(Fo,res+2);
    end;
procedure solve;
  var i,j:integer;
    begin
    end;
procedure print;
  var i,j:integer;
    begin
    end;
begin
  assignfile(fi,'');reset(fi);
  assignfile(fo,'');rewrite(Fo);
  enter;
  solve;
  print;
  closefile(fi);
  closefile(Fo);
end.

{$mode objfpc}
uses math;
var
  a, b, c: int64;
begin
  readln(a,b);
  while b > 1 do
    begin
      b := min(a - b,b);
      a := max(a,b) - min(a,b);
      inc(c);
    end;
  writeln(c + a);
end.

var
 a,b,n,m : int64;

begin
readln(a,b);
n := a div b;
m := a mod b;
while m > 0 do
 begin
  a := b;
  b := m;
  n := n + a div b;
  m := a mod b;
 end;
end.

uses math;
var a,b,c,d,n:int64;
 begin
 readln(a,b);
 if b<>1 then begin
 while (a>0) and(b>0) do begin
 if a>b then c:=c+a mod b else c:=c+b mod a;inc(n);
 if a>b  then a:=a mod b else b:=b mod a;inc(d);
 c:=n+d;
  end;
 write((c/10)); end;
 if b=1 then write(a);
 end.

var
q,n,m:int64;
i,j,w,e,r:longint;
a:array[1..100000] of longint;
begin
    {assign(input,'right.in');
    assign(output,'right.out');
    reset(input);
    rewrite(output);}
    read(n,m);
    if (n=1) or (m=1) then
    begin
        write(n+m-1);
        exit;
    end;
    while (n>1) and (m>1) do
    begin
        if n>m then
        begin
            inc(q,(n div m));
            n:=n mod m;

        end
        else
        begin
            inc(q,(m div n));
            m:=m mod n;
        end;
    end;
    write(q+n+m-1);
end.

var a,b,c,d:real;
begin
readln(a,b);  d:=b;
while (true) and (d<>1) and (a>=1) and (b>=1) and (b>a) do begin
if(a>=b) then begin a:=a-b; c:=c+1; end
else if (b>=a) then begin b:=b-a; c:=c+1; end
else break;
end;
if(d=1) then writeln(a:0:0) else writeln(c:0:0);
readln;
end.

var a,b,c,d:real;
begin
readln(a,b);  d:=b;
while (true) and (d<>1) and (a<>1) and (b<>1) do begin
if(a>=b) then begin a:=a-b; c:=c+1; end
else if (b>=a) then begin b:=b-a; c:=c+1; end
else break;
end;
if(d=1) then writeln(a:0:0) else writeln(c:0:0);
readln;
end.

Program Origami2;
Var a,b,k,nod,a1,b1:int64;
Begin
readln(a,b);
a1:=a; b1:=b;
k:=0;
while (a<>0) and (b<>0) do
 if a>b then a:=a mod b else b:=b mod a;
 nod:=a+b;
 a:=a1; b:=b1;
 if (nod>1) or ((nod=1) and ((a mod b=0) or (b mod a=0))) then
  if a> b then k:=a div nod else k:=b div nod
else
  begin
   while a<>b do
    begin
     if a>b then a:=a-b else b:=b-a;
     k:=k+1;
    end;
    k:=k+1;
  end;
writeln(k);
readln;
End.

Program Origami2;
Var a,b,k,nod,a1,b1:int64;
Begin
readln(a,b);
a1:=a; b1:=b;
k:=0;
while (a mod 10 =0) and (b mod 10 =0) do
 begin
  a:=a div 10;
  b:=b div 10;
 end;
while (a<>0) and (b<>0) do
 if a>b then a:=a mod b else b:=b mod a;
 nod:=a+b;
 a:=a1; b:=b1;
 if (nod>1) or ((nod=1) and ((a mod b=0) or (b mod a=0))) then
  if a> b then k:=a div nod else k:=b div nod
else
  begin
   while a<>b do
    begin
     if a>b then a:=a-b else b:=b-a;
     k:=k+1;
    end;
    k:=k+1;
  end;
writeln(k);
readln;
End.

var a,b,c:int64;
begin
{assign(input,'input.txt');
reset(input);
assign(output,'output.txt');
rewrite(output);}

read(a,b);
c:=0;

while a<>b do
begin
if (a mod b <> 0) and (b mod a <> 0) then
begin
     if a>b then a:=a-(b*(a div b)) else b:=b-(a*(b div a));
     c:=c+(a div b)+(b div a);
end else
begin
     if a>b then a:=a-b else b:=b-a;
     inc(c);
end;
end;

write(c);
{close(input);
close(output);}
end.

var a,b,k,c:int64;
begin
{assign(input,'input.txt');
reset(input);
assign(output,'output.txt');
rewrite(output);}

read(a,b);
c:=0;

while a<>b do
begin
if (a mod b <> 0) and (b mod a <> 0) then
begin
     if a>b then a:=a-(b*(a div b)) else b:=b-(a*(b div a));
     c:=c+(a div b)+(b div a);
     inc(k);
end else
begin
     if a>b then a:=a-b else b:=b-a;
     inc(c);
end;
end;
if k=0 then inc(c);
write(c);
{close(input);
close(output);}
end.

var a,b,k,c:integer;
begin
{assign(input,'input.txt');
reset(input);
assign(output,'output.txt');
rewrite(output);}

read(a,b);
c:=0;

while (a<>b) and (a>0) and (b>0) do
begin
if (a>b) then begin

   k:=a div b;
   a:=a mod b;

end else begin
    k:=b div a;
    b:=b mod a;
end;

end;
write(k);
{close(input);
close(output);}
end.

var a,b,c:int64;
begin
{assign(input,'input.txt');
reset(input);
assign(output,'output.txt');
rewrite(output);}

read(a,b);
c:=0;

while a<>b do
begin
if (a mod b <> 0) and (b mod a <> 0) then
begin
     if a>b then a:=a-(b*(a div b)) else b:=b-(a*(b div a));
     c:=c+(a div b)+(b div a);
end else
begin
     if a>b then a:=a-b else b:=b-a;
     inc(c);
end;
end;
inc(c);
write(c);
{close(input);
close(output);}
end.

var a,b,c:int64;
begin
{assign(input,'input.txt');
reset(input);
assign(output,'output.txt');
rewrite(output);}

read(a,b);
c:=0;

while a<>b do
begin
if (a mod b <> 0) and (b mod a <> 0) then if a>b then a:=a-(b*(a div b)) else b:=b-(a*(b div a)) else if a>b then a:=a-b else b:=b-a;
c:=c+(a div b)+(b div a);
end;

write(c);
{close(input);
close(output);}
end.

var a,b,c:integer;
begin
{assign(input,'input.txt');
reset(input);
assign(output,'output.txt');
rewrite(output);}

read(a,b);
c:=0;

while a<>b do
begin
if a>b then a:=a-b else b:=b-a;
inc(c);
end;

write(c);
{close(input);
close(output);}
end.

var
a, b, x: int64;
begin
read(a);
readln(b);
x:= 0;
while a <> b do begin
                if a < b then b:= b - a
                         else a:= a - b;
                x:= x + 1;
                end;
writeln(x);
end.

Var a : Array[97..122,97..122]of longint;
    b : array[97..122]of longint;
    s,k : array[1..200005]of char;
    i,j,t,c,f,n : longint;
begin

   readln(n);
   For i := 1 to n do
     read(s[i]);
   readln;
   for i := 1 to n do
     read(k[i]);
   For i := 1 to n do
    begin
     if (s[i] <> k[i]) then
      begin
         a[ord(s[i]),ord(k[i])] := i;
         b[ord(s[i])] := 1;
         inc(f);
      end;
    end;
   For i := 97 to 122 do
     for j := 97 to 122 do
       if (i <> j) then
         if (a[i,j] > 0) and (a[j,i] > 0) then
            begin
              writeln(f - 2);
              writeln(a[i,j],' ',a[j,i]);
            exit;
            end;
   For i := 1 to n do
    if (s[i] <> k[i]) then
     if (b[ord(k[i])] > 0) then
       For j := 1 to length(s) do
          if (ord(s[j]) = ord(k[i])) then
            begin
              writeln(f - 1);
              writeln(i,' ',j);
            exit;
            end;
   writeln(f);
   writeln('-1 -1');
end.

{$mode objfpc}
var
  a : array[1..100000] of integer;
  n,i,j,k,x,kq,t : integer;
begin
  readln(n,k);
  for i := 1 to n do
    begin
     readln(j);
     a[j] := i;
    end;
  for i := 1 to k do
     begin
       t := i mod n;
       if t = 0 then t := n;
       x := a[t];
       for j := 1 to n do
          begin
             if  a[j] >= x then  a[j] := a[j] - x + 1
             else a[j] := a[j] + n - x + 1;
          end;
        kq := kq + x -1;
     end;
  writeln(kq);
end.

var a,b,s:int64;
begin
  read(a,b);
  while a*b<>0 do
    if a>b then begin s:=s+a div b; a:=a mod b end else begin s:=s+b div a; b:=b mod a end;
  write(s)
end.

var a,b: int64;
    kq: int64;
{------------------------------}
procedure process;
var tam: longint;
begin
    readln(a,b);
    repeat
        kq := kq + a div b;
        tam := a mod b;
        a := b;
        b := tam;
    until b = 0;
    writeln(kq);
end;
{------------------------------}
begin
    process;
end.


var n,k,t,ans:longint ;
begin
   readln(n,k,t) ;
	 if (t<k) then ans:=ans+t ;
	 if t>=k then ans:=ans+k ;
	 if (t>n) then ans:=ans-t+n ;
	 writeln(ans) ;
end.

var
  t,k,i,j,n,m:int64;
begin
  read(n,k,t);
  if t<=k then writeln(t) else
  if t<=n then writeln(k) else
  writeln(k-(t-n));
end.

Var
    n, k, t: UInt64;

Begin
    Read(n, k, t);
    If t <= k then
        Write(t)
    Else If (t > k) and (t <= n) then
        Write(k)
    Else
        Write(n + k - t)
End.

program pr2;
 var
  z,n,k,t:longint;
 begin
  readln(n,k,t);
	  if t<=k then
	    z:=t;
	  if (t>k)and(t<n) then
	    z:=k;
	  if t>=n then
	    z:=n+k-t;
  write(z);
 end.

   const nn=1600000; nnn=1200000;eps=0.00000000001;
   besk=80000000000000;
   label 1,2,3;
    var    step, i,j:longint;
    k,pp,jj,ii,p3,p4, p1,p2,t1,t2,p,ost,q,n,ot,t,kk:int64;
     pal:array[-20..6000,-20..6000] of boolean;
     ppp:extended;
      dd,zz,z,c,d,x,y,a,b:array[-1002..2000000] of int64;
      ddnew,ddold:array[0..200,0..4040] of integer;
     per,dda,ddb:array[-10..120,-10..120] of int64;
        ss:string;
            ch:char;
           flagg,flag,flag1:boolean;

  function min(x,y:int64):int64;begin min:=x;if y<x then min:=y; end;
  function max(x,y:int64):int64;begin max:=x;if y>x then max:=y; end;
   function nod(x,y:int64):int64; var z:int64;
begin   repeat  z:=x mod y;x:=y;y:=z;   until  z=0 ;  nod:=x;end;

function ff(p:int64):int64;
var t1,t2:int64;
begin  if p=1 then ff:=0 else if p=2 then ff:=1 else
  begin
    t1:=ff(p shr 1);
    t2:=ff(p-(p shr 1));
    ff:=t1*t2+t1+t2;

  end;

end;

begin
   {    assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
    }

    read(n,k,t);
    if t<=k then begin write(t); goto 1; end;
    if t>=n then begin write(n+k-t); goto 1; end;
    write(k);

   1:          close(output);
end.

program mfs;
var
n,k,t:longint;
begin
readln(n,k,t);
if t<=k then begin writeln(t); halt; end;
if t>=n then begin writeln(k-(t-n)); halt; end;
if (k<t)and(t<n) then writeln(k);
end.

var
n,k,t,ans:integer;
begin
readln(n,k,t);
if t<k then
ans:=t else
if t>n then
ans:=n+k-t
else
ans:=k;
writeln(ans);
end.

var n , k , t : longint;
begin
read(n , k , t);
if (t < k) then write(t);
if ((t >= k) and (t <= n)) then write(k);
if (t > n) then write(n + k - t);
end.

var x,y,i,j,k,n,z:longint;
begin
 read(n,k,j);
 if (j<k)and(j<n) then write(j) else if j>=n+k then write(0) else if n<j
 then write(k-j+n)
 else write(k);
end.

uses math;
const fi=''; fo='';
var fin,fout:Text;
    n,k,t:int64;
BEGIN
      assign(fin,fi);
      assign(fout,fo);
      reset(Fin);rewrite(Fout);
      readln(Fin,n,k,t);
      if t<=k then writeln(fout,t);
      if (t>k) and (t<=n) then writeln(Fout,k);
      if (t>n) then
        writeln(fout,k-(t-n));
      close(Fin);close(Fout);
END.

var n,k,t:longint;
begin
readln(n,k,t);
if k>=t then writeln(t) else
if (k<t) and (t<=n) then writeln(k) else
if (k<t) and (t>n) then writeln(k-(t-n));
end.

var a,b,c:int64;
begin
read(a,b,c);
if c>a then write(b-c+a) else
if c>=b then write(b) else write(c)
end.

const
        fv='';
        fr='';
var
        n,k,t:longint;
        f,g:text;
begin
        assign(f,fv);reset(F);
        assign(g,fr);rewrite(g);
        read(f,n,k,t);
        if t<k then writeln(g,t);
        if (t>=k)and (t<=n) then writeln(g,k);
        if t > n then writeln(g,k-(t-n));
        close(f);
        close(G);
end.

var n,k,t:longint;
begin
readln(n,k,t);
if t>n then
        if k-(t-n)<>0 then writeln(k-(t-n))
                else writeln(0)
        else
        if t-k>0 then writeln(k)
                else writeln(t);
end.

const fin='';
        fout='';
var f,g:text;
        n,t,k:longint;
begin
        assign(f,fin);reset(F);
        assign(g,fout);rewrite(G);
        read(f,n,k,t);
        if t<=k then write(g,t)
        else if (t>k) and (t<=n) then write(g,k)
        else write(g,k-(t-n));
        close(F);
        close(G);
end.

var
n,k,t:longint;
begin
read(n,k,t);
if t<=k then begin write(t); halt; end;
if t<=n then begin write(k); halt; end;
write(n+k-t); end.

var
 n,k,t:int64;
begin
 readln(n,k,t);
 if (t<=k) then writeln(t) else
 if (t<=n) then writeln(k) else
                writeln(n+k-t);
end.

var n,k,t:longint;
Begin
    readln(n,k,t);
    if(t<=k) then writeln(t)
    else
    if(t>k)and(t<=n) then writeln(k)
    else writeln((n+k)-t);
end.

var n,t,k:Longint;
begin
	readln(n,k,t);
	if t<=k then writeln(t)
	else
	if t<=n then writeln(t-(t-k))
	else
	writeln(n+k-t);

end.

var n,k,t,x:longint;

begin
readln(n,k,t);
IF t<=n then
   IF t<k then write(t)
   else write(k)
else write(k-(t-n));
end.

var
n,k,t,m:qword;
begin

readln(n,k,t);
if (t<=k) then m:=t
else if (t<=n) then begin
    m:=k;
end
else begin
    m:=k-(t-n);
end;
writeln(m);
end.

var
  k, n, t: integer;

begin
  readln(n, k, t);
  if (t <= n) and (t >= k) then
  begin
    writeln(k)
  end
  else
  begin
  if t < k then
  begin
    writeln(t)
  end
  else
    writeln(n+k-t);
  end;
end.

var
n,k,t:int64;
begin
readln(n,k,t);
if t<=k then writeln(t)
else begin
    if t<=n then writeln(k)
    else begin
        writeln(n+k-t);
    end;
end;
end.

var n,k,t,x:longint;

procedure xuli;
   begin
   IF t<=n then
      IF t<k then write(t)
      else write(k)
   else write(k-(t-n));
   end;

begin
readln(n,k,t);
xuli;
end.

var
  n,k,t:longint;
begin
  readln(n,k,t);
  if (t>=0)and(t<=k-1)  then
    writeln(t) else
    if (t>=n+1)and(t<=n+k) then
    writeln(k-(t-n)) else writeln(k);
end.

var n,k,t:longint;
begin
readln(n,k,t);
if t<=k then write(t)
        else
                if (t>k)and(t<=n) then write(k)
                        else write(k-(t-n));
end.

const
        fin='432_a.inp';fon='432_a.out';
var n,k,t,i,j:longint;
begin
//assign(input,fin);
//reset(Input);
//assign(Output,fon);
//rewrite(output);
readln(n,k,t);
if t<=k then writeln(t)
        else
                if (t>k)and(t<=n) then write(k)
                        else writeln(k-(t-n));
//close(input);close(output);
end.

var
	a, b, n, t, c1, k, x, x1, x2, x3: uint64;
	s: string;

begin

	readln(n, k, t);
	if (t <= k) then
		writeln(t)
	else if ((t > k) and (t <= n)) then
		writeln(k)
	else if ((t >= n) and (t < n + k)) then
		writeln(n + k - t);

end.

{$coperators on}
{$mode objfpc}
{$inline on}
const
        fi='input.inp';
        fo='output.out';
procedure fileio;
begin
        assign(input,fi); reset(input);
        assign(output,fo); rewrite(output);
end;
procedure enter;
var     n,k,t:int64;
begin
        readln(n,k,t);
        if t<=k then writeln(t)
        else
         begin
                if t<=n then
                 writeln(k)
                else
                 writeln(k-(t-n));
         end;
end;
begin
        //fileio;
        enter;
end.

var i,j,k,n,s,t:integer;
begin
readln(n,k,t);
if (t<=n)and(t>=k) then write(k) else
if t<k then write(t) else write(n+k-t);
end.

var n,k,t:longint;
begin
  read(n,k,t);
  if t<=k then write(t);
  if (t>k)and(t<=n) then write(k);
  if t>n then write(k-t+n);
end.

const fi='';
      fo='';
var f:text;
    n,k,t:longint;

 procedure init;
  begin
   assign(f,fi);
   reset(f);
   read(f,n,k,t);
   close(f);
  end;

 procedure xuly;
  begin
   assign(f,fo);
   rewrite(f);
   if t<=k then writeln(f,t)
   else if t<=n then writeln(f,k)
   else writeln(f,n+k-t);
   close(f);
  end;

 begin
  init;
  xuly;
 end.

var n,k,t,o:integer;
begin
 readln(n,k,t);
 if t<k then o:=t
        else if (t>n)and(n-t+k>0) then o:=k+n-t
                                  else o:=k;
 writeln(o);
end.

Var
  n, k, t: integer;

  function mint(a, b: integer): integer;
   begin
     if (a <= b) then
      result:= a
     else
      result:= b;
    end;

Begin
  readln(n, k, t);
  if (t <= n) then
   write(mint(t, k))
  else
   write(k + n - t);
end.

uses math;
var n,k,t:int64;
begin
        read(n,k,t);
        write(min(t,min(k,n+k-t)));
end.

var n,k,t:int64;
begin
    readln(n,k,t);
    if (t<k) then writeln(t) else
    if (t>n) then writeln((n+k-t)) else
    writeln(k);
end.

var n,k,t:longint;
begin
read(n,k,t);
if(t<k) then write(t)
else if(t<n) then write(k)
else write(n+k-t);
end.

Program MexicanWave;
uses math;
var n,k,t:int64;
begin   //assign(input,'851A.inp'); reset(input);
        //assign(output,'851A.out'); rewrite(output);
        readln(n,k,t);
        if t<=k then writeln(t)
        else if (t>k) and (t<=n) then writeln(k)
        else writeln(k-(t-n));
        //close(input); close(output);
end.

var n,t,k: int64;
BEGIN
  readln(n,k,t);
  if (t <= k) then
    begin
      write(t);
      exit;
    end;
  if (t <= n) then
    begin
      write(k);
      exit;
    end;
  write(n+k - t);
END.

var n,k,t:longint;
begin
    read(n,k,t);
    if t<=k then writeln(t)
   else if t<=n then writeln(k)
   else writeln(n+k-t);
end.

var n,k,t:int64;
begin
readln(n,k,t);
if t<=k then
    begin
        writeln(T);
        halt;
    end;
if (t>=n) then
    begin
        writeln(k-t+n);
        halt;
    end;
writeln(k);
end.

uses math;
var
  n,k,t,ans:int64;
begin
  readln(n,k,t);
  ans:=min(t,k);
  ans:=min(ans,k-t+n);
  writeln(ans);
end.

var
  n,k,t:longint;
begin
  readln(n,k,t);
  if t<=k then writeln(t)
    else if (t>k) and (n>=t) then writeln(k)
           else writeln(n+k-t);
end.

var
n,k,t:longint;
begin
readln(n,k,t);

if t<=k then
begin
writeln(t);
halt;
end;

if n-t>=0 then
begin
writeln(k);
halt;
end;

if n-t<0 then
writeln(k-t+n);
end.

var
	n,k,t:longint;
begin
	readln(n,k,t);
	if t<=k then writeln(t)
	else if ((t>=k) and (t<=n)) then writeln(k)
	else writeln(k+n-t);
end.

var n,k,t:longint;
function process:longint;
begin
    if t<=k then exit(t)
    else if (t>k) and (t<=n) then exit(k)
    else if (t>n) then exit(k-(t mod n));
end;
begin
readln(n,k,t);
write(process);
end.

var
a,b,c,x,y,z:longint;
i,j,k,l,m,n,t:longint;
begin
readln(n,k,t);
if t<=k then writeln(t);
if (t>k)and(t<=n)then writeln(k);
if t>n then writeln(n+k-t);
end.

Type            ll=Longint;
Var             n,k,t:ll;
                f:text;
Procedure       Print(i:ll);
Begin
                Write(i);
                Readln;
                Halt;
End;
Begin
                Assign(f,'');Reset(f);
                Readln(f,n,k,t);
                Close(f);
                If t<k  Then Print(t);
                If t>n Then Print(k-t mod n);
                If t mod (n+k)=0 Then Print(0);
                Print(k);
End.

var n,k,t:longint;
begin
  readln(n,k,t);
  if t<=k then writeln(t) else
  if n>=t then writeln(k) else
  writeln(n+k-t);
end.

Var k , n , t : Longint;
Begin
readln(n , k , t);
If t <= k then Writeln(t) else
If t <= n then Writeln(k) else
Writeln(n + k - t);
End.

program asdf;
var n,k,t:longint;
begin
readln(n,k,t);
if t<=k then writeln(t)
else if (t>k) and (t<=n) then writeln(K)
else if (n-(t-k))>-1 then writeln(n-(t-k)) else writeln('0');
end.

var
  n, k, t, r: int64;

begin
  read(n, k, t);
  if t <= k then r := t
  else if t >= n then r := n + k - t
  else r := k;
  if r < 0 then r := 0;
  write(r);
end.

Program MexicanWave;
uses math;
var n,k,t:int64;
begin   //assign(input,'851A.inp'); reset(input);
        //assign(output,'851A.out'); rewrite(output);
        readln(n,k,t);
        if t<=k then writeln(t)
        else if (t>k) and (t<=n) then writeln(k)
        else writeln(k-t mod k);
        //close(input); close(output);
end.

program mfs;
var
n,k,t:longint;
begin
readln(n,k,t);
if t<=k then writeln(t);
if t>=n then writeln(k-(t-n));
if n=k then begin writeln(k-1); halt; end;
if (k<t)and(t<n) then writeln(k);
end.

program mfs;
var
n,k,t:longint;
begin
readln(n,k,t);
if t<=k then writeln(t);
if t>=n then writeln(k-(t-n));
if (k<t)and(t<n) then writeln(k);
end.

var
  n,k,t,i,xb:longint;
  a:array[-100000..100000] of longint;
begin
  readln(n,k,t);
  if t in[0..k-1] then
    writeln(t) else
    if t in[n+1..n+k] then
    writeln(k-(t-n)) else writeln(k);
end.

var
a,b,c,x,y,z:longint;
i,j,k,l,m,n,t:longint;
begin
readln(n,k,t);
if t<=k then writeln(t);
if (t>k)and(t<=n)then writeln(k);
if t>n then writeln(t-n-k);
end.

var
  n,k,t:longint;
begin
  readln(n,k,t);
  if k>t then writeln(t)
    else if (t>k) and (t<n) then writeln(k)
           else writeln(n+k-t);
end.

var
	a, b, n, t, c1, k, x, x1, x2, x3: integer;
	s: string;

begin

	readln(n, k, t);
	if (t <= k) then
		writeln(t)
	else if ((t > k) and (t < n)) then
		writeln(t - (t - k))
	else if (t >= n) then
		writeln(t - n + 1)

end.

Var
    n, k, t: Integer;

Begin
    Read(n, k, t);
    If t <= k then
        Write(t)
    Else If (t > k) and (t <= n) then
        Write(n - k)
    Else
        Write(n + k - t)
End.

Var
    n, k, t: Int64;

Begin
    Read(n, k, t);
    If t <= k then
        Write(t)
    Else If (t > k) and (t <= n) then
        Write(n - k)
    Else
        Write(n + k - t)
End.

program pr2;
 var
  z,n,k,t:integer;
 begin
  readln(n,k,t);
	  if t<=k then
	    z:=t;
	  if (t>k) then
	    z:=k;
  write(z);
 end.


var n,x:integer;
begin
     readln(n);
     if n = 2 then
        writeln('2')
     else
         writeln('1');
end.

var
 n:longint;
begin
 read(n);
 if n=2 then write(2)
        else write(1);
end.

var
  n:longint;
begin
  read(n);
  if n>2 then write(1) else if n=2 then write(2) else write(1);
end.

Var
    n: UInt64;

Begin
    Read(n);
    If n <> 2 then
        Write(1)
    Else
        Write(2)
End.

var
n :int64;
begin
    read(n);
    if n <= 2 then write(n)
    else write('1');
end.

var
  n:longint;
begin
  readln(n);
  if n=2 then writeln(2) else writeln(1);
end.

var
i,n:longint;
begin
read(n);
if n>2 then n:=1;
write(n);
end.

var n,i:longint;
begin
  readln(n);
  i:=n-1;
  while (n>1) do begin
    if n mod i<>0 then begin
      n:=n-i;
      i:=n;
    end;
    dec(i);
    if i<=1 then break;
  end;
  writeln(n);
end.

 var k:int64;
begin
 read(k);if k<3 then write(k) else
write(1)
end.

var n:int64;
begin
readln(n);

if (n<>2) then writeln('1') else writeln('2');
end.

var n:integer;
begin
        read(n);
	if n<=2 then write(n)
	else write(1);
end.

var v:int64;
begin
    readln(v);
    if v=2 then writeln(2)
    else writeln(1);
end.

program game;

var
n:longint;

begin
	readln (n);
	if (n <= 2) then
	begin
		writeln (n);
	end else
	begin
		writeln (1);
	end;
end.

var
  n: uint64;

begin
  readln(n);
  if n = 2 then
    write(2)
  else
    write(1);
end.

program A1081;

var
    v,n:longint;

begin
    readln(v);
    if v = 2 then writeln(2) else writeln(1);
end.

var
 n:longint;
begin
 readln(n);
 if n=2 then writeln('2')
  else writeln('1');
end.

var n:word;begin read(n);if n=2 then write(n)else write(1);end.

var
n:longint;
begin
read(n);
if n>2 then n:=1;
write(n);
end.

var n:integer;
begin
    readln(n);
    if (n = 2) then
        writeln(2)
    else
        writeln(1);
end.

var a,b,i:longint;
begin
read(a);
for i:=a downto 1 do
if a mod i<>0 then break;
if a mod 2=0 then
if i=1 then write(a)
else write(1)
else write(1);
end.

var a,b,c:longint;
begin
readln(a);
if a<>2 then
writeln(1)
else writeln(2);

end.

{* Author: Tejas Gopalakrishna *}

{$mode objfpc}{$H+}{$J-}

program definitegame;

var n: integer;

begin
	read(n);
	if n = 2 then
		writeln(2)
	else
		writeln(1);
end.

Var a:Int64;
Begin
    Readln(a);
    if a<>2 then
        Writeln('1')
    else
        Writeln('2');
End.

var
 n:longint;
begin
 read(n);
 if n=2 then writeln(2)
        else writeln(1);
end.

var n, x, k, v: int64;
begin
readln (v);
 if v = 0  then  write ('0')
  else
  begin
  if v = 2 then
  write ('2') else write ('1');
 end;

end.

var n:int64;
begin
read(n);
if (n = 1) or (n = 2) then write(n) else write(1)

end.

begin var n:=0;
  read(n);
  if n=2 then writeln(2) else
  writeln(1);
end.

var
  a, i, b, c, d, f, h, p, m, n, k, code: int64;

begin
  readln(n);
  if n=2 then writeln('2')
  else
writeln('1');

end.

var
n:int64;
begin
read(n);
if n<>2 then
write('1')
else
write('2');
end.

program A;

var v: integer;
//    s: string;
//    a,b,k:array of int64;
//    answer: array of int64;
//    fl_exit: boolean;

begin
 readln(v);

 if v>2 then
  writeln(1)
 else
  writeln(v);

end.

var
	a, b, n, c, c1, m, x, x1, x2, x3: integer;
	s: string;

begin

	readln(n);
	if (n >= 3) then
		writeln(1)
	else writeln(n);

end.

                     const eps=0.0000000001;  besk1=200000008;  nnn=4600000;
              modul=1000000007{998244353} ;
   besk=4000000000000000000;
   label 22,1,2,3,4;
    type    real=extended;   double=extended;
    mas=array [-300000..nnn] of int64;   point=record x,y,z:real;end;
    zap=record p1,p2,p3,p4 :int64;end; zap1=^zap;
    mass=array[-10000..nnn] of longint;mass1=^mass;
    var tmin,aaa,bbb,ccc,kolst,kollek,aa,bb,t1,t2,kolx,koly,ans,ans1,ans2,dl,posleft,posright,n,xxx,yyy,dx,dy,m,w,h,zz,jjj,l,r,cc,sum,mnog,nn,t3,t4,t5,t6,posmax,tmax,kold,kk,j2,ttt,ppp,kol,sq,pmin,pmax,limit,posi,posj,diam,oldt,ot1,time,pos,ll,posx,posy,gr,dloch,newdl,oldn,oldm,ost,ot,p1,p2,p3,p4,znamzz,kolperiod,znam,tperiod,start,finish,kol1,kol2,lavel,p0,jj,ii,ogr,q,p,pp,tt,oldtt,qq,qq1,qq2,q1,q2,imax,t,nm,smin,cen:int64;
   k,i2,i3,step2,k1,k2,pp0,mask,i1,j1,step1, step, i,j:longint;
   numx,numy,xold,yold,x1,x0,d,xx,yy,b,a,zsum,y,x,z:array[-200000..535002] of int64;
    prob1,prob2,prob3,pr1new,pr2new,pr3new,pr1,pr2,pr3:zap;
    iii,oldpos,finot,vv,znak:int64;
    aold:mas;  anss:zap;
    ss1,ss2:string;
    lll,rrr,v,dist,alpha,phi:extended;
    zzz,ddnew,koef,polex,ddsum,dd:array[-1..4400,-1..4200] of int64;
    ppos,mma,ma:array[-1000..2000005,-1..15] of int64;
    xleft,xright,xplus,xminus,px,py,xc,yc,xxc,yyc:extended;
    del:array[-1..100001,-1..20] of longint;
    z0,num,y1,z1,z2,kolnul,fact,next,prev,str,s,name,dnew, voltay,c,day:mas;
    zzzz:array[0..125000000] of byte;
    aaaa,bbbb,x2:extended;
    delit:array[0..100000,0..20] of longint;
    pole:array[-10..2002,-10..2005] of char;
    ddd,ppole,ddl,ddr,dd3,dd4:array[-10..200,-10..220] of int64;
    ddnnew:array[-1..12,-1..12,0..200,0..3] of int64;
   newzan,marked,zanx,zany:array[-1000000..2000000] of boolean;
    ssold,sss1,sss2:string;
    def,stepen,koled,och,newoch,place,newnabor,stoim,nabor:array[-10..200005] of int64;
     zanleft,zanright,zan,fl:array[-100000..10000000] of boolean;
     dost:array[-1..210,-1..210,0..3,0..3] of int64;
    ss:string;ch,ch1,ch2:char;
    pr,hesh,numright,dr: array [-10..206000,-1..16] of int64;
    s3,s2,s1:array[-100..500000] of string;

    grr :array [-10..2010,-10..2010] of extended;
    maxpr:array[0..nnn] of string;
    sss:array[-10..nnn] of string;
     tx,ty,vel,ott,v0,v1,s0,t0:int64;
     zv:point;
     left,right,ugol,rot,temp,sumtemp,vol,newx,newy:array[-1000..400000] of int64;
     mash,sqx,sqy,ottt:extended;
     xxxx,yyyy,yot:double;
       flag2,flag,flag1:boolean;

  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;

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

      function nod(x,y:int64):int64; var z:int64;
begin   repeat  z:=x mod y;x:=y;y:=z;   until  z=0 ;  nod:=x;end;

     function summ(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p2+xx.p2*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=t1 div tt mod modul;
    t2:=t2 div tt mod modul;
    ott.p1:=t1; ott.p2:=t2;

   summ:=@ott;
 end;

     function prod(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=(t1 div tt) mod modul;
    t2:=(t2 div tt) mod modul;
    ott.p1:=t1; ott.p2:=t2;

   prod:=@ott;
 end;

    function al(x,y:real):real; var t0,t1,p,s0,s1:real;
 begin
     if (x>0)and(y>=0) then p:=arctan(y/x);
     if x<0 then p:=pi+arctan(y/x);
     if(x>0)and(y<0) then p:=2*pi+arctan(y/x);
     if x=0 then if (y>=0) then p:=pi/2 else p:=3*pi/2;
     al:=p{/pi*180};
 end;

function degg(a,k:int64):int64; var p:int64;
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 shr 1); degg:=p*p mod modul;   end;

end;

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

   prost:=fl;
 end;

function arcsin(pp:extended):extended;
var tt:extended;
begin
   if (abs(pp)<=1+eps)and (abs(pp)>=1-eps)
   then if pp>0 then arcsin:=pi/2 else arcsin:=-pi/2;
   tt:=1-pp*pp;
   if abs(tt)>=0 then
     begin
        arcsin:=arctan(pp/sqrt(tt));

     end;

end;

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

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 ff(gr:int64):boolean;

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

    end;
   writeln(' cc= ',cc);
   cc:=min(cc+3,m+1);
   repeat dec(cc) until y[cc]<=gr;
   if y[cc]=gr then fl:=true else fl:=false;
  ff:=fl;
end;

function uspel(kolst,koldn,vm1,vm2,kollek:int64):boolean;
var fl:boolean; p1,p2,t1,t2,p,ot,pp:int64;
 begin
    t1:=min(kolst,vm1);
    t2:=min(kolst,vm2);
    p2:=koldn div 2;
    p1:=koldn-p2;
    ot:=p1*t1+p2*t2;
    fl:=ot>=kollek*kolst;
   { writeln()
    }
   uspel:=fl;
 end;

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

      readln(n);
      if n=2 then begin write(2); goto 1; end;
      write(1);

1:             close(output);
end.

var n: longint;

begin
  readln (n);
  if n=2 then writeln (2) else writeln (1);
end.

var x,n,v:integer;
begin
read (v);
n:=v;
If n<>2 then n:=1;
write(n);
end.

var x:word;begin read(x);if x=2 then write(2)else write(1);end.

var n : longint;
begin
        readln(n);
        if n = 0 then writeln(0) else
                begin
                        if n = 2 then writeln(2) else writeln(1);
                end;
end.

var
 n:int64;
begin
  readln(n);
  if n=2 then writeln(2)
    else writeln(1);
end.

var
 a:longint;
begin
 readln(a);
 if a=2 then writeln(a)
  else writeln(1);
end.

                     const eps=0.0000000001;  besk1=200000008;  nnn=4600000;
              modul=1000000007{998244353} ;
   besk=4000000000000000000;
   label 22,1,2,3,4;
    type    real=extended;   double=extended;
    mas=array [-300000..nnn] of int64;   point=record x,y,z:real;end;
    zap=record p1,p2,p3,p4 :int64;end; zap1=^zap;
    mass=array[-10000..nnn] of longint;mass1=^mass;
    var tmin,aaa,bbb,ccc,kolst,kollek,aa,bb,t1,t2,kolx,koly,ans,ans1,ans2,dl,posleft,posright,n,xxx,yyy,dx,dy,m,w,h,zz,jjj,l,r,cc,sum,mnog,nn,t3,t4,t5,t6,posmax,tmax,kold,kk,j2,ttt,ppp,kol,sq,pmin,pmax,limit,posi,posj,diam,oldt,ot1,time,pos,ll,posx,posy,gr,dloch,newdl,oldn,oldm,ost,ot,p1,p2,p3,p4,znamzz,kolperiod,znam,tperiod,start,finish,kol1,kol2,lavel,p0,jj,ii,ogr,q,p,pp,tt,oldtt,qq,qq1,qq2,q1,q2,imax,t,nm,smin,cen:int64;
   k,i2,i3,step2,k1,k2,pp0,mask,i1,j1,step1, step, i,j:longint;
   numx,numy,xold,yold,x1,x0,d,xx,yy,b,a,zsum,y,x,z:array[-200000..535002] of int64;
    prob1,prob2,prob3,pr1new,pr2new,pr3new,pr1,pr2,pr3:zap;
    iii,oldpos,finot,vv,znak:int64;
    aold:mas;  anss:zap;
    ss1,ss2:string;
    lll,rrr,v,dist,alpha,phi:extended;
    zzz,ddnew,koef,polex,ddsum,dd:array[-1..4400,-1..4200] of int64;
    ppos,mma,ma:array[-1000..2000005,-1..15] of int64;
    xleft,xright,xplus,xminus,px,py,xc,yc,xxc,yyc:extended;
    del:array[-1..100001,-1..20] of longint;
    z0,num,y1,z1,z2,kolnul,fact,next,prev,str,s,name,dnew, voltay,c,day:mas;
    zzzz:array[0..125000000] of byte;
    aaaa,bbbb,x2:extended;
    delit:array[0..100000,0..20] of longint;
    pole:array[-10..2002,-10..2005] of char;
    ddd,ppole,ddl,ddr,dd3,dd4:array[-10..200,-10..220] of int64;
    ddnnew:array[-1..12,-1..12,0..200,0..3] of int64;
   newzan,marked,zanx,zany:array[-1000000..2000000] of boolean;
    ssold,sss1,sss2:string;
    def,stepen,koled,och,newoch,place,newnabor,stoim,nabor:array[-10..200005] of int64;
     zanleft,zanright,zan,fl:array[-100000..10000000] of boolean;
     dost:array[-1..210,-1..210,0..3,0..3] of int64;
    ss:string;ch,ch1,ch2:char;
    pr,hesh,numright,dr: array [-10..206000,-1..16] of int64;
    s3,s2,s1:array[-100..500000] of string;

    grr :array [-10..2010,-10..2010] of extended;
    maxpr:array[0..nnn] of string;
    sss:array[-10..nnn] of string;
     tx,ty,vel,ott,v0,v1,s0,t0:int64;
     zv:point;
     left,right,ugol,rot,temp,sumtemp,vol,newx,newy:array[-1000..400000] of int64;
     mash,sqx,sqy,ottt:extended;
     xxxx,yyyy,yot:double;
       flag2,flag,flag1:boolean;

  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;

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

      function nod(x,y:int64):int64; var z:int64;
begin   repeat  z:=x mod y;x:=y;y:=z;   until  z=0 ;  nod:=x;end;

     function summ(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p2+xx.p2*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=t1 div tt mod modul;
    t2:=t2 div tt mod modul;
    ott.p1:=t1; ott.p2:=t2;

   summ:=@ott;
 end;

     function prod(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=(t1 div tt) mod modul;
    t2:=(t2 div tt) mod modul;
    ott.p1:=t1; ott.p2:=t2;

   prod:=@ott;
 end;

    function al(x,y:real):real; var t0,t1,p,s0,s1:real;
 begin
     if (x>0)and(y>=0) then p:=arctan(y/x);
     if x<0 then p:=pi+arctan(y/x);
     if(x>0)and(y<0) then p:=2*pi+arctan(y/x);
     if x=0 then if (y>=0) then p:=pi/2 else p:=3*pi/2;
     al:=p{/pi*180};
 end;

function degg(a,k:int64):int64; var p:int64;
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 shr 1); degg:=p*p mod modul;   end;

end;

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

   prost:=fl;
 end;

function arcsin(pp:extended):extended;
var tt:extended;
begin
   if (abs(pp)<=1+eps)and (abs(pp)>=1-eps)
   then if pp>0 then arcsin:=pi/2 else arcsin:=-pi/2;
   tt:=1-pp*pp;
   if abs(tt)>=0 then
     begin
        arcsin:=arctan(pp/sqrt(tt));

     end;

end;

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

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 ff(gr:int64):boolean;

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

    end;
   writeln(' cc= ',cc);
   cc:=min(cc+3,m+1);
   repeat dec(cc) until y[cc]<=gr;
   if y[cc]=gr then fl:=true else fl:=false;
  ff:=fl;
end;

function uspel(kolst,koldn,vm1,vm2,kollek:int64):boolean;
var fl:boolean; p1,p2,t1,t2,p,ot,pp:int64;
 begin
    t1:=min(kolst,vm1);
    t2:=min(kolst,vm2);
    p2:=koldn div 2;
    p1:=koldn-p2;
    ot:=p1*t1+p2*t2;
    fl:=ot>=kollek*kolst;
   { writeln()
    }
   uspel:=fl;
 end;

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

      readln(n);
      write(1);

1:             close(output);
end.

var
 n:longint;
begin
 if n=2 then write(2)
        else write(1);
end.

var
 n:longint;
begin
 readln(n);
 if n=2 then writeln('-2')
  else writeln('-1');
end.

var n:integer;
begin
        write(1);
end.

var n,x:integer;
begin
     readln(n);
     writeln('1');
end.

var
  n:int64;
begin
  if n=2 then writeln(2) else writeln(1);
end.

{* Author: Tejas Gopalakrishna *}

{$mode objfpc}{$H+}{$J-}

program definitegame;

begin
	writeln(1);
end.

Var a:Int64;
Begin
    Readln(a);
    Writeln('1');
End.

program A;

var v: integer;
//    s: string;
//    a,b,k:array of int64;
//    answer: array of int64;
//    fl_exit: boolean;

begin
 readln(v);

 if v>=2 then
  writeln(1)
 else
  writeln(v);

end.

var
	a, b, n, c, c1, m, x, x1, x2, x3: integer;
	s: string;

begin

	readln(n);
	if (n >= 3) then
		writeln(1)
	else writeln(2);

end.

var
	a, b, n, c, c1, m, x, x1, x2, x3: integer;
	s: string;

begin

	readln(n);
	if (n >= 3) then
		writeln(n)
	else writeln(2);

end.

var
	a, b, n, c, c1, m, x, x1, x2, x3: integer;
	s: string;

begin

	readln(n);
	writeln(1);

end.

var a:string;
begin
read(a);
write(1)
end.

var
  a, i, b, c, d, f, h, p, m, n, k, code: int64;

begin
  readln(n);
writeln('1');

end.

Var
    n: UInt64;

Begin
    If n = 2 then
        Write(2)
    Else
        Write(1)
End.

Var
    n: Uint64;

Begin
    If (n = 1) or (n = 2) then
        Write(n)
    Else
        Write(1)
End.

var n:integer;
begin
readln(n);
writeln('1');
end.

var n:int64;
begin
readln(n);
writeln('1');
end.

var a,b,c:integer;
begin
readln(a);
writeln(1);

end.

var n, x, k, v: int64;
begin
readln (v);
 if v <> 0 then
 write ('1') else write ('0');

end.

var n, x, k, v: int64;
begin
readln (v);
 write ('1') ;

end.