#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 2e3 + 5;
int n, m, sx, sy, ex, ey;
char a[maxn][maxn];
bool v[maxn][maxn];
bool f[maxn][maxn];
const int fx[] = {0, 0, 1, 0, -1};
const int fy[] = {0, 1, 0, -1, 0};
const string d = " >V<^";
struct node {
int x, y;
int step;
};
void calc(int x, int y) {
int i = d.find(a[x][y]), u = 1;
f[x][y] = false;
while (true) {
int tx = x + fx[i] * u;
int ty = y + fy[i] * u;
if (tx <= 0 || tx > n || ty <= 0 || ty > m) break;
if (a[tx][ty] == '#' || d.find(a[tx][ty]) != -1) break;
f[tx][ty] = false;
u++;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] == 'S') sx = i, sy = j;
if (a[i][j] == 'G') ex = i, ey = j;
f[i][j] = true;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == '#') f[i][j] = false;
if (d.find(a[i][j]) != -1) {
f[i][j] = false;
calc(i, j);
}
}
}
queue<node> q;
q.push({sx, sy, 0});
v[sx][sy] = true;
while (q.size() > 0) {
node u = q.front(); q.pop();
for (int i = 1; i <= 4; i++) {
```int tx = u.x + fx[i];
int ty = u.y + fy[i];
if (tx <= 0 || tx > n || ty <= 0 || ty > m) continue;
if (v[tx][ty] == true || f[tx][ty] == false) continue;
if (tx == ex && ty == ey) {
cout << u.step + 1 << endl;
return 0;
}
q.push({tx, ty, u.step + 1});
v[tx][ty] = true;
}
}
cout << -1 << endl;
return 0;
}