($n,$x,$y)=glob<>; $z=$y+0; @r=reverse map{ $s=(~$z&$_+0?0:2)|($y&~$_?0:1); $y=-1,$z=0 if !$s; $y&=~$_ if $s&2; $z|=~$_ if $s&1; $s; }reverse$x,glob<>; @r=-1 if shift@r!=3; print"@r";