rt
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e3 + 10;
int n, m, sx, sy, fx, fy, ans[MAXN][MAXN];
char c[MAXN][MAXN];
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
struct node {
int x, y, step;
};
deque<node> q;
void Record(int x, int y, int step) {
if(x < 1 || y < 1 || x > n || y > m || c[x][y] == '#' || ans[x][y] <= step) {
return;
}
q.push_back({x, y, step});
ans[x][y] = step;
}
void bfs(int x, int y) {
fill(ans[0] + 1, ans[n] + m + 1, INT_MAX);
Record(x, y, 0);
while(!q.empty()) {
node now = q.front();
q.pop_front();
for(int i = 0; i < 4; i++) {
int nx = now.x + dx[i], ny = now.y + dy[i];
Record(nx, ny, now.step + 1);
}
}
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> c[i][j];
if(c[i][j] == 'S') {
sx = i, sy = j;
}
if(c[i][j] == 'G') {
fx = i, fy = j;
}
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(c[i][j] == '^') {
int x = i, y = j;
c[x][y] = '.';
for(; c[x][y] == '.' && x >= 1; x += -1) {
c[x][y] = '#';
}
}
else if(c[i][j] == '<') {
int x = i, y = j;
c[x][y] = '.';
for(; c[x][y] == '.' && y >= 1; y += -1) {
c[x][y] = '#';
}
}
else if(c[i][j] == '>') {
int x = i, y = j;
c[x][y] = '.';
for(; c[x][y] == '.' && y <= m; y += 1) {
c[x][y] = '#';
}
}
else if(c[i][j] == 'v') {
int x = i, y = j;
c[x][y] = '.';
for(; c[x][y] == '.' && x <= n; x++) {
c[x][y] = '#';
}
}
}
}
bfs(sx, sy);
cout << (ans[fx][fy] == INT_MAX ? -1 : ans[fx][fy]);
return 0;
}