L=%w(E S W N) A,B=[gets,gets].map(&:chomp).map { L.find_index(_1) } p [Math.abs(A - B + 4), Math.abs(B - A + 4)].min