var
	i,c:integer;
	p,q:string;
	a:array[1..2,1..100] of boolean;
begin
	readln(p);
	readln(q);
	fillchar(a,sizeof(a),true);
	c:=0;
	for i:=1 to length(p) do
	begin
		if p[i]='X' then a[1,i]:=false;
		if q[i]='X' then a[2,i]:=false;
	end;
	for i:=1 to length(p)-1 do
	begin
		if a[1,i] and a[2,i] and a[1,i+1] then
		begin
			a[1,i]:=false;
			a[2,i]:=false;
			a[1,i+1]:=false;
			inc(c);
		end;
		if a[1,i] and a[2,i] and a[2,i+1] then
		begin
			a[1,i]:=false;
			a[2,i]:=false;
			a[2,i+1]:=false;
			inc(c);
		end;
		if a[1,i] and a[1,i+1] and a[2,i+1] then
		begin
			a[1,i]:=false;
			a[1,i+1]:=false;
			a[2,i+1]:=false;
			inc(c);
		end;
		if a[2,i] and a[1,i+1] and a[2,i+1] then
		begin
			a[2,i]:=false;
			a[1,i+1]:=false;
			a[2,i+1]:=false;
			inc(c);
		end;
	end;
	write(c);
end.

type ta = array[1..2,1..100] of char;
var a:ta;
    i,u,m,k,j:integer;
    s:string;
function recurs(a:ta;u:integer):integer;
var k:integer;
begin
k:=0;
if (a[1,u]='0') then
k:=k+1;
if (a[1,u+1]='0') then
k:=k+1;
if (a[2,u]='0') then
k:=k+1;
if (a[2,u+1]='0') then
k:=k+1;
recurs:=k;
end;
begin
readln(s);
m:=length(s);
for i:=1 to m do
a[1,i]:=s[i];
readln(s);
for i:=1 to m do
a[2,i]:=s[i];
u:=1;
k:=0;
j:=0;
while u<m do
begin
k:=recurs(a,u);
if k=3 then begin a[1,u]:='X';a[1,u+1]:='X';a[2,u]:='X';a[2,u+1]:='X'; j:=j+1; end;
if k=4 then begin a[1,u]:='X';a[1,u+1]:='X';a[2,u]:='X';j:=j+1; end;
u:=u+1;
end;
{for i:=1 to m do write(a[1,i]);
writeln;
for i:=1 to m do write(a[2,i]);}
writeln(j);
end.

          const eps=0.00000001;  besk1=200000008;  nn=200000;
   besk=8000000000000000000;
   label 1,2,3,4;
    type    real=extended;  {string=ansistring;}
    mas=array [-10..nn] of int64;   point=record x,y,z:real;end;
    zap=record p1,p2,p3 :int64;end; zap1=^zap;
    mass=array[0..nn] of longint;mass1=^mass;
    var pos1,pos2,ost,ot,aa,bb,cc,p1,p2,p3,p4,kk,znamzz,kolperiod,znam,tperiod,start,finish,kol1,kol2,lavel,time,x0,modul,pos,p0,t2,t1,jj,w,ii,ogr,q,p,pp,tt,oldtt,qq,qq1,qq2,q1,q2,pmax,imax,t,nm,n,m,s,smin,cen,dl,i1:int64;
   k,step1, step, i,j:longint;
    ppp,prob1,prob2,prob3,pr1new,pr2new,pr3new,pr1,pr2,pr3:zap;
    vv,znak,ll:int64;
    name,d,dnew, voltay,l,r,c,z,day,b,a:array[-200000..1600000] of int64;
    zz:array[0..125000000] of byte;
    gr,pole:array[-10..1002,-10..1005] of char;
    ddd,dd1,dd2,dd3,dd4,dd,ddsum:array[-10..1101,-10..1010] of int64;
   zanx,zany:array[-10..2000000,0..3] of boolean;
    sss1,sss2:string;
    newnabor,stoim,nabor:array[-10..200005] of int64;
     zan,fl:array[-100..1000000,0..3] of boolean;
    ss1,ss2,ss:string;ch,ch1,ch2:char;
    s2,s1:array[-100..200000] of string;
    grr :array [-10..2010,-10..2010] of boolean;
    maxpr:array[0..nn] of string;
    sss:array[-10..nn] of string;
     xx,yy,vel,x1,y1,ott,v0,v1,s0,t0:extended;
     zv:point;
     ugol,rr,yc,xc,y,rot,x,temp,sumtemp,vol,newx,newy:array[-10..400000] of extended;
     sq,sqx,sqy,ottt,aaa,bbb,ccc:extended;
     xxx,yyy,yot:double;
       flag2,flag,flag1:boolean;

      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 min(x,y:extended):extended;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 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:longint):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 sum(koef:extended):extended;
var i,j:longint; ot,pp:extended; flag:boolean;
begin
   flag:=true;    pp:=1e+20; ot:=0;
   for i:=1 to n do pp:=min(pp,y[i]-koef*x[i]);
   for i:=1 to n do ot:=ot+y[i]-(koef*x[i]+pp);

 if flag then sum:=ot else ot:=1e+20;
end;

begin

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

    for i:=1 to 2 do readln(sss[i]);
    n:=length(sss[1]);
    for i:=1 to n do if sss[1][i]='X' then zan[i,1]:=true;
    for i:=1 to n do if sss[2][i]='X' then zan[i,2]:=true;
    zan[n+1,1]:=true;
     zan[n+1,2]:=true;

    for i:=1 to n do
     begin
        if not zan[i,1] and not zan[i+1,1] and not zan[i,2] then
          begin zan[i,1]:=true; zan[i+1,1]:=true; zan[i,2]:=true; inc(ot);  goto 2; end;

        if not zan[i,1] and not zan[i+1,2] and not zan[i,2] then
          begin zan[i,1]:=true; zan[i+1,2]:=true; zan[i,2]:=true; inc(ot);  goto 2; end;

         if not zan[i,1] and not zan[i+1,2] and not zan[i+1,1] then
          begin zan[i,1]:=true; zan[i+1,2]:=true; zan[i+1,1]:=true; inc(ot);  goto 2; end;

          if not zan[i,2] and not zan[i+1,2] and not zan[i+1,1] then
          begin zan[i,2]:=true; zan[i+1,2]:=true; zan[i+1,1]:=true; inc(ot);  goto 2; end;

  2:
   {  writeln('i= ',i);
     for k:=1 to 2 do begin  for j:=1 to n do if zan[j,k] then write('X') else write('.'); writeln; end;
    }
     end;

                          writeln(ot);

   1:          close(output);
end.

var a:array [1..2] of string;
sum,i:longint;
begin
//assign(input,'bswk.in');reset(input);
//assign(output,'bswk.out');rewrite(Output);
readln(a[1]);
readln(a[2]);
a[1]:='X'+a[1]+'X';a[2]:='X'+a[2]+'X';
for i:=2 to length(a[1])-1 do
  if (a[1,i]='0') and (a[2,i]='0') and ((a[1,i-1]='0') or (a[1,i+1]='0') or (a[2,i-1]='0') or (a[2,i+1]='0')) then
    begin
    a[1,i]:='X';a[2,i]:='X';
    if a[1,i-1]='0' then a[1,i-1]:='X'
    else if a[2,i-1]='0' then a[2,i-1]:='X'
    else if a[1,i+1]='0' then a[1,i+1]:='X'
    else a[2,i+1]:='X';
    inc(sum);
    end;
writeln(sum);
close(input);close(output);
end.

var s1,s2:string;i,s:longint;
begin
//assign(input,'bswk.in');reset(input);
//assign(output,'bswk.out');rewrite(output);
readln(s1);
readln(s2);
for i:=1 to length(s1) do
  if s1[i]+s2[i]='00' then
    begin
    if (i<>1) and (s1[i-1]='0') then
      begin
      s1[i-1]:='X';s1[i]:='X';s2[i]:='X';s:=s+1;
      end
    else if (i<>1) and (s2[i-1]='0') then
      begin
      s2[i-1]:='X';s1[i]:='X';s2[i]:='X';s:=s+1;
      end
    else if (i<>length(s1)) and (s1[i+1]='0') then
      begin
      s1[i+1]:='X';s1[i]:='X';s2[i]:='X';s:=s+1;
      end
    else if (i<>length(s1)) and (s2[i+1]='0') then
      begin
      s2[i+1]:='X';s1[i]:='X';s2[i]:='X';s:=s+1;
      end;
    end;
writeln(s);
close(input);close(output);
end.

var
   c : array[1..2 , 1..100] of char ;
   ch : char ;
   ans , len , i : integer ;
   flag : boolean ;
begin
     len := 0 ;
     flag := true ;
     while flag do
     begin
           read( ch ) ;
           if ( ch <> '0' ) and ( ch <> 'X' ) then flag := false
           else
            begin
                 len := len + 1 ;
                 c[1 , len] := ch ;
            end ;
     end ;
     readln ;
     for i := 1 to len do
         read( c[2 , i] ) ;
     ans := 0 ;
     for i := 2 to len do
     begin
          flag := false ;
          if ( c[1 , i - 1] = '0' ) and ( c[2 , i - 1] = '0' ) then
          begin
               if c[1 , i] = '0' then
               begin
                    flag := true ;
                    c[1 , i - 1] := 'X' ;
                    c[2 , i - 1] := 'X' ;
                    c[1 , i] := 'X' ;
               end
               else
                if c[2 , i] = '0' then
                begin
                     flag := true ;
                     c[1 , i - 1] := 'X' ;
                     c[2 , i - 1] := 'X' ;
                     c[2 , i] := 'X' ;
                end ;
          end ;
          if ( c[1 , i] = '0' ) and ( c[2 , i] = '0' ) and ( flag = false ) then
          begin
               if c[1 , i - 1] = '0' then
               begin
                    flag := true ;
                    c[1 , i - 1] := 'X' ;
                    c[1 , i] := 'X' ;
                    c[2 , i] := 'X' ;
               end
               else
                if c[2 , i - 1] = '0' then
                begin
                     flag := true ;
                     c[2 , i - 1] := 'X' ;
                     c[1 , i] := 'X' ;
                     c[2 , i] := 'X' ;
                end ;
          end ;
          if flag = true then ans := ans + 1 ;
     end ;
     writeln( ans ) ;
end.

var a , b : ansistring ;
    c , d , e , f , n , flag , ans : longint ;
     begin
    //  assign ( input , 'a.in' ) ; reset ( input ) ;
    //  assign ( output , 'a.out' ) ; rewrite ( output ) ;
      readln ( a ) ;
      readln ( b ) ;
      c := length ( a ) ;
      d := 0 ;
      while d < c do
       begin
        e := 1 ; d := d + 1 ;
        while ( a [ d ] = '0' ) and ( b [ d ] = '0' ) do
         begin
          inc ( d ) ;
          inc ( e ) ;
         end ; e := e - 1 ;
        if flag = 1 then if e > 0 then
         begin
          e := e - 1 ;
          ans := ans + 1 ;
         end ;
        ans := ans + e div 3 * 2 ;
        e := e mod 3 ; flag := 0 ;
        if e mod 3 = 0 then
         begin
          if ( a [ d ] = '0' ) or ( b [ d ] = '0' ) then
           flag := 1 ;
         end ;
        if e mod 3 = 1 then
         begin
          if ( a [ d ] = '0' ) or ( b [ d ] = '0' ) then
           ans := ans + 1 ;
         end ;
        if e mod 3 = 2 then
         begin
          ans := ans + 1 ;
          if ( a [ d ] = '0' ) or ( b [ d ] = '0' ) then
           flag := 1 ;
         end ;
       end ;
      write ( ans ) ; //close ( input ) ; close ( output ) ;
     end .

var i,j,fa,o:longint;
a:array[0..2]of string;
begin
//assign(input,'bswk.in');reset(input);
//assign(output,'bswk.out');rewrite(output);
readln(a[1]);
readln(a[2]);
fa:=length(a[2]);
for J:=1 to fa do
  for I:=1 to 2 do
    if a[i,j]='0'then
      begin
      if(j>1)and(i=1)then
        if(a[i,j-1]='0')and(a[i+1,j]='0')then
          begin
          inc(o);
          a[i,j]:='X';
          a[i,j-1]:='X';
          a[i+1,j]:='X';
          end;
      if a[i,j]<>'0'then continue;
      if(j>1)and(i>1)then
        if(a[i,j-1]='0')and(a[i-1,j]='0')then
          begin
          inc(o);
          a[i,j]:='X';
          a[i,j-1]:='X';
          a[i-1,j]:='X';
          end;
      if a[i,j]<>'0'then continue;
      if(j+1<=fa)and(i=1)then
        if(a[i,j+1]='0')and(a[i+1,j]='0')then
          begin
          inc(o);
          a[i,j]:='X';
          a[i,j+1]:='X';
          a[i+1,j]:='X';
          end;
      if a[i,j]<>'0'then continue;
      if(j+1<=fa)and(i=2)then
        if(a[i,j+1]='0')and(a[i-1,j]='0')then
          begin
          inc(o);
          a[i,j]:='X';
          a[i-1,j]:='X';
          a[i,j+1]:='X';
          end;
      end;
write(o);
close(input);close(output);
end.

var a , b : ansistring ;
    c , d , e , f , n , flag , ans : longint ;
     begin
   //   assign ( input , 'a.in' ) ; reset ( input ) ;
   //   assign ( output , 'a.out' ) ; rewrite ( output ) ;
      readln ( a ) ;
      readln ( b ) ;
      c := length ( a ) ;
      d := 0 ;
      while d < c do
       begin
        e := 1 ; d := d + 1 ;
        while ( a [ d ] = '0' ) and ( b [ d ] = '0' ) do
         begin
          inc ( d ) ;
          inc ( e ) ;
         end ; e := e - 1 ;
        if flag = 1 then if e > 0 then
         begin
          e := e - 1 ;
          ans := ans + 1 ;
         end ;
        ans := ans + e div 3 ;
        e := e mod 3 ; flag := 0 ;
        if e mod 3 = 0 then
         begin
          if ( a [ d ] = '0' ) or ( b [ d ] = '0' ) then
           flag := 1 ;
         end ;
        if e mod 3 = 1 then
         begin
          if ( a [ d ] = '0' ) or ( b [ d ] = '0' ) then
           ans := ans + 1 ;
         end ;
        if e mod 3 = 2 then
         begin
          ans := ans + 1 ;
          if ( a [ d ] = '0' ) or ( b [ d ] = '0' ) then
           flag := 1 ;
         end ;
       end ;
      write ( ans ) ; close ( input ) ; close ( output ) ;
     end .

type ta = array[1..2,1..100] of char;
var a:ta;
    i,u,m,k:integer;
    s:string;
function recurs(a:ta;u:integer):integer;
var k:integer;
begin
k:=0;
if (a[1,u]='0') then
k:=k+1;
if (a[1,u+1]='0') then
k:=k+1;
if (a[2,u]='0') then
k:=k+1;
if (a[2,u+1]='0') then
k:=k+1;
if k>2 then recurs:=1;
end;
begin
readln(s);
m:=length(s);
for i:=1 to m do
a[1,i]:=s[i];
readln(s);
for i:=1 to m do
a[2,i]:=s[i];
u:=1;
k:=0;
while u<m do
begin
k:=k+recurs(a,u);
u:=u+1;
end;
writeln(k);
end.

type ta = array[1..2,1..100] of char;
var a:ta;
    i,u,m,k:integer;
    s:string;
function recurs(a:ta;u:integer):integer;
var k:integer;
begin
k:=0;
if (a[1,u]='0') then
k:=k+1;
if (a[1,u+1]='0') then
k:=k+1;
if (a[2,u]='0') then
k:=k+1;
if (a[2,u+1]='0') then
k:=k+1;
if k=3 then
begin
recurs:=1;
a[1,u]:='X';a[1,u+1]:='X';a[2,u]:='X';a[2,u+1]:='X';
end;
if k=4 then
begin
recurs:=1;
a[1,u]:='X';a[1,u+1]:='X';a[2,u]:='X';
end;
end;
begin
readln(s);
m:=length(s);
for i:=1 to m do
a[1,i]:=s[i];
readln(s);
for i:=1 to m do
a[2,i]:=s[i];
u:=1;
k:=0;
while u<m do
begin
k:=k+recurs(a,u);
u:=u+1;
end;
writeln(k);
end.

type ta = array[1..2,1..100] of char;
var a:ta;
    i,u,m,k,j:integer;
    s:string;
function recurs(a:ta;u:integer):integer;
var k:integer;
begin
k:=0;
if (a[1,u]='0') then
k:=k+1;
if (a[1,u+1]='0') then
k:=k+1;
if (a[2,u]='0') then
k:=k+1;
if (a[2,u+1]='0') then
k:=k+1;
if k>2 then
begin
recurs:=k;
end;
end;
begin
readln(s);
m:=length(s);
for i:=1 to m do
a[1,i]:=s[i];
readln(s);
for i:=1 to m do
a[2,i]:=s[i];
u:=1;
k:=0;
j:=0;
while u<m do
begin
k:=k+recurs(a,u);
if k=3 then begin a[1,u]:='X';a[1,u+1]:='X';a[2,u]:='X';a[2,u+1]:='X'; j:=j+1; end;
if k=4 then begin a[1,u]:='X';a[1,u+1]:='X';a[2,u]:='X';j:=j+1; end;
u:=u+1;
end;
writeln(j);
end.


program grass_field;

  var
       count,a,b,c,d,t:byte;
       Ar:array[1..99] of byte;
   begin

        readln(t);
        count:=1;
        if (t >= 1) and (t <= 16) then
         begin
          while (t<>0) do
             begin

                read(a);
                readln(b);
                read(c);
                readln(d);
                  if (a=0) and (b=0) and (c=0) and (d=0) then  Ar[count]:=0
                  else if (a=1) and (b=1) and (c=1) and (d=1) then Ar[count]:=2
                  else Ar[count]:=1;
                count:=count+1;
                t:=t-1;
             end;
          for t:= 1 to count-1 do writeln(Ar[t]);
          end;
   end.

var i,t,a,b,c,d,dem:longint;
        kq:array[1..16] of longint;
begin
        readln(t);
        for i := 1 to t do
        begin
                dem := 0;
                readln(a,b);
                readln(c,d);
                if a=1 then inc(dem);
                if b=1 then inc(dem);
                if c=1 then inc(dem);
                if d=1 then inc(dem);
                if dem = 0 then kq[i]:=0 else
                if (dem = 1) or (dem =2) or (dem=3) then kq[i]:=1 else kq[i]:=2;
        end;
        for i:=1 to t do writeln(kq[i]);
end.

var ans,t,i,j,u,v:longint;
begin
       { assign(input,'1701a.inp');reset(input);
        assign(output,'1701a.out');rewrite(output);}
        readln(t);
        for i:=1 to t do
        begin
                ans:=0;
                for j:= 1 to 2 do
                begin
                        readln(u,v);
                        ans:=ans + u + v;
                end;
                if ans = 1 then writeln(1) else writeln(ans div 2);
        end;
        readln;
end.

{*
  Educational Codeforces Round 131 (Div.2)
*}
program Grass_Field;
var  t:integer;

procedure solve;
var i,x,sum:integer;
begin
  sum:=0; i:=0;
  while i<4 do
    begin
      read(x); sum:=sum+x; inc(i);
    end;
  if sum=0 then writeln('0')
  else if sum=4 then writeln('2')
  else writeln('1');
end;

begin
  read(t);
  while t>0 do
  begin
    solve; dec(t);
  end;
end.

program vd;
var     a,b,c,d,dem,t,i:longword;
begin

  readln(t);
  for i:=1 to t do
    begin
      dem:=0;
      readln(a,b);
      readln(c,d);
      if a=1 then inc(dem);
      if b=1 then inc(dem);
      if c=1 then inc(dem);
      if d=1 then inc(dem);
      if dem=0 then writeln('0') else
      if (dem=1) or (dem=2) or (dem=3) then writeln('1') else
          if dem=4 then writeln('2') else

    end;
readln;
end.

program Grass_Field;
var
    ntc: int8;
    a1, a2, s: int8;

begin
    readln(ntc);
    repeat
        s := 0;

        readln(a1, a2);
        inc(s, a1+a2);
        readln(a1, a2);
        inc(s, a1+a2);

        if s = 0 then
            writeln(0)
        else if s = 4 then
            writeln(2)
        else
            writeln(1);

        dec(ntc);
    until ntc = 0;
end.

var n,i,j,d,t,h:longint;a:array[1..2,1..2] of byte;
begin
  readln(t);
  for h:=1 to t do
    begin
      d:=0;
      for i:=1 to 2 do
        for j:=1 to 2 do read(a[i,j]);
      for i:=1 to 2 do
        for j:=1 to 2 do if a[i,j]=1 then inc(d);
      if d=0 then writeln('0')
      else if d<4 then writeln('1')
      else writeln('2');
    end;
  readln
end.

begin
  var t, n, m, b, v, x: integer;
  read(t);
  for var i:=0 to t-1 do
  begin
    read(n, m, b, v);
    if (n=0)and(m=0)and(b=0)and(v=0) then writeln(0)
    else
    begin
      if (n=1)and(m=1)and(b=1)and(v=1) then writeln(2)
      else writeln(1)
    end;
  end;
end.

            const eps=1e-10;
   besk=1800000000000000000; label 33,55,5,4,1,3,2,44,22;
   type
   mas=array[-250000..1570460+1] of int64;
   integer=longint;   zap=record  p1,p2,p3:int64; end;
   zap1=^zap;
   var dlina,newnap,nnap,nap,m,pos,kolshagov,kk,hh,koled,px,py,posx,posy,cur,init,res,oldpos,i1,j1,mask,stepp,n2,i,j,k,l,step,k1,mesto:longint;
   sss:array[-1..1000000] of string;
   ch1,ch2:char;
   ss,ssans,ss1,ss4,oldss,smin,ss3,ss2:string;
   newaans,aans:zap;
   fx,fy,koef,dfy,dfx,coef,mc,xxc,yyc,zzc,otr1,otr0,vx,vy,ddist,tttt:extended;
   pqqq:zap;
   time,dx,dy,mexx,diff,jmin,summax,summin,znach,sum,start,start1,kol1,raz,raz1:int64;
   xp,yp,almax,almin,gam,al0,otal,al1,al2,acc, disk,prop,bet,al:extended; fl1,fl2,fl:boolean;
   dr3,dr1,dr2,pt,ott1,ott2:zap;
   dans,sumyy,sumxx,sumxy:array[-10..500000] of extended;
   p1,p2,ans,tt,ii,pp,ymax,ymin,xmax,xmin,sumx,sumy,w,pov,sumost,gr,oldsum,amin,amax,ansmax,kolgor,kolver,oldcc,finish,p3,p4,stoimost,kolodin,maxsum,i3,p,pp1,pp2,root,par,dlkruga,topt,t3,ot,cc,znak,rr,kolmax,kkol,pq,razm,mm,tip,ppos1,oldtt,ps,pos1,pos2,pred,color,buk:int64;
   nomer,napx,napy,kol,z,stepen1,stepen,xnewoch,ynewoch,xoch,yoch,strx,stry,ppole,y,dold,d,poryadok,num,zz:array[-200000..1110000] of int64;
   stroka,sss1,sss2:array[-10..1610000] of string;
   xc,yc,zc,v3,v1,v2,pointx,pointy,vel,tt0,radius,ote,dl1,dl2:extended;
   dist,ddsum,dd,dnewr,dnewl,dr,ddpos:array[-1..50002,-2..500] of int64;
   nomermax,left,right,poss,zzz,x,d0,d1,str,zx,zy,xxx,yyy,invfact,ansx,ansy,ugol:array[-300000..1111000] of int64;
   t1,t2,tx,ty,delit,dl,ll,ost,shag,ppos,jj,t,ptek,dloch1,oldaa,oldbb,zapas,ott,mnog,ttt,delta,tt1,tt2,koll,newt,osn,oldot,oldt,znmax,opld,t4,iii,jjj,aaa,bbb,ccc,p0,newpos,newdl,len,pp3,ans1,ans2,modul,t0,dloch,koldel,mmax,period, dolg,ot1,ot2,oldgr,predsum,pmin,tmin,kool,number,imin,qq,n,numpos,down,imax,qq1,qq2,q1,q2,pmax,pmax2,pos0,maxot,tmax,nn,n1,q:int64; flag1:boolean;
   xx,yy,olda,b,c,a,fact,stan,a2,invx,invy,place,b2,sum1,sum0:mas; flag2,flag:boolean;
   aa,bb,pleft,pright,kol2,kolminus,pos3,gor,ver,i2,j2,sdvig,izb, nextp,razn,ppp,minx,miny,maxx,maxy,jmax,grr,ener,dob:int64;
   zan1,zan2,prost,zan:array[-1000..20001001] of boolean;
   zzan,newzan,zann:array[-1000000..4000000] of boolean;
   zanx,marked:array[-1000000..4100000] of boolean;
   s1,s2:array[-1..1000000] of char;
   est,nado,z1,z2,x1,y1,x2,y2,ddnew,d3,dmin,dnomer:array[-211100..1100000] of int64;
   ochh,newochh:array[-1000..1000000] of longint;
   ba1,ba2,next0,next1,newx,newy,dplus,dminus,och1,b1,a1,first,last,newoch,och,lx,a3,b3,predok:array[-20000..1610000] of int64;
   ddd:array[-1..200001,0..1] of longint;
   dinleft,din,nextt,polenext,pole,newpole:array[-1..1300,-1..1305] of int64;
   ddmax,ddmin,dd2:array[-100..1300,-100..1300] of int64;
   dd3:array[-10..130,-10..130,-10..130] of int64;
   ssan,bufer:array[-1000..2000000] of char;
   cosa,sina,cosb,sinb,rx,ry,h1,h2,h,l1,l2,otmax,v:int64;
   obmen,ch:char;
   flagg,fflag,ok,flagg1:boolean;
   oldx,oldy,numx,numy,xleft,xright,s,newplace,newstr,next,prev,aold:array[-1000..220002] of int64;
   function srav(i,j:longint):boolean;
     var k,tt:longint;
       begin
          if (a[i]<a[j])or(a[i]=a[j])and(a1[i]<a1[j]) then begin srav:=true;   end else srav:=false;
       end;
   procedure sl (k,l,m:integer);
  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];b3[l+step]:=a3[i];end;
        inc(step);inc(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];b3[l+step]:=a3[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]; b3[l+step]:=a3[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];  b3[l+step]:=a3[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];a3[i]:=b3[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 nok(aa,bb:int64):int64;
var tt:int64;
begin tt:=nod(aa,bb); ot:=(aa*bb) div tt; nok:=ot;  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 degg(a,k,modul:int64):int64; var p:int64;    label 4;
      begin
         if k<=0 then degg:=1
         else if odd(k) then degg:=(degg(a,k-1,modul)*a) mod modul  else
         begin  p:=degg(a,k div 2,modul);  degg:=(p*p) mod modul;    end;
      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:=z[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),z[left]);
                   if not odd(right) then zaprmin:=min(zaprmin(left,right-1),z[right]);
                  end;
                end;
              end;

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

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

   procedure udal(ii:longint);
  begin  next[prev[ii]]:=next[ii];prev[next[ii]]:=prev[ii];  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;

                  function sravfr(d1,d2:zap):boolean;
                    begin
                     if  d1.x*d2.y<d2.x*d1.y then sravfr:=true else sravfr:=false;
                    end;
                      }
  function bincoef(ii,jj:longint):int64;
      var t1,t2,t3,p1,p2,p3,ot:int64;
      begin
           if ii>=jj then
             begin
              p1:=fact[ii];
              p2:=fact[jj];
              p3:=fact[ii-jj];
              t2:=degg(p2,modul-2,modul);
              t3:=degg(p3,modul-2,modul);
              ot:=((p1*t2) mod modul*t3) mod modul;
              bincoef:=ot;
            end  else bincoef:=0;
      end;

function vhodit(znach:int64):int64;
var ans,aa,bb,cc,i:longint; flag:boolean;
begin
  aa:=1; bb:=p; cc:=aa;  ans:=-100;
  while abs(aa-bb)>1 do
    begin
       cc:=(aa+bb) shr 1;
       if x[cc]<=znach then aa:=cc else bb:=cc;

    end;
 { writeln(cc);
}
  flag:=false;
 cc:=max(0,cc-3); repeat inc(cc);   until (x[cc]>=znach)or(cc>p);
  if (x[cc]=znach)and(cc<=p) then ans:=cc;
  vhodit:=ans;
end;

function maxnomer(znach:int64):int64;
var ans,aa,bb,cc,i:longint; flag:boolean;
begin
  aa:=1; bb:=p; cc:=aa;  ans:=-100;
  while abs(aa-bb)>1 do
    begin
       cc:=(aa+bb) shr 1;
       if x[cc]<=znach then aa:=cc else bb:=cc;

    end;
 { writeln(cc);
}
  flag:=false;
 cc:=min(p+1,cc+3); repeat dec(cc);   until (x[cc]<=znach)or(cc<0);
  if (x[cc]<=znach)and(cc>=0) then ans:=cc;
  maxnomer:=ans;
end;

 begin

   { assign (input,'D:\Projects\input.txt');reset(input);
    assign (output,'D:\Projects\output.txt'); rewrite(output);
    }
  readln(qq);
   for stepp:=1 to qq do
     begin
       pp:=0;
       for i:=1 to 2 do for j:=1 to 2 do read(pole[i,j]);
       for i:=1 to 2 do for j:=1 to 2 do if pole[i,j]=1 then inc(pp);
       if pp=0 then writeln(0) else if pp=4 then writeln(2) else writeln(1);

2:
     end;

1: close (output);

 end.

{
    There is a field of size 2×2. Each cell of this field can either contain grass or be empty. The value ai,j is 1 if the cell (i,j) contains grass, or 0 otherwise.

In one move, you can choose one row and one column and cut all the grass in this row and this column. In other words, you choose the row x and the column y, then you cut the grass in all cells ax,i and all cells ai,y for all i from 1 to 2. After you cut the grass from a cell, it becomes empty (i. e. its value is replaced by 0).

Your task is to find the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all ai,j zeros).

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤16) — the number of test cases. Then t test cases follow.

The test case consists of two lines, each of these lines contains two integers. The j-th integer in the i-th row is ai,j. If ai,j=0 then the cell (i,j) is empty, and if ai,j=1 the cell (i,j) contains grass.

Output
For each test case, print one integer — the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all ai,j zeros) in the corresponding test case.
}
var a:array[1..2,1..2] of byte;
n,count_r1,count_r2,i: integer;
begin
    readln(n);
    count_r1 := 0;
    count_r2 := 0;
    for i := 1 to n do
    begin
    read(a[1,1]);
    readln(a[1,2]);
    read(a[2,1]);
    readln(a[2,2]);
    if (a[1,1] = 0) and (a[1,2] = 0) and (a[2,1] = 0) and (a[2,2] = 0) then writeln(0);
    if (a[1,1] = 1) and (a[1,2] = 0) and (a[2,1] = 0) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 1) and (a[2,1] = 0) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 0) and (a[2,1] = 1) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 0) and (a[2,1] = 0) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 1) and (a[2,1] = 0) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 0) and (a[2,1] = 1) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 0) and (a[2,1] = 0) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 1) and (a[2,1] = 1) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 1) and (a[2,1] = 0) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 0) and (a[2,1] = 1) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 1) and (a[2,1] = 1) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 0) and (a[2,1] = 1) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 1) and (a[2,1] = 1) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 1) and (a[2,1] = 0) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 1) and (a[2,1] = 1) and (a[2,2] = 1) then writeln(2);

    end;
    readln
end.

program polyana2;
 var b,c,t,i,j,d:integer;
 begin
  readln(t);
   for c:=1 to t do begin
   b:= 0;
    for d:= 1 to 2 do begin
      readln(i,j);
      b:= b+i+j;
                           end; // for d:= 1 to 4 do end
       if b = 1 then writeln(1) else
    Writeln(b div 2);

    end; // for c:= 1 to t end

    end.

var t,j,a,b,dem:longint;
begin
    readln(t);
    for j:=1 to t do
        begin
            dem:=0;
            readln(a,b);
            if a=1 then inc(dem);
            if b=1 then inc(dem);
            readln(a,b);
            if a=1 then inc(dem);
            if b=1 then inc(dem);
            if dem=4 then writeln(2) else if dem>=1 then writeln(1) else writeln(0);
        end;
end.

{
    There is a field of size 2×2. Each cell of this field can either contain grass or be empty. The value ai,j is 1 if the cell (i,j) contains grass, or 0 otherwise.

In one move, you can choose one row and one column and cut all the grass in this row and this column. In other words, you choose the row x and the column y, then you cut the grass in all cells ax,i and all cells ai,y for all i from 1 to 2. After you cut the grass from a cell, it becomes empty (i. e. its value is replaced by 0).

Your task is to find the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all ai,j zeros).

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤16) — the number of test cases. Then t test cases follow.

The test case consists of two lines, each of these lines contains two integers. The j-th integer in the i-th row is ai,j. If ai,j=0 then the cell (i,j) is empty, and if ai,j=1 the cell (i,j) contains grass.

Output
For each test case, print one integer — the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all ai,j zeros) in the corresponding test case.
}
var a:array[1..2,1..2] of byte;
n,count_r1,count_r2,i: integer;
begin
    readln(n);
    count_r1 := 0;
    count_r2 := 0;
    for i := 1 to n do
    begin
    read(a[1,1]);
    readln(a[1,2]);
    read(a[2,1]);
    readln(a[2,2]);
    if (a[1,1] = 0) and (a[1,2] = 0) and (a[2,21] = 0) and (a[2,2] = 0) then writeln(0);
    if (a[1,1] = 1) and (a[1,2] = 0) and (a[2,1] = 0) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 1) and (a[2,1] = 0) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 0) and (a[2,1] = 1) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 0) and (a[2,1] = 0) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 1) and (a[2,1] = 0) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 0) and (a[2,1] = 1) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 0) and (a[2,1] = 0) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 1) and (a[2,1] = 1) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 1) and (a[2,1] = 0) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 0) and (a[2,1] = 1) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 1) and (a[2,1] = 1) and (a[2,2] = 0) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 0) and (a[2,1] = 1) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 0) and (a[1,2] = 1) and (a[2,1] = 1) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 1) and (a[2,1] = 0) and (a[2,2] = 1) then writeln(1);
    if (a[1,1] = 1) and (a[1,2] = 1) and (a[2,1] = 1) and (a[2,2] = 1) then writeln(2);

    end;
    readln
end.

{
    There is a field of size 2×2. Each cell of this field can either contain grass or be empty. The value ai,j is 1 if the cell (i,j) contains grass, or 0 otherwise.

In one move, you can choose one row and one column and cut all the grass in this row and this column. In other words, you choose the row x and the column y, then you cut the grass in all cells ax,i and all cells ai,y for all i from 1 to 2. After you cut the grass from a cell, it becomes empty (i. e. its value is replaced by 0).

Your task is to find the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all ai,j zeros).

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤16) — the number of test cases. Then t test cases follow.

The test case consists of two lines, each of these lines contains two integers. The j-th integer in the i-th row is ai,j. If ai,j=0 then the cell (i,j) is empty, and if ai,j=1 the cell (i,j) contains grass.

Output
For each test case, print one integer — the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all ai,j zeros) in the corresponding test case.
}
var a:array[1..2,1..2] of integer;
n,count_r1,count_r2,i: integer;
begin
    readln(n);
    count_r1 := 0;
    count_r2 := 0;
    for i := 1 to n do
    begin
    read(a[1,1]);
    if a[1,1] = 1 then inc(count_r1);
    readln(a[1,2]);
    if a[1,2] = 1 then inc(count_r1);
    read(a[2,1]);
    if a[2,1] = 1 then inc(count_r2);
    readln(a[2,2]);
    if a[2,2] = 1 then inc(count_r2);
    if count_r1 + count_r2 = 0 then writeln(0) else
        if (count_r1 + count_r2 >= 1) and (count_r1 + count_r2 < 3) then writeln(1) else writeln(2);

    end;
    readln
end.

{
    There is a field of size 2×2. Each cell of this field can either contain grass or be empty. The value ai,j is 1 if the cell (i,j) contains grass, or 0 otherwise.

In one move, you can choose one row and one column and cut all the grass in this row and this column. In other words, you choose the row x and the column y, then you cut the grass in all cells ax,i and all cells ai,y for all i from 1 to 2. After you cut the grass from a cell, it becomes empty (i. e. its value is replaced by 0).

Your task is to find the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all ai,j zeros).

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤16) — the number of test cases. Then t test cases follow.

The test case consists of two lines, each of these lines contains two integers. The j-th integer in the i-th row is ai,j. If ai,j=0 then the cell (i,j) is empty, and if ai,j=1 the cell (i,j) contains grass.

Output
For each test case, print one integer — the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all ai,j zeros) in the corresponding test case.
}
var a:array[1..2,1..2] of integer;
n,count_r1,count_r2,i: integer;
begin
    readln(n);
    count_r1 := 0;
    count_r2 := 0;
    for i := 1 to n do
    begin
    read(a[1,1]);
    if a[1,1] = 1 then inc(count_r1);
    readln(a[1,2]);
    if a[1,2] = 1 then inc(count_r1);
    read(a[2,1]);
    if a[2,1] = 1 then inc(count_r2);
    readln(a[2,2]);
    if a[2,2] = 1 then inc(count_r2);
    if count_r1 + count_r2 = 0 then writeln(0) else
        if (count_r1 + count_r2 >= 1) and (count_r1 + count_r2 < 4) then writeln(1) else writeln(2);

    end;
    readln
end.

{
    There is a field of size 2×2. Each cell of this field can either contain grass or be empty. The value ai,j is 1 if the cell (i,j) contains grass, or 0 otherwise.

In one move, you can choose one row and one column and cut all the grass in this row and this column. In other words, you choose the row x and the column y, then you cut the grass in all cells ax,i and all cells ai,y for all i from 1 to 2. After you cut the grass from a cell, it becomes empty (i. e. its value is replaced by 0).

Your task is to find the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all ai,j zeros).

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤16) — the number of test cases. Then t test cases follow.

The test case consists of two lines, each of these lines contains two integers. The j-th integer in the i-th row is ai,j. If ai,j=0 then the cell (i,j) is empty, and if ai,j=1 the cell (i,j) contains grass.

Output
For each test case, print one integer — the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all ai,j zeros) in the corresponding test case.
}
var a:array[1..2,1..2] of integer;
n,count_r1,count_r2,i: integer;
begin
    readln(n);
    count_r1 := 0;
    count_r2 := 0;
    for i := 1 to n do
    begin
    read(a[1,1]);
    if a[1,1] = 1 then inc(count_r1);
    readln(a[1,2]);
    if a[1,2] = 1 then inc(count_r1);
    read(a[2,1]);
    if a[2,1] = 1 then inc(count_r2);
    readln(a[2,2]);
    if a[2,2] = 1 then inc(count_r2);
    if count_r1 + count_r2 = 0 then writeln(0);
    if count_r1 + count_r2 = 1 then writeln(1);
    if count_r1 + count_r2 = 2 then writeln(1);
    if count_r1 + count_r2 = 3 then writeln(1);
    if count_r1 + count_r2 = 4 then writeln(2);
    end;
    readln
end.

{
    There is a field of size 2×2. Each cell of this field can either contain grass or be empty. The value ai,j is 1 if the cell (i,j) contains grass, or 0 otherwise.

In one move, you can choose one row and one column and cut all the grass in this row and this column. In other words, you choose the row x and the column y, then you cut the grass in all cells ax,i and all cells ai,y for all i from 1 to 2. After you cut the grass from a cell, it becomes empty (i. e. its value is replaced by 0).

Your task is to find the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all ai,j zeros).

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤16) — the number of test cases. Then t test cases follow.

The test case consists of two lines, each of these lines contains two integers. The j-th integer in the i-th row is ai,j. If ai,j=0 then the cell (i,j) is empty, and if ai,j=1 the cell (i,j) contains grass.

Output
For each test case, print one integer — the minimum number of moves required to cut the grass in all non-empty cells of the field (i. e. make all ai,j zeros) in the corresponding test case.
}
var a:array[1..2,1..2] of integer;
n,count_r1,count_r2,i: integer;
begin
    readln(n);
    count_r1 := 0;
    count_r2 := 0;
    for i := 1 to n do
    begin
    read(a[1,1]);
    if a[1,1] = 1 then inc(count_r1);
    readln(a[1,2]);
    if a[1,2] = 1 then inc(count_r1);
    read(a[2,1]);
    if a[2,1] = 1 then inc(count_r2);
    readln(a[2,2]);
    if a[2,2] = 1 then inc(count_r2);
    if count_r1 + count_r2 = 0 then writeln(0) else
    if (count_r1 + count_r2 >= 1) and (count_r1 + count_r2<= 3) then writeln(1) else writeln(2);
    end;
    readln
end.

program polyana2;
 var b,c,t,i,j,d:integer;
 begin
  readln(t);
   for c:=1 to t do begin
   b:= 0;
    for d:= 1 to 2 do begin
      readln(i,j);
      b:= b+i+j;
                           end; // for d:= 1 to 4 do end

    Writeln(b div 2);

    end; // for c:= 1 to t end

    end.

program polyana2;
 var b,c,t,i,j,d:integer;
 begin
  readln(t);
   for c:=1 to t do begin
    for d:= 1 to 2 do begin
      readln(i,j);
      b:= b+i+j;
                           end; // for d:= 1 to 4 do end

    Writeln(b div 2);

    end; // for c:= 1 to t end

    end.

var t,j,a,b,dem:longint;
begin
    readln(t);
    for j:=1 to t do
        begin
            dem:=0;
            readln(a,b);
            if a=1 then inc(dem);
            if b=1 then inc(dem);
            readln(a,b);
            if a=1 then inc(dem);
            if b=1 then inc(dem);
            if dem>=3 then writeln(2) else if dem>=1 then writeln(1) else writeln(0);
        end;
end.

var t,j,a,b,dem:longint;
begin
    readln(t);
    for j:=1 to t do
        begin
            dem:=0;
            readln(a,b);
            if a=1 then inc(dem);
            if b=1 then inc(dem);
            readln(a,b);
            if a=1 then inc(dem);
            if b=1 then inc(dem);
            writeln(dem);
            if dem>=3 then writeln(2) else if dem>=1 then writeln(1) else writeln(0);
        end;
end.

var i,t,a,b,c,d,dem:longint;
        kq:array[1..16] of longint;
begin
        readln(t);
        for i := 1 to t do
        begin
                readln(a,b);
                readln(c,d);
                if a=1 then inc(dem);
                if b=1 then inc(dem);
                if c=1 then inc(dem);
                if d=1 then inc(dem);
                if dem = 0 then kq[i]:=0 else
                if (dem = 1) or (dem =2) or (dem=3) then kq[i]:=1 else kq[i]:=2;
        end;
        for i:=1 to t do writeln(kq[i]);
end.

var i,t,a,b,c,d,dem:longint;
begin
        readln(t);
        for i := 1 to t do
        begin
                readln(a,b);
                readln(c,d);
                if a=1 then inc(dem);
                if b=1 then inc(dem);
                if c=1 then inc(dem);
                if d=1 then inc(dem);
                if dem = 0 then writeln('0') else
                if (dem = 1) or (dem =2) or (dem=3) then writeln('1') else writeln('2');
        end;
end.

var i,t,a,b,c,d,dem:longint;
begin
        readln(t);
        for i := 1 to t do
        begin
                readln(a,b);
                readln(c,d);
                if a=1 then inc(dem);
                if b=1 then inc(dem);
                if c=1 then inc(dem);
                if d=1 then inc(dem);
                if dem = 0 then writeln('0') else
                if (dem = 1) or (dem =2) then writeln('1') else writeln('2');
        end;
end.

program grass_field;

  var
       count,a,b,c,d,t:byte;
       Ar:array[1..99] of byte;
   begin

        readln(t);
        if (t >= 1) and (t <= 16) then
         begin
          while (t<>0) do
             begin
                count:=1;
                read(a);
                readln(b);
                read(c);
                readln(d);
                  if (a=0) and (b=0) and (c=0) and (d=0) then  Ar[count]:=0
                  else if (a=1) and (b=1) and (c=1) and (d=1) then Ar[count]:=2
                  else Ar[count]:=1;
                count:=count+1;
                t:=t-1;
             end;
          for t:= 1 to count do writeln(Ar[t]);
          end;
   end.

program grass_field;

  var
       a,b,c,d,t:byte;

   begin

        readln(t);
        if (t >= 1) and (t <= 16) then
         begin
          while (t<>0) do
             begin
                read(a);
                readln(b);
                read(c);
                readln(d);
                  if (a=0) and (b=0) and (c=0) and (d=0) then  write(0)
                  else if (a=1) and (b=1) and (c=1) and (d=1) then write(2)
                  else writeln(1);
                t:=t-1;
             end;
          end;
     readln;
   end.


 var n,i,j:LongInt;
Begin
  Read(n);
  for i:=0 to n div 1234567 do
    begin
      for j:=0 to (n-i*1234567) div 123456 do
        begin
          if(n-i*1234567-j*123456)mod 1234=0 then
            begin
              Write('YES');
              halt;
            end;
        end;
    end;
  Write('NO');
End.

var
  a,b,ans,c,n,k:int64;
  i,j:longint;
function min(a,b:longint):longint;
begin
  if a<=b then min:=a else min:=b;
end;
begin
  read(n);

  for i:=0 to n div 1234567 do
    for j:=0 to n div 123456 do
    if (n-i*1234567-j*123456>=0) and ((n-i*1234567-j*123456)mod 1234=0) then begin writeln('YES');halt;end;
    writeln('NO');
end.

Uses math;
Const
        fin='';
        fon='';
        maxC=1000000000;
        maxN=1000;
	module=1000000007;

Type
	re=record
		x,y:longint;
		end;

Var
        fi,fo:text;
        n:int64;

Procedure mo;
Begin
        Assign(fi,fin); Reset(fi);
        Assign(fo,fon); Rewrite(fo);
End;

Procedure dong;
Begin
        Close(fi);
        Close(fo);
End;

Procedure nhap;
Begin
        Readln(fi,n);
End;

Procedure xuly;
Var
        i,j,o,n1,n2,n3:longint;
        tg,tg1,tg2:int64;
Begin
        n1:=n div 1234567;
        n2:=n div 123456;
        n3:=n div 1234;
        For i:=0 to n1 do
        Begin
                tg:=i*1234567;
                For j:=0 to n2 do
                Begin
                        tg1:=n-(tg+j*123456);
                        If tg1<0 then break;
                        If tg1 mod 1234=0 then
                        Begin
                                Writeln(fo,'YES');
                                Dong;
                                Halt;
                        End;
                End;
        End;
        Writeln(fo,'NO');
End;

BEGIN
        Mo;
        Nhap;
        Xuly;
        Dong;
END.

const
    fi ='';
    fg ='';
var a,b,c,n:longint;
    f,g:text;
function calc(n:longint):string;
var x,i,j:longint;
begin
    For i:=0 to 810 do
        For j:=0 to 810 do
        begin
            x :=n - a*i - b*j;
            If (x >= 0) and (x mod c = 0) then exit('YES');
        end;
    exit('NO');
end;
begin
    assign(f,fi);
    reset(f);
    assign(g,fg);
    rewrite(g);
    readln(f,n);
    a :=1234567;b :=123456;c :=1234;
    writeln(g,calc(n));
    close(f);
    close(g);
end.

{$MODE OBJFPC}
var   n,i,j:integer;
begin
     readln(n);
     for i:=0 to n div 1234567 do
         for j:=0 to (n-1234567*i) div 123456 do
             if (n-1234567*i-123456*j) mod 1234=0 then
                begin
                   writeln('YES');
                   halt
                end;
     writeln('NO')
end.

var n,i,j:longint;begin read(n);for i:=0to n div 1234567do for j:=0to(n-i*1234567)div 123456do if(n-i*1234567-j*123456)mod 1234=0then begin write('YES');halt;end;write('NO')end.

var n,i,j:longint;begin read(n);for i:=0to n div 1234567do for j:=0to(n-i*1234567)div 123456 do if(n-i*1234567-j*123456)mod 1234=0then begin write('YES');halt;end;write('NO')end.

var
n,i,j:longint;
bol:boolean;
begin
bol:=false;
readln(n);
for i:=0 to n div 1234567 do begin
for j:=0 to n div 123456 do begin
    if (n-(1234567*i+123456*j))>=0 then begin
        if (n-(1234567*i+123456*j)) mod 1234=0 then begin
            bol:=true;
            break;
        end;
        if bol then break;
    end;
    if bol then break;
end;
if bol then break;
end;
if bol then writeln('YES')
else
writeln('NO');
end.

var
 a,b,c,i,j,k,m,n,o:longint;
begin
 readln(n);
 for i:=0 to (n div 1234567)do
 for j:=0 to (n div 123456) do
 begin
  k:=n-i*1234567-j*123456;
  if k<0 then continue;
  if (k mod 1234)=0 then begin writeln('YES'); halt; end;
 end;
 writeln('NO');
end.

uses math;
var i,j:longint;
   pom,pom1,n:int64;
 begin
 read(n);

 for i:=0 to 1000 do
 for j:=0 to 10000 do
  begin
   pom:=i;
   pom1:=j;
   if (n>=pom*1234567+pom1*123456) and ((n-pom*1234567-pom1*123456) mod 1234=0) then
    begin
     writeln('YES');
     exit;
   end;
  end;
 writeln('NO');
end.

const fi = '';

var  n, a, b, house, car, computer: longint;
     res: boolean;
     f: text;

begin
     assign(f, fi); reset(f);
     readln(f, n);
     close(f);

     res:= false; house:= 1234567; car:= 123456; computer:= 1234;
     for a:= 0 to n div house do
        for b:= 0 to (n - a * house) div car do
           begin
              if (n - a * house - b * car) mod computer = 0 then
                 begin
                    res:= true;
                    break;
                 end;
           end;

     if res then writeln('YES') else writeln('NO');
     readln;
end.

program O_O;
type
        ll=longint;
        bo=boolean;
var
        i,j,k,n,m,a,b,c:ll;
begin
        readln(n);
      //  if n<1234567+123456+1234 then begin writeln('NO');exit;end;
        for a:=0 to 820 do
        for b:=0 to 8200 do
        begin
         c:=n-a*1234567-b*123456;
         if (c mod 1234=0)and(c>=0) then
         begin
                writeln('YES');
                exit;
         end;
        end;
         writeln('NO');
end.

var      a,b,n      :longint;

begin
        read(n);
        for a:=0 to n div 1234567 do
        begin
                for b:=0 to (n-a*1234567) div 123456 do
                        if (n-a*1234567-b*123456) mod 1234=0 then
                        begin
                                write('YES');
                                halt;
                        end;
        end;
        write('NO');
end.

var
 n,i,j:longint;
begin
 readln(n);
 for i:=0 to n div 1234567 do
  for j:=0 to n div 123456 do if i*1234567+j*123456<=n then
  begin
   if (n-i*1234567-j*123456) mod 1234=0 then
   begin
    writeln('YES');
    halt;
   end;
  end;
 writeln('NO');
end.

var n, i, j: longint;
    ans: boolean;

begin
    read(n);
    ans := False;
    for i := 0 to (n div 1234567) do
        for j := 0 to ((n - 1234567 * i) div 123456) do
        if (n - 1234567 * i - 123456 * j) mod 1234 = 0 then ans := True;
    if ans then write('YES') else write('NO');
end.

var
  n:int64;
  a,b:longint;
begin
  readln(n);
  for a:=0 to 100 do
    for b:=0 to 1000 do
      if n>=a*1234567+b*123456 then
        if (n-a*1234567-b*123456) mod 1234=0 then
          begin
            writeln('YES');
            exit;
          end;
  writeln('NO');
end.

const
   fin='681B.inp';fon='681B.out';
var i,j,k,n:longint;
procedure mo;
begin
assign(input,fin);reset(input);
assign(output,fon);rewrite(output);
end;

procedure solve;
begin
readln(n);
for i:=0 to n div 1234567 do
   for j:=0 to (n-1234567*i) div 123456 do
      if (n-1234567*i-123456*j) mod 1234 = 0 then
         begin
         writeln('YES');
         halt;
         end;
writeln('NO');
end;

begin
//mo;
solve;
end.

Var
n,a,b,c,d,i,j,k,l,f,n1,f1,f2:longint;
begin
 read(n); n1:=n;
 k:= n div 1234567;
 l:= n div 123456;
 f:=n div 1234;
 if f=0 then
  write('NO')
 else
 if n mod 1234=0 then
  write('YES')
 else
  if n mod 123456=0 then
   write('YES')
 else
  if n mod 1234567=0 then
   write('YES')
 else
 if l=0 then
  write('NO')
 else
 if k=0 then
  for j:=1 to l do
   begin
    if n1 mod 1234=0 then
     f1:=1;
    f2:=1;
    n1:=n1-123456;
    if n1 mod 1234=0 then
     f1:=1;
   end
 else

 begin
   for i:=1 to k do
    begin
     n1:=n; n1:=n1-i*1234567;
    for j:=1 to (n1 div 123456) do
     begin
      if n1 mod 1234=0 then
       f1:=1;
      f2:=1;
      n1:=n1-123456;
      if n1 mod 1234=0 then
       f1:=1;
     end;
    end;
       for j:=1 to l do
     begin
      if n mod 1234=0 then
       f1:=1;
      f2:=1;
      n:=n-123456;
      if n mod 1234=0 then
       f1:=1;
     end;
 end;
if (k=1) and (n1 div 123456=0) then
 if n1 mod 1234=0 then
  write('YES')
 else
 if f1=1 then
  write('YES')
 else
  write('NO')
else
if f2=1 then
 if f1=1 then
  write('YES')
 else
  write('NO');
end.

Const
        car=123456;
        house=1234567;
        cpt=1234;
        tfi='';
        tfo='';
Var
        a,b:array[0..10000] of longint;
        n,n1,n2:longint;
        fi,fo:text;
PRocedure       Data;
Begin
        Assign(fi,tfi);reset(fi);
        Assign(fo,tfo);rewrite(fo);
        readln(fi,n);
ENd;
Procedure       Solve;
Var     res:boolean;
        i,j:longint;
Begin
        res:= false ;
        If (n mod car =0) or (n mod house=0) or (n mod cpt=0) then res:=true;
        n1:=n div car;n2:=n div house;
        For i:=1 to n1 do a[i]:=car*i;
        For i:=1 to n2 do b[i]:=house*i;
        FOr i:=0 to n1 do
          For j:=0 to n2 do
            If b[j]+a[i]<=n then
              Begin
                If (n-(b[j]+a[i])) mod cpt=0 then res:=true;
              end
            else break;
        If res=true then writeln(fo,'YES')
        Else Writeln(fo,'NO');
        Close(fi);Close(fo);
ENd;
Begin
        data;
        solve;
End.

const
   fin='';fon='';
   t=1234567;
   t2=123456;
   t3=1234;
   maxn=trunc(1e9);
var
   n:longint;
   c:int64;
procedure nhap;
   begin
   assigN(input,fin);reset(input);
   assigN(output,fon);rewritE(output);
   readln(n);
   end;
procedure xuli;
   var
      i,j,k:longint;
      top1,top2,top3:int64;
   begin
   top1:=maxn div t;
   top2:=maxn div t2;
   top3:=maxn div t3;
   for i:=0 to top1 do
      for j:=0 to top2 do
         begin
         if n>=(t*i)+(t2*j) then
         begin
         c:=n-(t*i+t2*j);
         if (c mod t3=0) and (c>=0) then
            begin
            writeln('YES');
            //writeln(i,' ',j,' ',(c div t3));
            exit;
            end;
         end;
         end;
   writeln('NO');
   close(input);
   close(output);
   end;
begin
nhap;
xuli;
end.

var c:real;
    x:int64;
    i,j,n:longint;

procedure xuli;
    begin
        readln(n);
        For i:=0 to 810 do
            For j:=0 to 8100 do
                begin
                    x:=i*1234567+j*123456;
                    x:=n-x;
                    if x>=0 then
                        begin
                            c:=x/1234;
                            if trunc(c)=c then
                                begin
                                writeln('YES');
                                halt;
                                end;
                        end;
                end;
        writeln('NO');
    end;

begin
//assign(input,'357B.inp');reset(input);
//assign(output,'357B.out');rewrite(output);
xuli;
end.

  var N : LongInt;
      A , B : LongInt;
      sum : Int64;
begin
  readLn ( N ) ;

  for A := 0 to 100 do
  begin
    for B := 0 to 1000 do
    begin
      sum := (A * 1234567) + ( B * 123456 );

      if ( N >= sum ) then

      begin

        if ( (N - sum) mod 1234 = 0 ) then
        begin
          writeLn ( 'YES' ) ;
          halt();
        end;

      end;

    end;
  end;

  writeLn ( 'NO' ) ;

end.

var
  ans:boolean;
  n,i,j,m,m1,m2:longint;
{
procedure dfs(x,z:longint);
begin
  if (x mod 1234567=0)or(x mod 123456=0)or(x mod 1234=0)
    then begin
           writeln('YES');
           halt;
         end;
  if (x>1234567)and(z<>1)
    then dfs(x-1234567,1);
  if (x>123456)and(z<>2)
    then dfs(x-123456,2);
  if (x>1234)and(z<>3)
    then dfs(x-1234,3);
end;
}
begin
  m1:=811;
  m2:=8101;
  readln(n);
  for i:=0 to m1 do
    begin
      for j:=0 to m2 do
        begin
          m:=n-(i*1234567+j*123456);
          if m<0
            then break;
          if (m mod 1234=0)
            then begin
                   writeln('YES');
                   halt;
                 end;
         end;
    end;
  writeln('NO');
end.

Const Fi='';
      Fo='';
Var f:text;
    n:longint;
    a,b,c:longint;
Procedure docfile;
         Begin
              Assign(f,fi);Reset(f);
              Readln(f,n);
              Close(f);
         End;
Function kt:boolean;
        Begin
             For a:=0 to n div 1234567 do
                For b:=0 to n div 123456 do
                   If (n-a*1234567 -b*123456) >=0 then
                      If (n-a*1234567 -b*123456) mod 1234 =0 then exit(true);
             exit(false);
        End;
Procedure xuly;
         Begin
              Assign(f,fo);
              Rewrite(f);
              If kt=true then writeln(f,'YES') else writeln(f,'NO');
              Close(f);
         End;
Begin
     docfile;
     xuly;
End.

var n,i,j:longint;
begin
readln(n);
for i:=0 to n div 1234567 do
  for j:=0 to (n-i*1234567) div 123456 do
    if (n-i*1234567-j*123456) mod 1234=0 then
      begin writeln('YES'); halt; end;
writeln('NO');
end.

var
	x,y,z,tt,ttt,n,k,i,j:longint;
	boo:boolean;
begin
	readln(n);
	x:=1234567;
	y:=123456;
	z:=1234;
	boo:=false;
	for k:= (n div x) downto 0 do
	begin
		tt:=n-(k*x);
		for i:= (tt div y) downto 0 do
		begin
			ttt:= tt-(i*y);
			if (ttt mod z)=0 then boo:=true;
		end;
	end;
	if boo then writeln('YES') else writeln('NO');
end.

var
  n, i, j, f : longint;
begin
  f := 0;
  readln(n);
  for i := 0 to n div 1234567 do
    for j := 0 to n div 123456 do
      if (n - (i * 1234567 + j * 123456) >= 0) and
        ((n - (i * 1234567 + j * 123456)) mod 1234 = 0) then f := 1;
  if f = 1 then writeln('YES')
  else writeln('NO');
end.

var n,i,x,y,k,p,j,z:longint;
begin
{assign(input,'input.txt');
assign(output,'output.txt');
reset(input);
rewrite(output);  }
readln(n);
x:=1234567;
y:=123456;
z:=1234;
for i:=0 to 100 do
   for j:=0 to 1000 do
     begin
      p:=n-(i*x+j*y);
      if (p mod z=0) and (p>=0) then begin writeln('YES');
                                  halt; end;
     end;
writeln('NO');
{close(input);
close(output); }
end.

uses math;
const
	fin='';
	fon='';
	maxC=1000000008;
	maxN=108;

Procedure Openf;
begin
	assign(input,fin); reset(input);
	assign(output,fon); rewrite(output);
end;

Procedure Closef;
begin
	close(input);
	close(output);
End;

Procedure Nhap;
var
        i,j,n,a,b,c,tg,x,y:longint;
Begin
        readln(n);
        tg:=n;
        a:=1234567;
        b:=123456;
        c:=1234;
        i:=0;
        j:=0;

        while i*a<=n do
        begin
                while n-j*b-i*a>=0 do
                begin
                        tg:=n-j*b-i*a;
                        if tg mod c=0 then
                        begin
                                writeln('YES');
                                halt;
                        end;
                        inc(j);
                end;
                inc(i);
                j:=0;
        end;
        writeln('NO');
end;

Procedure xuli;
begin
end;

Procedure Xuat;
begin
end;

BEGIN
	Openf;
	Nhap;
	Xuli;
	Xuat;
	Closef;
END.

program BB;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
x,y,z,i,j,n,ni,nj:longint;
flag:boolean;
begin
 readln(n);
 x:=1234567;
 y:=123456;
 z:=1234;
 flag:=false;
 for i:=0 to n div x do
   begin
     ni:=n-i*x;
     for j:=0 to ni div y do
       begin
         nj:=ni-j*y;
         if (nj mod z=0) then begin flag:=true; end;
       end;
   end;
 if (flag=true) then writeln('YES') else writeln('NO');
end.

var s:string;q:char;i,n,a,b:longint;
begin
 readln(n);
 for i:=0 to n div 1234567 do
 begin
 b:=n-i*1234567;
 for a:=0 to b DIV 123456 do

    if (b-a*123456) MOD  1234 =0then
  begin write('YES')
   ;halt;
  end;
 end;write('NO')
end.

program u;
var n,i,j,k,a,b:longint; m:array[0..37,-37..37]of int64; c:char;
begin
 { assign(input,'input.txt'); reset(input);
  assign(output,'output.txt'); rewrite(output);
  }read(n);
  for i:=0 to n div 1234567 do
  for j:=0 to (n-i*1234567) div 123456 do
  if (n-i*1234567-j*123456) mod 1234=0
  then k:=1;
  if k=1
  then write('YES')
  else write('NO');
end.

var
  a, b, c, i, j, t: int64;
  q: boolean;

begin
  readln(a);
  i := 0;
  q := false;
  while i * 1234567 <= a do begin
    j := 0;
    while j * 123456 + i * 1234567 <= a do begin
      t := (a - j * 123456 - i * 1234567) div 1234;
      if j * 123456 + i * 1234567 + t * 1234 = a then q := true;
      inc(j);
    end;
    inc(i);
  end;
  if q then writeln('YES')
    else writeln('NO');
end.

var
a,b,c,n1,n,n2,i,j:longint;
kt:boolean;

begin
//assign(input,'681b.inp');reset(input);
//assign(output,'681b.out');rewrite(output);
readln(n);
while a*1234567<=n do
        inc(a);
dec(a);
for i:=0 to a do
        begin
        n1:=n-i*1234567;
        b:=0;
        while b*123456<=n1 do
                inc(b);
        dec(b);
        for j:=0 to b do
                begin
                n2:=n1-j*123456;
                if n2 mod 1234=0 then
                        begin
                        writeln('YES');
                        halt;
                        end;
                end;
        end;
writeln('NO');
end.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  x, y, z, w, a, b, v : integer;
  k : longint;
begin
  read(w);
  x:=1234;
  y:=123456;
  z:=1234567;
  k:=0;
  for a:=0 to w div x do
    for b:=0 to w div y do
    begin
      v:=w-a*x-b*y;
      if (v>=0) and (v mod z=0) then begin
         writeln('YES');
         exit;
      end;
    end;
  write('NO');
end.

var
   i,casos,c,n:longint;
   a,b,x,y:int64;
   res:extended;
begin
    readln(n);
    while a*1234567<=n do
    begin
      b:=0;
    while a*1234567+b*123456<=n do
    begin
        c:=n-a*1234567-b*123456;
        if (c>=0) And (c mod 1234=0) then
        begin
          writeln('YES');
          halt;
        end;
       b:=b+1;
    end;
      a:=a+1;
    end;
    writeln('NO');
    i:=i;
end.

var
   a,b,c,n,m:int64;
   lim:longint;
begin
   lim:=1000000000;
   readln(n);
   while a*1234567<=n do
   begin
      b:=0;
      while a*1234567+b*123456<=n do
      begin
          m:=n-(a*1234567+b*123456);
          if (m>=0) AND (m mod 1234=0) then
          begin
              writeln('YES');
              halt;
          end;
          b:=b+1;
      end;
      a:=a+1;
   end;
   writeln('NO');
end.

var a,b,n,x,f,kk,aa,otv,i1,i2,i3,d:longint;
begin
readlN(n);
for i1:=0 to 1000 do
for i2:=0 to 1000 do
begin
d:=n-(i1*1234567+i2*123456);
if (d>=0)and(d mod 1234=0) then begin writelN('YES');halt;end;
end;
writelN('NO');
end.

const fi = '';
      fo = '';
var a, b, c, n: longint;
begin
  assign(input,fi); reset(input);
  assign(output,fo); rewrite(output);

  readln(n);
  for a:=0 to n div 1234567 do
    for b:=0 to (n-a*1234567) div 123456 do
      if (n-a*1234567-b*123456) mod 1234 = 0 then
          begin
            writeln('YES');
            halt;
          end;
  writeln('NO');

  close(input); close(output);
end.

var n,i,j,x,y,z:longint;
procedure nhap;
   begin

   readln(n);

   x:=1234567; y:=123456; z:=1234;

   end;

procedure xuli;
   begin

   for i:=0 to 100 do
      for j:=1000 downto 0 do
         if n-i*x-j*y>=0 then
            if (n-i*x-j*y) mod z=0 then
               begin

               writeln('YES');
               halt;

               end;

   writeln('NO');

   end;

begin
nhap;
xuli;
end.

Const Finp='';
      Fout='';
      res:array[0..1] of string=('YES','NO');
      numa=1234567;
      numb=123456;
      numc=1234;
Var n:int64;

Procedure Nhap;
Var f:Text;
Begin
    assign(f,finp); reset(f);
    read(f,n);
    close(f);
End;
Function Solve:longint;
Var i,j,a,b,maxa,maxb:longint;
Begin
    maxa:=n div numa; maxb:=n div numb;
    for a:=0 to maxa do
        for b:=0 to maxb do
            if (a*numa+b*numb)<=n then
                if (n-(a*numa+b*numb)) mod numc=0 then
                    exit(0);
    exit(1);
End;
Procedure Xuat;
Var f:Text;
    tmp:longint;
Begin
    assign(f,fout); rewrite(f);
    tmp:=solve;
    write(f,res[tmp]);
    close(f);
End;

Begin
    nhap;
    xuat;
End.

Program B337;
CONST FINP = '1.INP';	FOUT = '1.OUT';
VAR fi, fo: text;
n, a1, b1: int64;
a, b: longint;

Procedure Process;
BEGIN
readln(fi, n);
for a := 0 to n div 1234567 do
	begin
	a1 := n - a*1234567;
	for b := 0 to a1 div 123456 do
		begin
		b1 := a1 - b*123456;
		if b1 mod 1234 = 0 then
			begin
			writeln(fo, 'YES');
			exit;
			end;
		end;
	end;
writeln(fo, 'NO');
END;

BEGIN
{$IFDEF ONLINE_JUDGE}
assign(fi, '');		assign(fo, '');
{$ELSE}
assign(fi, FINP);	assign(fo, FOUT);
{$ENDIF}
reset(fi);			rewrite(fo);
Process;
close(fi);
close(fo);
END.

var
 os,r,t,y,a,b,c,n,i,j,q:longint;
 const h:longint=1234567;
 const bu:longint=1234;
 const k:longint=123456;
begin
 readln(N);
 for a:=0 to 810 do
 begin
  for b:=0 to 8100 do

   if ((n-((a*h)+(b*k))) mod bu=0)and( (n-( (a*h) + (b*k) ) )>=0 ) then
   begin

    writeln('YES');
    halt;
   end;
 end;
 writeln('NO');

end.

   const eps=0.000000000001;  besk1=8000000000000000000;  nn=200000;
   modul=1000000007;
   label 1,2;
    type
    mas=array [0..nn] of longint;   point=record x,y,z:real;end;
    zap=record p1,p2,p3 :int64;end; zap1=^zap;
    mass=array[0..nn] of longint;mass1=^mass;
    var ii,jj,n,qq,xx,q,pos,left,right,amax,ost,h,tt,t1,pp,p,aa,bb,cc,kk,dist,pmax,imax,p1,p2,p3,p4,t,l,ot,nm,s1,s2,m,s,cen,t2,i1,besk:int64;
    mask,newug,ugol,newdl,dloch,dugol,ist,jst,ifin,jfin,newnapx,newnapy,napx,napy,step, k,i,j:longint;
     prmax,v,aaa,bbb,ccc:extended; hhh:zap;
     dl,ochnewi,ochnewj,xxx,yyy,ochi,ochj,c,y,z,newoch,och,x,aold,b,a:array[-100000..2000000] of int64;
   stpr,stoim,blup,bldown, blright, blleft, ppole,vol:array[-10..1200,-10..1200] of int64;
   marked:array[-10..1100,-10..1100] of boolean;
    sss, ss1,ss2:string;
    sa,sb,pra,prb,suma,sumb,ota,otb,mmax,mmin,smax,smin:array[-100..200000] of extended;
     fl:array[0..200] of boolean;
    ss: array[-10..100000] of string;ch3,ch,ch1,ch2:char;
    razl:array[0..300000,0..18] of integer;
    pr:array[-10..300,-10..300] of extended;
  ddmax,ddmin :array[-1..300000,-1..20] of extended;
    pole :array [-10..2000] of char;
    maxpr:array[0..nn] of string;
    str:array[0..nn] of string;
     tt1,tt2,disk,v0,v1,s0,t0:extended;
     zv:point;
     newx,newy,yy:array[-1..200000] of int64;

     yot:double;
       flag,flag1:boolean;
    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 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 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 div 2); degg:=p*p mod modul;   end;

end;

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

readln(n);
   for i:=0 to 1000  do for j:=0 to 10000 do

     begin
       ii:=i; jj:=j;
        t:=n-ii*1234567-jj*123456;
        if (t>=0) and(t mod 1234=0) then flag:=true;

     end;

    if  flag then write('YES') else write('NO');

   1:          close(output);
end.

var a,b,c,f,n,i,j,l,m:longint;  p:array [1..1000000] of longint;
begin
  {assign (input,'input.txt'); reset (input);
  assign (output,'output.txt'); rewrite (output);  }
  readln (n);
  f:=0;
  a:=1234;
  b:=123456;
  c:=1234567;
  l:=1;
  if n mod a=0 then f:=1;
  if n mod b=0 then f:=1;
  if n mod c=0 then f:=1;
  if f=0 then
   begin
     i:=1;
     p[l]:=n;
     l:=l+1;
     if n>c then
begin
     while (f=0) and (i<=(n div c)) do
      begin
        m:=n-(c*i);
        if m mod a=0 then f:=1;
        if m mod b=0 then f:=1;
        p[l]:=m;
        l:=l+1;
        i:=i+1;
      end;
end;
     l:=l-1;
     if f=0 then
      begin
        i:=1;
        while (f=0) and (i<=l) do
         begin
           j:=1;
           while (f=0) and (j<=(p[i] div b)) do
            begin
              m:=p[i]-b*j;
              if m mod a=0 then f:=1;
              j:=j+1;
            end;
           i:=i+1;
         end;
      end;
   end;
  //if f=1 then writeln ('YES',' ',m,' ',l,' ',i-1,' ',p[i-1]) else writeln ('NO');
  if f=1 then write ('YES') else write ('NO');
end.

program yan;
  var a,b,c,n:longint; z:int64;
begin
  readln(n);
  for a:=0 to 1000000 do
    begin
      z:=a*1234567;
      if z>n then break;
      for b:=0 to 1000000 do
        begin
          z:=a*1234567+b*123456;
          if z>n then break;
          if (n-z)mod 1234=0 then
            begin
              write('YES');
              exit;
            end;
        end;
    end;
  write('NO');
end.

var
  n,i,j,k,tt,t : int64;

  function fnd(x : int64) : int64;
    var
      l,r,mid : int64;
    begin
      l:=1; r:=815000;
      while l < r do begin
        mid:=(l+r) div 2;
        if x <= mid*1234 then r:=mid else l:=mid+1;
      end;
      if l*1234 <= x then exit(l) else exit(0);
    end;

begin
  readln(n);
  i:=0;
  while i <= 820 do begin
    if n-1234567*i < 0 then break;
    t:=n-1234567*i;
    if t = 0 then begin writeln('YES'); halt; end;
    j:=0;
    while j <= 8105 do begin
      if t-j*123456 < 0 then break;
      tt:=t-j*123456;
      if tt = 0 then begin writeln('YES'); halt; end;
      k:=fnd(tt);
      if (tt-k*1234 = 0) then begin writeln('YES'); halt; end;
      inc(j);
    end;
    inc(i);
  end;
  writeln('NO');
end.

Var f,g:Text;
    N,i,j:longint;
 Begin
     Readln(N);
     For i:=0 to (N div 1234567) do
      For j:=0 to (N-i*1234567) div 123456 do
       Begin
           If (N-i*1234567-j*123456) mod 1234 = 0 then
            Begin
                Write('YES');
                Exit;
            End;
       End;
     Write('NO');
 End.

program z1;
const a = 1234567;
      b = 123456;
      c = 1234;
var d,i,j,k,l,m,n,p1,p2:longint;
begin
//assign(input,'d:\input.txt'); reset(input);
//assign(output,'d:\output.txt'); rewrite(output);
readln(n);
p1:=n div a;
for i:=0 to p1 do
 begin
 m:=n-a*i;
 p2:=m div b;
 for j:=0 to p2 do
  begin
  k:=m-j*b;
  if k mod c=0 then begin write('YES'); exit; end;
  end;
 end;
write('NO');
end.

var n: longint;
    a,b,c: longint;

begin
  readln(n);
  for a:=0 to n div 1234567 do
    for b:=0 to n div 123456 do
      if ((n-a*1234567-b*123456) mod 1234=0) and (n>=a*1234567+b*123456) then
        begin
          writeln('YES');
          halt;
        end;
  writeln('NO');
end.

var
     N,k1,k2,i,j:Longint;
     F:Boolean;
begin
     Readln(N);
     k1:=N div 1234567;
     F:=False;
     for i:=0 to k1 do
begin
     k2:=(N-i*1234567) div 123456;
     for j:=0 to k2 do
     if ((n-i*1234567-j*123456)>=0) and ((n-i*1234567-j*123456) mod 1234=0) then begin
                                                                                F:=True;
                                                                                Break;
                                                                                end;
     if F then Break;
 end;
     if F then Writeln('YES')
          else Writeln('NO');
 end.

var
  a,b,ans,c,n,k:int64;
  i,j:longint;
function min(a,b:longint):longint;
begin
  if a<=b then min:=a else min:=b;
end;
begin
  read(n);

  for i:=0 to n div 1234567 do
    for j:=0 to n div 123456 do
    if (n-i*1234567-j*123456>0) and ((n-i*1234567-j*123456)mod 1234=0) then begin writeln('YES');halt;end;
    writeln('NO');
end.

var
  a,b,ans,c,n,k:int64;
  i,j:longint;
function min(a,b:longint):longint;
begin
  if a<=b then min:=a else min:=b;
end;
begin
  read(n);

  for i:=1 to n div 1234567 do
    for j:=1 to n div 123456 do
    if (n-i*1234567-j*123456>0) and ((n-i*1234567-j*123456)mod 1234=0) then begin writeln('YES');halt;end;
end.

var
  a,b,i,n,k,j:longint;
begin
  read(n);
  a:=n div 1234567;
  n:=n mod 1234567;
  n:=n mod 123456;
  n:=n mod 1234;
  if n=0 then writeln('YES') else writeln('NO');
end.

var
  a,b,ans,c,n,k:int64;
  i,j:longint;
function min(a,b:longint):longint;
begin
  if a<=b then min:=a else min:=b;
end;
begin
  read(n);

  for i:=0 to 1000 do
    for j:=0 to 1000 do
    if (n-i*1234567-j*123456>0) and ((n-i*1234567-j*123456)mod 1234=0) then begin writeln('YES');halt;end;
    writeln('NO');
end.

var
  a,b,i,n,k,j:longint;
  s:string;
begin
  read(n);

  for i:=1 to 100 do
    for j:=1 to 1000 do
      for k:=1 to 1000 do
      if i*1234567+j*123456+k*1234=n then begin writeln('YES');halt;end;
      writeln('NO');
end.

var
  a,b,ans,c,n,k:int64;
  i,j:longint;
function min(a,b:longint):longint;
begin
  if a<=b then min:=a else min:=b;
end;
begin
  read(n);

  for i:=1 to n div 1234567 do
    for j:=1 to n div 123456 do
    if (n-i*1234567-j*123456>0) and ((n-i*1234567-j*123456)mod 1234=0) then begin writeln('YES');halt;end;
writeln('NO');
end.

var
  a,b,i,n,k,j:longint;
  s:string;
begin
  read(n);

  for i:=1 to 100 do
    for j:=1 to 100 do
      for k:=1 to 1000 do
      if i*1234567+j*123456+k*1234=n then begin writeln('YES');halt;end;
      writeln('NO');
end.

var n,i,j:LongInt;
Begin
  Read(n);
  for i:=1 to n div 1234567 do
    begin
      for j:=1 to (n-i*1234567) div 123456 do
        begin
          if(n-i*1234567-j*123456)mod 1234=0 then
            begin
              Write('YES');
              halt;
            end;
        end;
    end;
  Write('NO');
End.

program O_O;
type
        ll=longint;
        bo=boolean;
var
        i,j,k,n,m,a,b,c:ll;
begin
        readln(n);
        if n<1234567+123456+1234 then begin writeln('NO');exit;end;
        for a:=1 to 810 do
        for b:=1 to 8100 do
        begin
         c:=n-a*1234567-b*123456;
         if (c mod 1234=0)and(c>0) then
         begin
                writeln('YES');
                exit;
         end;
        end;
         writeln('NO');
end.

program O_O;
type
        ll=longint;
        bo=boolean;
var
        i,j,k,n,m,a,b,c:ll;
begin
        readln(n);
        if n<1234567+123456+1234 then begin writeln('NO');exit;end;
        for a:=1 to 820 do
        for b:=1 to 8200 do
        begin
         c:=n-a*1234567-b*123456;
         if (c mod 1234=0)and(c>=0) then
         begin
                writeln('YES');
                exit;
         end;
        end;
         writeln('NO');
end.

program O_O;
type
        ll=longint;
        bo=boolean;
var
        i,j,k,n,m,a,b,c:ll;
begin
        readln(n);
        if n<1234567+123456+1234 then begin writeln('NO');exit;end;
        for a:=1 to 820 do
        for b:=1 to 8200 do
        begin
         c:=n-a*1234567-b*123456;
         if (c mod 1234=0)and(c>0) then
         begin
                writeln('YES');
                exit;
         end;
        end;
         writeln('NO');
end.

var n, i, j: longint;
    ans: boolean;

begin
    read(n);
    ans := False;
    for i := 0 to (n div 1234567) do
        for j := 0 to ((n - 1234567 * i) mod 123456) do
        if (n - 1234567 * i - 123456 * j) mod 1234 = 0 then ans := True;
    if ans then write('YES') else write('NO');
end.

var
  n:int64;
  a,b:longint;
begin
  readln(n);
  for a:=1 to 1000 do
    for b:=1 to 1000 do
      if n>=a*1234567+b*123456 then
        if (n-a*1234567-b*123456) mod 1234=0 then
          begin
            writeln('YES');
            exit;
          end;
  writeln('NO');
end.

var
  n:int64;
  a,b:longint;
begin
  readln(n);
  for a:=1 to 1000 do
    for b:=1 to 10000 do
      if n>=a*1234567+b*123456 then
        if (n-a*1234567-b*123456) mod 1234=0 then
          begin
            writeln('YES');
            exit;
          end;
  writeln('NO');
end.

var
  n:int64;
  a,b:longint;
begin
  readln(n);
  for a:=1 to 100 do
    for b:=1 to 1000 do
      if n>a*1234567+b*123456 then
        if (n-a*1234567-b*123456) mod 1234=0 then
          begin
            writeln('YES');
            exit;
          end;
  writeln('NO');
end.

var
  n:int64;
  a,b:longint;
begin
  readln(n);
  for a:=1 to 1000 do
    for b:=1 to 1000 do
      if n>a*1234567+b*123456 then
        if (n-a*1234567-b*123456) mod 1234=0 then
          begin
            writeln('YES');
            exit;
          end;
  writeln('NO');
end.

Var
n,a,b,c,d,i,j,k,l,f,n1,f1,f2:longint;
begin
 read(n); n1:=n;
 k:= n div 1234567;
 l:= n div 123456;
 f:=n div 1234;
 if f=0 then
  write('NO')
 else
 if n mod 1234=0 then
  write('YES')
 else
  if n mod 123456=0 then
   write('YES')
 else
  if n mod 1234567=0 then
   write('YES')
 else
 if l=0 then
  write('NO')
 else
 if k=0 then
  for j:=1 to l do
   begin
    if n1 mod 1234=0 then
     f1:=1;
    f2:=1;
    n1:=n1-123456;
    if n1 mod 1234=0 then
     f1:=1;
   end
 else

 begin
   for i:=1 to k do
    begin
     n1:=n; n1:=n1-i*1234567;
    for j:=1 to (n1 div 123456) do
     begin
      if n1 mod 1234=0 then
       f1:=1;
      f2:=1;
      n1:=n1-123456;
      if n1 mod 1234=0 then
       f1:=1;
     end;
    end;
 end;
if (k=1) and (n1 div 123456=0) then
 if n1 mod 1234=0 then
  write('YES')
 else
  write('NO')
else
if f2=1 then
 if f1=1 then
  write('YES')
 else
  write('NO');
end.

Var
n,a,b,c,d,i,j,k,l,f,n1,f1,f2:longint;
begin
 read(n); n1:=n;
 k:= n div 1234567;
 l:= n div 123456;
 f:=n div 1234;
 if f=0 then
  write('NO')
 else
 if n mod 1234=0 then
  write('YES')
 else
  if n mod 123456=0 then
   write('YES')
 else
  if n mod 1234567=0 then
   write('YES')
 else
 if l=0 then
  write('NO')
 else
 if k=0 then
  for j:=1 to l do
   begin
    if n1 mod 1234=0 then
     f1:=1;
    f2:=1;
    n1:=n1-123456;
   end
 else

 begin
   for i:=1 to k do
    begin
     n1:=n; n1:=n1-i*1234567;
    for j:=1 to (n1 div 123456) do
     begin
      if n1 mod 1234=0 then
       f1:=1;
      f2:=1;
      n1:=n1-123456;
      if n1 mod 1234=0 then
       f1:=1;
     end;
    end;
 end;
if f2=1 then
 if f1=1 then
  write('YES')
 else
  write('NO');
end.

Var
n,a,b,c,d,i,j,k,l,f,n1,f1,f2:longint;
begin
 read(n); n1:=n;
 k:= n div 1234567;
 l:= n div 123456;
 f:=n div 1234;
 if f=0 then
  write('NO')
 else
 if n mod 1234=0 then
  write('YES')
 else
  if n mod 123456=0 then
   write('YES')
 else
  if n mod 1234567=0 then
   write('YES')
 else
 if l=0 then
  write('NO')
 else
 if k=0 then
  for j:=1 to l do
   begin
    if n1 mod 1234=0 then
     f1:=1;
    f2:=1;
    n1:=n1-123456;
    if n1 mod 1234=0 then
     f1:=1;
   end
 else

 begin
   for i:=1 to k do
    begin
     n1:=n; n1:=n1-i*1234567;
    for j:=1 to (n1 div 123456) do
     begin
      if n1 mod 1234=0 then
       f1:=1;
      f2:=1;
      n1:=n1-123456;
      if n1 mod 1234=0 then
       f1:=1;
     end;
    end;
 end;
    for j:=1 to l do
     begin
      if n mod 1234=0 then
       f1:=1;
      f2:=1;
      n:=n-123456;
      if n mod 1234=0 then
       f1:=1;
     end;
if (k=1) and (n1 div 123456=0) then
 if n1 mod 1234=0 then
  write('YES')
 else
 if f1=1 then
  write('YES')
 else
  write('NO')
else
if f2=1 then
 if f1=1 then
  write('YES')
 else
  write('NO');
end.

Var
n,a,b,c,d,i,j,k,l,f,n1,f1,f2:longint;
begin
 read(n); n1:=n;
 k:= n div 1234567;
 l:= n div 123456;
 f:=n div 1234;
 if f=0 then
  write('NO')
 else
 if n mod 1234=0 then
  write('YES')
 else
  if n mod 123456=0 then
   write('YES')
 else
  if n mod 1234567=0 then
   write('YES')
 else
 if l=0 then
  write('NO')
 else
 if k=0 then
  for j:=1 to l do
   begin
    if n1 mod 1234=0 then
     f1:=1;
    f2:=1;
    n1:=n1-123456;
    if n1 mod 1234=0 then
     f1:=1;
   end
 else

 begin
   for i:=1 to k do
    begin
     n1:=n; n1:=n1-i*1234567;
    for j:=1 to (n1 div 123456) do
     begin
      if n1 mod 1234=0 then
       f1:=1;
      f2:=1;
      n1:=n1-123456;
      if n1 mod 1234=0 then
       f1:=1;
     end;
    end;
 end;
if f2=1 then
 if f1=1 then
  write('YES')
 else
  write('NO');
end.

const
   fin='';fon='';
   t=1234567;
   t2=123456;
   t3=1234;
   maxn=trunc(1e9);
var
   n:longint;
procedure nhap;
   begin
   assigN(input,fin);reset(input);
   assigN(output,fon);rewritE(output);
   readln(n);
   end;
procedure xuli;
   var
      i,j,k:longint;
      top1,top2,top3,c:int64;
   begin
   top1:=maxn div t;
   top2:=maxn div t2;
   for i:=1 to top1 do
      for j:=1 to top2 do
         begin
         c:=n-(t*i+t2*j);
         if (c mod t3=0) and (c>=t3) then
            begin
            writeln('YES');
            writeln(i,' ',j,' ',(c div t3));
            exit;
            end;
         end;
   writeln('NO');
   close(input);
   close(output);
   end;
begin
nhap;
xuli;
end.

const
   fin='';fon='';
   t=1234567;
   t2=123456;
   t3=1234;
   maxn=trunc(1e9);
var
   n:longint;
procedure nhap;
   begin
   assigN(input,fin);reset(input);
   assigN(output,fon);rewritE(output);
   readln(n);
   end;
procedure xuli;
   var
      i,j,k:longint;
      top1,top2,top3,c:int64;
   begin
   top1:=maxn div t;
   top2:=maxn div t2;
   if (n mod t=0) or (n mod t2=0) or (n mod t3=0) then
         begin
         writeln('YES');
         exit;
         end;
   for i:=0 to top1+1 do
      for j:=0 to top2+1 do
         begin
         c:=n-(t*i+t2*j);
         if (c mod t3=0) and (c>=t3) then
            begin
            writeln('YES');
            //writeln(i,' ',j,' ',(c div t3));
            exit;
            end;
         end;
   writeln('NO');
   close(input);
   close(output);
   end;
begin
nhap;
xuli;
end.

const
   fin='';fon='';
   t=1234567;
   t2=123456;
   t3=1234;
   maxn=trunc(1e9);
var
   n:longint;
procedure nhap;
   begin
   assigN(input,fin);reset(input);
   assigN(output,fon);rewritE(output);
   readln(n);
   end;
procedure xuli;
   var
      i,j,k:longint;
      top1,top2,top3,c:int64;
   begin
   top1:=maxn div t;
   top2:=maxn div t2;
   for i:=1 to top1+1 do
      for j:=1 to top2+1 do
         begin
         c:=n-(t*i+t2*j);
         if (c mod t3=0) and (c>=t3) then
            begin
            writeln('YES');
            //writeln(i,' ',j,' ',(c div t3));
            exit;
            end;
         end;
   writeln('NO');
   close(input);
   close(output);
   end;
begin
nhap;
xuli;
end.

const
   fin='';fon='';
   t=1234567;
   t2=123456;
   t3=1234;
   maxn=trunc(1e9);
var
   n:longint;
procedure nhap;
   begin
   assigN(input,fin);reset(input);
   assigN(output,fon);rewritE(output);
   readln(n);
   end;
procedure xuli;
   var
      i,j,k:longint;
      top1,top2,top3,c:int64;
   begin
   top1:=maxn div t;
   top2:=maxn div t2;
   for i:=0 to top1+1 do
      for j:=0 to top2+1 do
         begin
         c:=n-(t*i+t2*j);
         if (c mod t3=0) and (c>=t3) then
            begin
            writeln('YES');
            //writeln(i,' ',j,' ',(c div t3));
            exit;
            end;
         end;
   writeln('NO');
   close(input);
   close(output);
   end;
begin
nhap;
xuli;
end.

var
  ans:boolean;
  n,i,j,m,m1,m2:longint;
{
procedure dfs(x,z:longint);
begin
  if (x mod 1234567=0)or(x mod 123456=0)or(x mod 1234=0)
    then begin
           writeln('YES');
           halt;
         end;
  if (x>1234567)and(z<>1)
    then dfs(x-1234567,1);
  if (x>123456)and(z<>2)
    then dfs(x-123456,2);
  if (x>1234)and(z<>3)
    then dfs(x-1234,3);
end;
}
begin
  m1:=811;
  m2:=8101;
  readln(n);
  for i:=1 to m1 do
    begin
      for j:=1 to m2 do
        begin
          m:=n-(i*1234567+j*123456);
          if m<0
            then break;
          if (m mod 1234=0)
            then begin
                   writeln('YES');
                   halt;
                 end;
         end;
    end;
  writeln('NO');
end.

Const Fi='';
      Fo='';
Var f:text;
    n:longint;
    a,b,c:longint;
Procedure docfile;
         Begin
              Assign(f,fi);Reset(f);
              Readln(f,n);
              Close(f);
         End;
Function kt:boolean;
        Begin
             For a:=0 to 810 do
                For b:=0 to 8100 do
                      If n-a*1234567 +b*123456 mod 1234 =0 then exit(true);
             exit(false);
        End;
Procedure xuly;
         Begin
              Assign(f,fo);
              Rewrite(f);
              If kt=true then writeln(f,'YES') else writeln(f,'NO');
              Close(f);
         End;
Begin
     docfile;
     xuly;
End.

Const Fi='';
      Fo='';
Var f:text;
    n:longint;
    a,b,c:longint;
Procedure docfile;
         Begin
              Assign(f,fi);Reset(f);
              Readln(f,n);
              Close(f);
         End;
Function kt:boolean;
        Begin
             For a:=0 to n div 1234567 do
                For b:=0 to n div 123456 do
                      If (n-a*1234567 -b*123456) mod 1234 =0 then exit(true);
             exit(false);
        End;
Procedure xuly;
         Begin
              Assign(f,fo);
              Rewrite(f);
              If kt=true then writeln(f,'YES') else writeln(f,'NO');
              Close(f);
         End;
Begin
     docfile;
     xuly;
End.

Const Fi='';
      Fo='';
Var f:text;
    n:longint;
    a,b,c:longint;
Procedure docfile;
         Begin
              Assign(f,fi);Reset(f);
              Readln(f,n);
              Close(f);
         End;
Function kt:boolean;
        Begin
             For a:=0 to n div 1234567 do
                For b:=0 to n div 123456 do
                   If (n-a*1234567 -b*123456) >0 then
                      If (n-a*1234567 -b*123456) mod 1234 =0 then exit(true);
             exit(false);
        End;
Procedure xuly;
         Begin
              Assign(f,fo);
              Rewrite(f);
              If kt=true then writeln(f,'YES') else writeln(f,'NO');
              Close(f);
         End;
Begin
     docfile;
     xuly;
End.

var
  n, i, j, f : longint;
begin
  f := 0;
  readln(n);
  for i := 1 to n div 1234567 do
    for j := 1 to n div 123456 do
      if (n - (i * 1234567 + j * 123456) >= 0) and
        ((n - (i * 1234567 + j * 123456)) mod 1234 = 0) then f := 1;
  if f = 1 then writeln('YES')
  else writeln('NO');
end.

uses math;
const
	fin='';
	fon='';
	maxC=1000000008;
	maxN=108;

Procedure Openf;
begin
	assign(input,fin); reset(input);
	assign(output,fon); rewrite(output);
end;

Procedure Closef;
begin
	close(input);
	close(output);
End;

Procedure Nhap;
var
        i,j,n,a,b,c,tg:longint;
Begin
        readln(n);
        tg:=n;
        a:=1234567;
        b:=123456;
        c:=1234;

        while n>=a do
        begin
                n:=n-a;
        end;
        //writeln(n);
        while n>=b do
        n:=n-b;
        while n>=c do
        n:=n-c;
        if n=0 then writeln('YES')
        else writeln('NO');
End;

Procedure xuli;
begin
end;

Procedure Xuat;
begin
end;

BEGIN
	Openf;
	Nhap;
	Xuli;
	Xuat;
	Closef;
END.

var n:longint;
begin
  readln(n);
  n:=n mod 1234567;
  if n>0 then n:=n mod 123456;
  if n>0 then n:=n mod 1234;
  if n=0 then writeln('YES')
         else writeln('NO');
end.

var a,b,n,x,f,kk,aa,otv,i1,i2,i3:longint;
begin
readlN(n);
for i1:=0 to 1000 do
for i2:=0 to 100 do
for i3:=0 to 100 do
if (i3*1234567+i2*123456+i1*1234=n) then begin writelN('YES');halt;end;
writelN('NO');
end.

const fi = '';
      fo = '';
var a, b, c, n: longint;
begin
  assign(input,fi); reset(input);
  assign(output,fo); rewrite(output);

  readln(n);
  for a:=1 to n div 1234567 do
    for b:=1 to (n-a*1234567) div 123456 do
      if (n-a*1234567-b*123456) mod 1234 = 0 then
          begin
            writeln('YES');
            halt;
          end;
  writeln('NO');

  close(input); close(output);
end.

var
 os,r,t,y,a,b,c,n,i,j,q:longint;
 const h:longint=1234567;
 const bu:longint=1234;
 const k:longint=123456;
begin
 readln(N);
 a:=n div h;
 n:=n mod h;

 b:=n div k;
 n:=n mod k;

 c:=n div bu;
 n:=n mod bu;

 if n=0 then
 writeln('YES') else writeln('NO');

end.

   const eps=0.000000000001;  besk1=8000000000000000000;  nn=200000;
   modul=1000000007;
   label 1,2;
    type
    mas=array [0..nn] of longint;   point=record x,y,z:real;end;
    zap=record p1,p2,p3 :int64;end; zap1=^zap;
    mass=array[0..nn] of longint;mass1=^mass;
    var ii,jj,n,qq,xx,q,pos,left,right,amax,ost,h,tt,t1,pp,p,aa,bb,cc,kk,dist,pmax,imax,p1,p2,p3,p4,t,l,ot,nm,s1,s2,m,s,cen,t2,i1,besk:int64;
    mask,newug,ugol,newdl,dloch,dugol,ist,jst,ifin,jfin,newnapx,newnapy,napx,napy,step, k,i,j:longint;
     prmax,v,aaa,bbb,ccc:extended; hhh:zap;
     dl,ochnewi,ochnewj,xxx,yyy,ochi,ochj,c,y,z,newoch,och,x,aold,b,a:array[-100000..2000000] of int64;
   stpr,stoim,blup,bldown, blright, blleft, ppole,vol:array[-10..1200,-10..1200] of int64;
   marked:array[-10..1100,-10..1100] of boolean;
    sss, ss1,ss2:string;
    sa,sb,pra,prb,suma,sumb,ota,otb,mmax,mmin,smax,smin:array[-100..200000] of extended;
     fl:array[0..200] of boolean;
    ss: array[-10..100000] of string;ch3,ch,ch1,ch2:char;
    razl:array[0..300000,0..18] of integer;
    pr:array[-10..300,-10..300] of extended;
  ddmax,ddmin :array[-1..300000,-1..20] of extended;
    pole :array [-10..2000] of char;
    maxpr:array[0..nn] of string;
    str:array[0..nn] of string;
     tt1,tt2,disk,v0,v1,s0,t0:extended;
     zv:point;
     newx,newy,yy:array[-1..200000] of int64;

     yot:double;
       flag,flag1:boolean;
    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 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 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 div 2); degg:=p*p mod modul;   end;

end;

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

readln(n);
   for i:=1 to 1000  do for j:=1 to 10000 do

     begin
       ii:=i; jj:=j;
        t:=n-ii*1234567-jj*123456;
        if (t>=0) and(t mod 1234=0) then flag:=true;

     end;

    if  flag then write('YES') else write('NO');

   1:          close(output);
end.

var a,b,c,f,n,i,j,l,m:longint;  p:array [1..1000000] of longint;
begin
  {assign (input,'input.txt'); reset (input);
  assign (output,'output.txt'); rewrite (output);  }
  readln (n);
  f:=0;
  a:=1234;
  b:=123456;
  c:=1234567;
  l:=1;
  if n mod a=0 then f:=1;
  if n mod b=0 then f:=1;
  if n mod c=0 then f:=1;
  if f=0 then
   begin
     i:=1;
     p[l]:=n;
     l:=l+1;
     if n>c then
else
begin
     while (f=0) and (i<=(n div c)) do
      begin
        m:=n-(c*i);
        if m mod a=0 then f:=1;
        if m mod b=0 then f:=1;
        p[l]:=m;
        l:=l+1;
        i:=i+1;
      end;
end;
     l:=l-1;
     if f=0 then
      begin
        i:=1;
        while (f=0) and (i<=l) do
         begin
           j:=1;
           while (f=0) and (j<=(p[i] div b)) do
            begin
              m:=p[i]-b*j;
              if m mod a=0 then f:=1;
              j:=j+1;
            end;
           i:=i+1;
         end;
      end;
   end;
  //if f=1 then writeln ('YES',' ',m,' ',l,' ',i-1,' ',p[i-1]) else writeln ('NO');
  if f=1 then write ('YES') else write ('NO');
end.

var a,b,c,f,n,i,j,l,m:longint;  p:array [1..1000000] of longint;
begin
  {assign (input,'input.txt'); reset (input);
  assign (output,'output.txt'); rewrite (output);  }
  readln (n);
  f:=0;
  a:=1234;
  b:=123456;
  c:=1234567;
  l:=1;
  if n mod a=0 then f:=1;
  if n mod b=0 then f:=1;
  if n mod c=0 then f:=1;
  if f=0 then
   begin
     i:=1;
     if n<c then begin p[l]:=n; l:=l+1; end
else
begin
     while (f=0) and (i<=(n div c)) do
      begin
        m:=n-(c*i);
        if m mod a=0 then f:=1;
        if m mod b=0 then f:=1;
        p[l]:=m;
        l:=l+1;
        i:=i+1;
      end;
end;
     l:=l-1;
     if f=0 then
      begin
        i:=1;
        while (f=0) and (i<=l) do
         begin
           j:=1;
           while (f=0) and (j<=(p[i] div b)) do
            begin
              m:=p[i]-b*j;
              if m mod a=0 then f:=1;
              j:=j+1;
            end;
           i:=i+1;
         end;
      end;
   end;
  //if f=1 then writeln ('YES',' ',m,' ',l,' ',i-1,' ',p[i-1]) else writeln ('NO');
  if f=1 then write ('YES') else write ('NO');
end.

var
  n : int64;

begin
  readln(n);
  if n div 1234567 = 0 then begin writeln('NO'); halt; end;
  n:=n-(n div 1234567)*1234567;
  if n div 123456 = 0 then begin writeln('NO'); halt; end;
  n:=n-(n div 123456)*123456;
  if n div 1234 = 0 then begin writeln('NO'); halt; end;
  n:=n-(n div 1234)*1234;
  if n = 0 then writeln('YES') else writeln('NO');
end.

var n,i,i1,i2,i3,n1,n2,n3:longint;
    ans:int64;
    a:array[1..100000] of longint;
begin
read(n);
n1:=n div 1234567+1;
n2:=n div 123456+1;
n3:=n div 1234+1;
for i1:=1 to n1 do
for i2:=1 to n2 do
if (n-i1*1234567-i2*123456) mod 1234=0 then begin write('YES'); halt; end;
write('NO');
end.

var
    a, b, c: LongInt;
    n: Int64;

begin
    Read (n);

    for a:= 0 to n div 1234567 + 1 do
        for b:= 0 to (n - a * 1234567) div 123456 + 1 do
                if (n - a * 1234567 - b * 123456) mod 1234 = 0 then
                begin
                    WriteLn ('YES');
                    Exit
                end;

    WriteLn ('NO')
end.

var
     N,k1,k2,i,j:Longint;
     F:Boolean;
begin
     Readln(N);
     k1:=N div 1234567;
     F:=False;
     for i:=0 to k1 do
begin
     k2:=(N-i*1234567) div 123456;
     for j:=0 to k2 do
     if ((n-i*1234567-j*123456)>0) and ((n-i*1234567-j*123456) mod 1234=0) then begin
                                                                                F:=True;
                                                                                Break;
                                                                                end;
     if F then Break;
 end;
     if F then Writeln('YES')
          else Writeln('NO');
 end.

var
  a,b,i,n,k,j:longint;
begin
  read(n);
  n:=n mod 1234567;
  if n=0 then begin writeln('NO');halt;end;
  n:=n mod 123456;
  if n=0 then begin writeln('NO');halt;end;
  n:=n mod 1234;
  if n=0 then writeln('YES') else writeln('NO');
end.