require 'set'

n, m = gets.split.map(&:to_i)
a = [[?#] * (m + 2)] + (1..n).map { [?#] + gets.chomp.split("") + [?#] } + [[?#] * (m + 2)] 
y = x = dy = dx = gy = gx = nil
(1..n).each do |i|
  (1..m).each do |j|
    next if a[i][j] == ?. or a[i][j] == ?# 
    if a[i][j] ==  ?G
      gy, gx = i, j
      a[i][j] = ?.
      next
    end
    y, x = i, j
    case a[i][j]
    when ?<
      dy, dx = 0, -1
    when ?^
      dy, dx = -1, 0
    when ?v
      dy, dx = 1, 0
    when ?>
      dy, dx = 0, 1
    end
    a[i][j] = ?.
  end
end

reached = Set[[y, x, dy, dx]]
answer = -1
loop do
  until a[y + dy][x + dx] == ?.
    dy, dx = -dx, dy 
    break if reached.include?([y, x, dy, dx])
    reached << [y, x, dy, dx]
  end
  y += dy
  x += dx
  break if reached.include?([y, x, dy, dx])
  reached << [y, x, dy, dx]
  if y == gy && x == gx
    answer = reached.to_a.map {|b| b[0..1]}.uniq.size
    break
  end
  if a[y + dx][x - dy] == ?.
    dy, dx = dx, -dy
    y += dy
    x += dx
    break if reached.include?([y, x, dy, dx])
    reached << [y, x, dy, dx]
    if y == gy && x == gx
      answer = reached.to_a.map {|b| b[0..1]}.uniq.size
      break
    end
  end
end
p answer

