arr = 2.times.map { gets.chomp } from, to = arr.map { %w[E W S N].index(_1) } puts((to - from).then { _1.negative? ? _1 + %w[E W S N].length : _1 })