// template {{{

#include <bits/stdc++.h>
using namespace std;

#define loop(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) loop(i, 0, n)
#define rloop(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); i--)
#define rrep(i, n) rloop(i, 0, n)
#define eb emplace_back
#define ef emplace_front
#define pb pop_back
#define pf pop_front
#define all(c) std::begin(c), std::end(c)
#define mp std::make_pair
#define mt std::make_tuple
#define fi first
#define se second
#define popcnt __builtin_popcountll

using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

template<typename T>
using max_pq = priority_queue<T, vector<T>, less<T>>;

template<typename T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;
const ll LLINF = 1e18 + 10;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int dx8[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

template<typename T>
inline T sq(T x){ return x * x; }

template<typename T, typename U>
inline bool chmax(T &x, U y){ if (x >= y) return false; x = y; return true; }

template<typename T, typename U>
inline bool chmin(T &x, U y){ if (x <= y) return false; x = y; return true; }

template<typename T>
inline void sort(T &c){ std::sort(std::begin(c), std::end(c)); }

template<typename T>
inline void reverse(T &c){ std::reverse(std::begin(c), std::end(c)); }

template<typename T>
inline void unique(T &c){ std::sort(std::begin(c), std::end(c)); c.erase(std::unique(all(c)), std::end(c)); }

// }}}

const int rdx[][4] = {
	{-1, -1, 0, 1},
	{0, -1, -1, -1},
	{1, 1, 0, -1},
	{0, 1, 1, 1}
};

const int rdy[][4] = {
	{0, 1, 1, 1},
	{-1, -1, 0, 1},
	{0, -1, -1, -1},
	{1, 1, 0, -1}
};

int conv[256];

int n, m;
char g[64][64];
int sx, sy, sdir, gx, gy;
int dist[64][64][4][4];
tuple<int, int, int, int> prv[64][64][4][4];

int main()
{
	rep(i, 4) conv["^<v>"[i]] = i;
	memset(g, '#', sizeof(g));
	cin >> n >> m;
	rep(i, n) rep(j, m){
		char &c = g[i + 1][j + 1];
		cin >> c;
		if (c == 'G'){
			gx = i + 1;
			gy = j + 1;
			c = '.';
		}
		else if (c != '#' && c != '.'){
			sx = i + 1;
			sy = j + 1;
			sdir = conv[c];
			c = '.';
		}
	}
	
	tuple<int, int, int, int> v;
	deque<tuple<int, int, int, int, int>> q;
	fill_n(***dist, 64 * 64 * 4 * 4, INF);
	dist[sx][sy][sdir][2] = 0;
	q.eb(0, sx, sy, sdir, 2);
	while (q.size()){
		int sum, x, y, dir, rdir;
		tie(sum, x, y, dir, rdir) = q.front(); q.pf();
		if (x == gx && y == gy){
			v = tie(x, y, dir, rdir);
			break;
		}
		if (dist[x][y][dir][rdir] < sum) continue;
		
		int nx = x + dx[dir], ny = y + dy[dir];
		if (g[nx][ny] == '.' && rdir < 3){
			if (dist[nx][ny][dir][rdir + 1] > sum + 1){
				dist[nx][ny][dir][rdir + 1] = sum + 1;
				prv[nx][ny][dir][rdir + 1] = tie(x, y, dir, rdir);
				q.eb(sum + 1, nx, ny, dir, rdir + 1);
			}
		}

		if (rdir >= 2){
			if (dist[x][y][(dir + 3) % 4][rdir - 2] > sum){
				dist[x][y][(dir + 3) % 4][rdir - 2] = sum;
				prv[x][y][(dir + 3) % 4][rdir - 2] = tie(x, y, dir, rdir);
				q.ef(sum, x, y, (dir + 3) % 4, rdir - 2);
			}
		}
		else {
			if (dist[x][y][(dir + 1) % 4][rdir + 2] > sum){
				dist[x][y][(dir + 1) % 4][rdir + 2] = sum;
				prv[x][y][(dir + 1) % 4][rdir + 2] = tie(x, y, dir, rdir);
				q.ef(sum, x, y, (dir + 1) % 4, rdir + 2);
			}
		}
		
		vector<int> nxs = {-1, 1};
		if (~rdir & 1) nxs.eb((rdir ^ 2) - rdir);
		for (int i : nxs){
			int nrdir = rdir + i;
			if (nrdir < 0 || nrdir >= 4) continue;
			int nrx = x + rdx[dir][nrdir], nry = y + rdy[dir][nrdir];
			if (g[nrx][nry] != '#') continue;
			if (dist[x][y][dir][nrdir] > sum){
				dist[x][y][dir][nrdir] = sum;
				prv[x][y][dir][nrdir] = tie(x, y, dir, rdir);
				q.ef(sum, x, y, dir, nrdir);
			}
		}
	}
	
	if (v == mt(0, 0, 0, 0)){
		cout << -1 << endl;
		return 0;
	}
	
	set<pair<int, int>> st;
	while (v != mt(0, 0, 0, 0)){
		int x, y, dir, rdir;
		tie(x, y, dir, rdir) = v;
		v = prv[x][y][dir][rdir];
		st.insert(mp(x, y));
	}

	cout << st.size() << endl;
}

Measured memory_bytes: 4288