#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int> > v;
vector<vector<vector<int> > > memoria;
vector<int> minimo;
vector<int> maximo;
int computa(int ii, int jj, int dir) {
  if (ii == n) return 0;
  int &mem = memoria[ii][jj][dir];
  if (mem != -1) return mem;
  mem = 1000000000;
  if (minimo[ii] == -1 or (jj == minimo[ii] and jj == maximo[ii])) {
    if (dir == 1) {
      for (int j = jj; j < m; j++)
        mem = min(mem, j - jj + 1 + computa(ii + 1, j, 0));
    } else {
      for (int j = 0; j <= jj; j++)
        mem = min(mem, jj - j + 1 + computa(ii + 1, j, 1));
    }
  } else if (minimo[ii] < jj and jj < maximo[ii]) {
  } else if (maximo[ii] <= jj) {
    if (dir == 1) {
    } else {
      for (int j = 0; j <= minimo[ii]; j++)
        mem = min(mem, jj - j + 1 + computa(ii + 1, j, 1));
    }
  } else {
    if (dir == 1) {
      for (int j = maximo[ii]; j < m; j++)
        mem = min(mem, j - jj + 1 + computa(ii + 1, j, 0));
    } else {
    }
  }
  if (ii == n - 1) mem--;
  return mem;
}
int minn(vector<int> w) {
  for (int i = 0; i < int(w.size()); i++)
    if (w[i] == 1) return i;
  return -1;
}
int maxx(vector<int> w) {
  for (int i = int(w.size()) - 1; i >= 0; i--)
    if (w[i] == 1) return i;
  return -1;
}
int main() {
  cin >> n >> m;
  v = vector<vector<int> >(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (c == 'G')
        v[i][j] = 0;
      else
        v[i][j] = 1;
    }
  }
  while (int(v.size()) > 0 and v.back() == vector<int>(m, 0)) {
    v.pop_back();
    n--;
  }
  if (n == 0) {
    cout << 0 << endl;
    exit(0);
  }
  memoria = vector<vector<vector<int> > >(
      n, vector<vector<int> >(m, vector<int>(2, -1)));
  minimo = vector<int>(n);
  maximo = vector<int>(n);
  for (int i = 0; i < n; i++) {
    minimo[i] = minn(v[i]);
    maximo[i] = maxx(v[i]);
  }
  cout << computa(0, 0, 1) << endl;
}
