#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<<25;
int dp[5][1005];

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_cur = -1;
		int res = INF;
		for (int cur = 0; cur < 5; cur++ ){
			rep (i, 5 ) rep (j, 1005 ) dp[i][j] = INF;
			int nxt = (cur+1)%5;
			// S ツつゥツづァ ツ堕ョツ青ォ:nxt ツづ鳴づ個経ツ路
			for (int j = 0; j < p[nxt].size(); j++ ){
				dp[nxt][j] = abs (p[nxt][j].first - s.first ) + abs (p[nxt][j].second - s.second );
			} // end for
			for (int i = nxt, cnt = 0; cnt < 5; cnt++, i = (i+1) % 5  ){
				for (int j = 0; j < p[(i+1) % 5].size(); j++ ){
					for (int k = 0; k < p[i].size(); k++ ){
						int l = abs (p[(i+1)%5][j].first - p[i][k].first ) + abs (p[(i+1)%5][j].second - p[i][k].second );
						dp[(i+1)%5][j] = min (dp[(i+1)%5][j], dp[i][k] + l );
					} // end for
				} // end for
			} // end for
			// ツ堕ョツ青ォ: (cur-1+5) % 5 ツつゥツづァ G ツづ鳴づ個経ツ路
			for (int j = 0; j < p[(cur-1+5)%5].size(); j++ ){
				int l = dp[(cur-1+5)%5][j] + abs (g.first - p[(cur-1+5)%5][j].first ) + abs (g.second - p[(cur-1+5)%5][j].second );
				if (res > l ) {
					res = l;
					first_cur = cur;
				} // end if
			} // end for
		} // end for

		if (res == INF )
			cout << -1 << endl;
		else
			cout << first_cur+1 << ' ' << res << endl;

	} // end loop
		
	return 0;
}