#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

const int INF = 1<<30;
vector<PP> dp[5];

int main()
{
//	cut here before submit 
//	freopen ("testcase.Pachimon_Creature", "r", stdin );
	int w, h;
	while (cin >> w >> h && w && h ){
		vector <string> m (h );
		rep (i, h ) m[i].clear();

		P s, g;
		vector <P> p[5];
		rep (i, 5 )
			p[i].clear();

		rep (i, h ){
			cin >> m[i];
		} // end rep
		rep (i, h ){
			rep (j, w ){
				if (m[i][j] == 'S' ) s = P (i, j );
				else if (m[i][j] == 'G' ) g = P (i, j );
				else if (isdigit (m[i][j] ) ) p[m[i][j] - '0'- 1].push_back (P (i, j ) );
			} // end rep
		} // end rep

		int first_give = -1;
		int res = INF;

		for (int give = 0; give < 5; give++ ){
			rep (i, 5 ) dp[i].clear();
			int f = (give+1)%5;
			for (int j = 0; j < p[f].size(); j++ ){
				PP pp = PP (abs (p[f][j].first - s.first ) + abs (p[f][j].second - s.second ), p[f][j] );
				dp[f].push_back (pp );
			} // end for
			for (int i = f, cnt = 0; cnt < 5; cnt++, i = (i+1) % 5  ){
				for (int j = 0; j < p[(i+1) % 5].size(); j++ ){
					int mind = INF;
					for (int k = 0; k < dp[i].size(); k++ ){
						int l = abs (p[(i+1)%5][j].first - dp[i][k].second.first ) + abs (p[(i+1)%5][j].second - dp[i][k].second.second );
						int curr = dp[i][k].first + l;
						if (curr <= mind ){
							mind = curr;
							PP pp = PP (curr, p[(i+1)%5][j] );
							dp[(i+1)%5].push_back (pp );
						} // end if
					} // end for
				} // end for
			} // end for
			for (int j = 0; j < dp[(give-1+5)%5].size(); j++ ){
				int l = dp[(give-1+5)%5][j].first + abs (g.first - dp[(give-1+5)%5][j].second.first ) + abs (g.second - dp[(give-1+5)%5][j].second.second );
				if (res > l ) {
					res = l;
					first_give = give;
				} // end if
			} // end for
		} // end for
		if (res == INF )
			cout << -1 << endl;
		else
			cout << first_give+1 << ' ' << res << endl;

	} // end loop
		
	return 0;
}