#include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<ll(n);++i)
#define RREP(i,n) for(ll i=ll(n)-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<ll(n);++i)
#define RFOR(i,m,n) for(ll i=ll(n)-1;i>=ll(m);--i)
#define ALL(v) (v).begin(),(v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

constexpr int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
constexpr int dy[8] = { 0,1,1,1,0,-1,-1,-1 };


using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }

using tpl=tuple<int, int, int, int>;
pair<bool, tpl> nex(vector<string>& v, tpl tup, int nx, int ny) {
	auto ret = tup;
	bool ok = true;
	int x, y, d, t;
	tie(x, y, d, t) = tup;
	x += nx;
	y += ny;
	ret = tpl(x, y, d, t);
	if (v[x][y] == '#')return { false,ret };
	if (t == 3)return { false,ret };
	assert(t != 0);
	get<3>(ret)++;
	return { true,ret };

}
template<typename T>
T& ac(vector<vector<vector<vector<T>>>>& v, tpl& tup) {
	int a, b, c, d;
	tie(a, b, c, d) = tup;
	return (v[a][b][c][d]);
}
pair<bool, tpl> rrot(vector<string>& v, tpl tup) {
	auto ret = tup;
	bool ok = true;
	get<2>(ret) += 3;
	get<2>(ret) %= 4;
	if (get<3>(ret) < 2)return{ false,ret };
	get<3>(ret) -= 2;
	return { true,ret };
}

pair<bool, tpl> lrot(vector<string>& v, tpl tup) {
	auto ret = tup;
	bool ok = true;
	get<2>(ret)++;
	get<2>(ret) %= 4;
	if (get<3>(ret) >= 2)return { false,ret };
	get<3>(ret) += 2;
	return { true,ret };

}


bool f(vector<string>& v, int x, int y, int dir) {
	return v[x + dx[dir]][y + dy[dir]] == '#';
}
int g(int t, int dir) {
	return (2 * t - dir + 8) % 8;
}
bool check(vector<string>& v, tpl tup, int dir) {
	int x, y, d, t;
	tie(x, y, d, t) = tup;
	return f(v, x, y, g(d, dir));
}
tpl ne(tpl now, int dir) {
	get<3>(now) = dir;
	return now;
}

pair<bool, tpl> rsl(vector<string>& v, tpl tup) {
	auto ret = tup;
	int x, y, a, t;
	tie(x, y, a, t) = ret;
	if (t == 3)return{ false,ret };
	else if (t == 2) {
		if (!check(v, ret, 4) && check(v, ret, 3))return { true,ne(ret,3) };
	}
	else if (t == 1) {
		if (check(v, ret, 2))return { true,ne(ret,2) };
	}
	else {
		if (check(v, ret, 2))return { true,ne(ret,2) };

		if (check(v, ret, 1))return { true,ne(ret,1) };
	}

	return { false,ret };
}

pair<bool, tpl> lsl(vector<string>& v, tpl tup) {
	auto ret = tup;
	int x, y, a, t;
	tie(x, y, a, t) = ret;
	if (t == 0)return{ false,ret };
	else if (t == 1) {
		if (check(v, ret, 0))return { true,ne(ret,0) };
	}
	else if (t == 2) {
		if (check(v, ret, 0))return { true,ne(ret,0) };
		if (check(v, ret, 1))return { true,ne(ret,1) };
	}
	else {
		if (check(v, ret, 2))return { true,ne(ret,2) };
	}
	return { false,ret };
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m; cin >> n >> m;
	vector<string> tmp(n);
	REP(i, n)cin >> tmp[i];
	vector<string> v(n + 2);
	v[0] = string(m + 2, '#');
	v[n + 1] = string(m + 2, '#');
	REP(i, n)v[i + 1] = "#" + tmp[i] + "#";
	int sx, sy, tx, ty, sd;
	REP(i, n + 2)REP(j, m + 2) {
		if (v[i][j] == 'G') {
			tx = i;
			ty = j;
			v[i][j] = '.';
		}
		else if (v[i][j] != '.' && v[i][j] != '#') {
			sx = i;
			sy = j;

			if (v[i][j] == '^')sd = 2;
			else if (v[i][j] == 'v')sd = 0;
			else if (v[i][j] == '>')sd = 1;
			else sd = 3;
			v[i][j] = '.';
		}
	}


	vector<vector<vvi>> b(n + 2, vector<vvi>(m + 2, vvi(4, vi(4, INF))));
	vector<vector<vector<vector<tpl>>>> prev(n + 2, vector<vector<vector<tpl>>>(m + 2, vector<vector<tpl>>(4, vector<tpl>(4))));

	b[sx][sy][sd][2] = 0;
	queue<tpl> q;

	auto update = [&](bool f, tpl tup, tpl p, int cost) {
		if (f && ac(b, tup) > ac(b, p) + cost) {
			q.push(tup);
			ac(b, tup) = ac(b, p) + cost;
			ac(prev, tup) = p;
		}
	};

	q.push(tpl(sx, sy, sd, 2));
	while (!q.empty()) {
		auto p = q.front(); q.pop();
		pair<bool, tpl> tup;
		//cout << ac(b, p) << endl;
		tup = nex(v, p, dx[get<2>(p) * 2], dy[get<2>(p) * 2]);
		update(tup.first, tup.second, p, 1);

		tup = rrot(v, p);
		update(tup.first, tup.second, p, 0);
		tup = lrot(v, p);
		update(tup.first, tup.second, p, 0);
		tup = rsl(v, p);
		update(tup.first, tup.second, p, 0);
		tup = lsl(v, p);
		update(tup.first, tup.second, p, 0);
	}
	set<pii> st;
	int mi = INF;
	tpl now;
	REP(i, 4)REP(j, 4)if (chmin(mi, b[tx][ty][i][j]))now = tpl(tx, ty, i, j);
	if (mi == INF) {
		cout << -1 << endl;
		return 0;
	}
	st.insert({ get<0>(now),get<1>(now) });
	while (ac(prev, now) != tpl()) {
		now = ac(prev, now);
		st.insert({ get<0>(now), get<1>(now) });

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


}


Measured memory_bytes: 5188