Language: C++

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<list>
#include<cctype>
#include<climits>

using namespace std;

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

//----?????°????????????????????????????????£??????????????????
template <class T>
class myQueue {
public:
	T *data;
	int head, tail;
	int max_size;

	myQueue() {};
	myQueue(int size);
	T &dequeue();
	void enqueue(T &city);
};

template <class T>
myQueue<T>::myQueue(int size) {
	data = new T[size];
	head = tail = 0;
	max_size = size;
}

template <class T>
T &myQueue<T>::dequeue() {
	if (head == max_size) head = 0;
	return data[head++];
}

template <class T>
void myQueue<T>::enqueue(T &c) {
	if (tail == max_size) tail = 0;
	data[tail++] = c;
}
//-------------------------------------

struct Data
{
	int x, y, dist, state;
	Data() {}
	Data(int _x, int _y, int _dist, int _state)
	{
		x = _x;
		y = _y;
		dist = _dist;
		state = _state;
	}
};

char fld[1000][1000];
bool isUsed[5][1000][1000];

inline int GetNext(int n)
{
	return (n == 4) ? 0 : n + 1;
}

inline int GetPrev(int n)
{
	return (n == 0) ? 4 : n - 1;
}

signed main()
{
	int W, H;
	while (cin >> W >> H, W || H)
	{
		int sx, sy, gx, gy;
		for (int i = 0; i < H; i++)
		{
			for (int j = 0; j < W; j++)
			{
				scanf(" %c", &fld[i][j]);
				if (fld[i][j] == 'S')
				{
					sx = j;
					sy = i;
				}
				else if (fld[i][j] == 'G')
				{
					gx = j;
					gy = i;
				}
			}
		}
		
		int mi = INT_MAX / 2, ans;
		for (int i = 0; i < 5; i++)
		{
			myQueue<Data> q(5 * W * H);
			Data qf;
			
			q.enqueue(Data(sx, sy, 0, i));
			fill_n((bool*)isUsed, 5 * 1000 * 1000, false);
			isUsed[i][sy][sx] = true;
			while (q.head < q.tail)
			{
				qf = q.dequeue();
				if (qf.x == gx && qf.y == gy && qf.state == GetPrev(qf.state))
				{
					if (mi > qf.dist)
					{
						mi = qf.dist;
						ans = i;
						break;
					}
				}
				for (int j = 0; j < 4; j++)
				{
					int nx = qf.x + dx[j];
					int ny = qf.y + dy[j];
					if (nx < 0 || W <= nx || ny < 0 || H <= ny) continue;
					int state = qf.state;
					if (isdigit(fld[ny][nx]) && fld[ny][nx] - '1' != i && fld[ny][nx] - '1' == GetNext(state))
					{
						state = GetNext(state);
					}
					if (isUsed[state][ny][nx]) continue;
					isUsed[state][ny][nx] = true;
					q.enqueue(Data(nx, ny, qf.dist + 1, state));
				}
			}
		}
		if (mi == INT_MAX / 2) puts("NA");
		else printf("%d %d\n", ans, mi);
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:106:35: error: cannot bind non-const lvalue reference of type 'Data&' to an rvalue of type 'Data'
  106 |                         q.enqueue(Data(sx, sy, 0, i));
      |                                   ^~~~~~~~~~~~~~~~~~
a.cc:44:29: note:   initializing argument 1 of 'void myQueue<T>::enqueue(T&) [with T = Data]'
   44 | void myQueue<T>::enqueue(T &c) {
      |                          ~~~^
a.cc:133:51: error: cannot bind non-const lvalue reference of type 'Data&' to an rvalue of type 'Data'
  133 |                                         q.enqueue(Data(nx, ny, qf.dist + 1, state));
      |                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:44:29: note:   initializing argument 1 of 'void myQueue<T>::enqueue(T&) [with T = Data]'
   44 | void myQueue<T>::enqueue(T &c) {
      |                          ~~~^