n = gets.strip.to_i x1,y1,x2,y2 = gets.strip.split(' ').map(&:to_i) @mvs = %w(UL UR R LR LL L) def getmoves(a,b,n) [[a - 2,b-1], [a-2, b + 1], [a, b + 2], [a+2, b+1],[a+2,b-1],[a,b-2]].zip(@mvs).select{|(a,b),mv| a >= 0 && b >= 0 && a < n && b < n} end @cue = Queue.new #@cue.push nil @cue.push [[x1,y1],''] @thecash = {} @thecash[[x1,y1]] = true 10000000.times do break if @cue.size < 1 vll = @cue.pop mv,str = vll x1,y1 = mv if x1 == x2 && y1 == y2 puts str.split(' ').count puts str.strip exit end break if !mv mvs = getmoves(x1,y1,n) mvs.each do |onemove,curstr| if !@thecash[onemove] @cue.push [onemove, str + ' ' + curstr] @thecash[onemove] = true end end end puts 'Impossible'