#include <bits/stdc++.h>
using namespace std;

class Node {
  public:
    int x;
    int y;
    int cost;
    Node() {}
    Node(int _x, int _y, int _cost) {
      x = _x;
      y = _y;
      cost = _cost;
    }
};

class Point {
  public:
    int x;
    int y;
    Point() {}
    Point(int _x, int _y) {
      x = _x;
      y = _y;
    }
};

int main()
{
  int h, w;
  cin >> h >> w;
  vector<vector<char>> board(h, vector<char>(w, ' '));
  vector<vector<int>> cost(h, vector<int>(w, 1e9));
  vector<vector<Point>> tele(26);
  int startx = -1, starty = -1, goalx = -1, goaly = -1;
  for (int x = 0; x < h; x++) {
    for (int y = 0; y < w; y++) {
      char ch = getchar();
      while (ch == '\n') {
        ch = getchar();
      }
      board.at(x).at(y) = ch;
      switch (ch) {
        case 'S':
          startx = x;
          starty = y;
          break;
        case 'G':
          goalx = x;
          goaly = y;
          break;
        case '#':
          cost[x][y] = -1;
          break;
        case '.':
          break;
        default:
          tele.at(ch - 'a').emplace_back(x, y);
          break;
      }
    }
    getchar(); // LF
  }
  vector<bool> tele_is_alive(26, true);
  queue<Node> q;
  q.emplace(startx, starty, 0);
  while (!q.empty()) {
    auto n = q.front();
    q.pop();
    if (n.x < 0 || h <= n.x || n.y < 0 || w <= n.y) {
      continue;
    }
    if (cost.at(n.x).at(n.y) <= n.cost) {
      continue;
    }
    cost.at(n.x).at(n.y) = n.cost;
    if (n.x == goalx && n.y == goaly) {
      break;
    }
    char ch = board.at(n.x).at(n.y);
    if ('a' <= ch && ch <= 'z' && tele_is_alive.at(ch-'a')) {
      for (auto p : tele.at(ch-'a')) {
        q.emplace(p.x, p.y, n.cost+1);
      }
      tele_is_alive.at(ch-'a') = false;
    }
    q.emplace(n.x+1, n.y, n.cost+1);
    q.emplace(n.x-1, n.y, n.cost+1);
    q.emplace(n.x, n.y+1, n.cost+1);
    q.emplace(n.x, n.y-1, n.cost+1);
  }
  if (cost.at(goalx).at(goaly) == 1e9) {
    cout << "-1\n";
  } else {
    cout << cost.at(goalx).at(goaly) << '\n';
  }
  return 0;
}

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cassert>
#include <map>
#include <numeric>
#include <cstring>
#include <set>
#include <ctime>
#include <queue>
#include <cmath>
#include <iomanip>
#include <iterator>

using namespace std;

class InputReader {
public:
    InputReader() {}
    InputReader(const char *file_name) {
        input_file = fopen(file_name, "r");
        cursor = 0;
        fread(buffer, SIZE, 1, input_file);
    }
    inline InputReader &operator >>(int &n) {
        while((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-') {
            advance();
        }
        int sign = 1;
        if (buffer[cursor] == '-') {
            sign = -1;
            advance();
        }
        n = 0;
        while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
            n = n * 10 + buffer[cursor] - '0';
            advance();
        }
        n *= sign;
        return *this;
    }
private:
    FILE *input_file;
    static const int SIZE = 1 << 17;
    int cursor;
    char buffer[SIZE];
    inline void advance() {
        ++ cursor;
        if(cursor == SIZE) {
            cursor = 0;
            fread(buffer, SIZE, 1, input_file);
        }
    }
};

const int MAXN = 200000;

char s[1 + MAXN];
char t[3];

bool check(int start, int n) {
    t[0] = t[1] = '1';
    t[2] = '0';
    for (int i = start, j = 0; i <= n; i++, j = (j + 1) % 3) {
        if (s[i] != t[j]) {
            return false;
        }
    }
    return true;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //ifstream cin("input.txt");
    int n;
    long long p = 10000000000L;
    cin >> n >> (s + 1);
    if (n == 1) {
        if (s[1] == '0') {
            cout << p << "\n";
        } else {
            cout << p * 2 << "\n";
        }
    } else if (n == 2) {
        if (s[1] == '0' && s[2] == '0') {
            cout << "0\n";
        } else if (s[1] == '0' && s[2] == '1') {
            cout << p - 1 << "\n";
        } else {
            cout << p << "\n";
        }
    } else {
        if (s[1] == '0') {
            if (check(2, n)) {
                int m = 1 + (n - 2) / 3 + 1;
                cout << p - m + 1 << "\n";
            } else {
                cout << "0\n";
            }
        } else if (s[1] == '1' && s[2] == '0') {
            if (check(3, n)) {
                int m = 1 + (n - 3) / 3 + 1;
                cout << p - m + 1 << "\n";
            } else {
                cout << "0\n";
            }
        } else if (s[1] == '1' && s[2] == '1' && s[3] == '0') {
            if (n == 3) {
                cout << p << "\n";
            } else if (check(4, n)) {
                int m = 1 + (n - 4) / 3 + 1;
                cout << p - m + 1 << "\n";
            } else {
                cout << "0\n";
            }
        } else {
            cout << "0\n";
        }
    }
    return 0;
}