#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}


struct edge { int to, cost;};

class Dijkstra{
private:
    vector< vector<edge> > g;

public:
    Dijkstra(){};
    Dijkstra(int size){
        g.resize(size);
    }

    vi dist;

    void add(int a, int b, int c){//無向辺
        edge e1 = {b, c}, e2 = {a, c};
        g[a].push_back(e1);
        //g[b].push_back(e2);//有向辺の場合はここをコメントアウトする
    }

    void calc(int s){
        priority_queue< pii, vector<pii>, greater<pii> > q;
        int n = g.size();
        dist = vi (n, INF);
        dist[s] = 0;
        q.push(pii(0, s));

        while(!q.empty()){
            pii p = q.top();
            q.pop();
            int v = p.second;
            if(dist[v] < p.first)continue; //更新されていたら何もしない
            for(int i = 0; i < g[v].size(); i++){
                edge e = g[v][i];
                if(dist[e.to] > dist[v] + e.cost){
                    dist[e.to] = dist[v] + e.cost;
                    q.push(pii(dist[e.to], e.to));
                }
            }
        }
    }

};


int h,w;

int f(pii p){
    return p.first * w + p.second;
}
int g(pii x, pii y){
    return abs(x.first - y.first) + abs(x.second - y.second);
}
int num(string s){
    int ret = 0;
    rep(i,s.size()){
        ret *= 10;
        ret += s[i] - '0';
    }
    return ret;
}

signed main(void) {
    while(cin >> w >> h, h){
        int sx,sy,gx,gy;
        vector<vp> v(h*w);
        rep(i,h)rep(j,w){
            string in;
            cin >> in;
            if(in == ".");
            else if(in == "S")sx = i, sy = j;
            else if(in == "G")gx = i, gy = j;
            else v[num(in)].push_back(pii(i,j));
        }
        v[0].push_back(pii(sx,sy));
        int ind = 0;
        while(v[ind].size())ind++;
        v[ind].push_back(pii(gx,gy));
        while(v.back().size() == 0)v.pop_back();
        /*
        rep(i,v.size()){
            cout << i << endl;
            rep(j,v[i].size())cout << v[i][j].first << " " << v[i][j].second << endl;
        }
        */
        Dijkstra dij(h*w);
        rep(i,v.size()-1){
            rep(j,v[i].size())rep(k,v[i+1].size()){
                //v[i][j]からv[i+1][k]に辺をはる
                dij.add(f(v[i][j]),f(v[i+1][k]), g(v[i][j],v[i+1][k]));
            }
        }
        dij.calc(f(v.front().front()));
        cout << dij.dist[f(v.back().front())] << endl;
    }

}



Measured memory_bytes: 10060