#include <bits/stdc++.h>
using namespace std;
const int MAX_BUF_SIZE = 16384;
char BUFOR[MAX_BUF_SIZE];
int BUF_SIZE, BUF_POS;
char ZZZ;
const int MXN = 510;
const int C = 262144;
const int INF = 1000000001;
int D[MXN][MXN][2];
int ile[MXN];
int pref[MXN][MXN];
int n, m;
int a[MXN][MXN];
void test() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char z = getchar();
      while (!isalpha(z)) z = getchar();
      a[i][j] = (z == 'W');
      pref[i][j] = pref[i][j - 1] + a[i][j];
    }
    ile[i] = ile[i - 1] + pref[i][m];
  }
  for (int i = 0; i <= n + 1; i++)
    for (int j = 0; j <= m + 1; j++) D[i][j][0] = D[i][j][1] = INF;
  D[0][1][0] = -1;
  while (n && pref[n][m] == 0) n--;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      D[i][j][1] = D[i][j - 1][1] + 1;
      if (pref[i][j - 1] == 0 && pref[i - 1][j - 1] == 0)
        D[i][j][1] = min(D[i][j][1], D[i - 1][j][0] + 1);
    }
    for (int j = m; j >= 1; j--) {
      D[i][j][0] = D[i][j + 1][0] + 1;
      if (pref[i][m] - pref[i][j] == 0 && pref[i - 1][m] == pref[i - 1][j])
        D[i][j][0] = min(D[i - 1][j][1] + 1, D[i][j][0]);
    }
  }
  int last, mini;
  last = mini = -1;
  for (int j = 1; j <= m; j++) {
    if (a[n][j] == 1) {
      if (mini == -1) mini = j;
      last = j;
    }
  }
  printf("%d\n", min(D[n][mini][0], D[n][last][1]));
}
int main() {
  int te = 1;
  while (te--) test();
}
