gets e=gets.split.map &:to_i d=[0,-1,0,1] until d.size>e.size d.size.times{|i|e[i+e.size-d.size]-=e[-1]/d[-1]*d[i]} e.pop end e.pop while e[-1]==0&&1