fun main(args: Array<String>?): Unit {
    operator fun Pair<IntRange, IntRange>.contains(that: Point): Boolean = that.x in first && that.y in second
    operator fun Array<Array<BooleanArray>>.get(state: State): Boolean = this[state.direction.index][state.position.y][state.position.x]
    operator fun Array<Array<BooleanArray>>.set(state: State, value: Boolean): Unit {this[state.direction.index][state.position.y][state.position.x] = value}
    val (n, m) = readLine()!!.trim().split(' ').map(String::toInt)
    val state = Array(n){ readLine()!!.trim()}
    val (sx, sy) = (0 until m).mapNotNull { x -> (0 until n).find{ y -> state[y][x] in "^v<>"}?.let{ y -> Pair(x, y)} }.first()
    val goal = (0 until m).mapNotNull { x -> (0 until n).find{ y -> state[y][x] == 'G'}?.let{y -> Point(x, y)} }.first()
    val range = Pair(0 until m, 0 until n)
    val isPassed = Array(4){Array(n){BooleanArray(m){false}}}
    var current = when(state[sy][sx]) {
        '^' -> State(Point(sx, sy), Direction.North)
        '>' -> State(Point(sx, sy), Direction.East)
        'v' -> State(Point(sx, sy), Direction.South)
        '<' -> State(Point(sx, sy), Direction.West)
        else -> TODO()
    }
    while(current.position != goal && !isPassed[current]) {
        isPassed[current] = true
        val next = current.moveForward()
        if (next.position in range && state[next.position.y][next.position.x] != '#') {
            val right = next.turnRight().moveForward()
            if (next.position != goal && right.position in range && state[right.position.y][right.position.x] != '#') {
                isPassed[next] = true
                current = right
            }else
                current = next
        }else
            current = current.turnLeft()
    }
    if (current.position == goal) {
        println((0 until m).sumBy{ x -> (0 until n).count{ y -> (0 until 4).any{ d -> isPassed[d][y][x]}}} + 1)
    }else {
        println(-1)
    }
}
data class State(val position: Point, val direction: Direction) {
    fun moveForward(): State = State(position.moved(direction), direction)
    fun turnRight(): State = State(position, direction.turnRight())
    fun turnLeft(): State = State(position, direction.turnLeft())
}
data class Point(val x: Int, val y: Int)
fun Point.moved(direction: Direction): Point {
    return when(direction) {
        Direction.North -> Point(x, y - 1)
        Direction.East -> Point(x + 1, y)
        Direction.South -> Point(x, y + 1)
        Direction.West -> Point(x - 1, y)
    }
}
enum class Direction(val index: Int) {
    North(0){
        override fun turnRight(): Direction = East
    },
    East (1){
        override fun turnRight(): Direction = South
    },
    South (2){
        override fun turnRight(): Direction = West
    },
    West (3){
        override fun turnRight(): Direction = North
    };

    abstract fun turnRight(): Direction
    fun turnLeft(): Direction = turnRight().turnRight().turnRight()
}
