#include <bits/stdc++.h>
using namespace std;
int n, m;
int floors[15][105];
char line[105];
int dequi(int fl, int stair) {
  int first = m + 1, last = 0;
  for (int i = 1; i <= m; i++)
    if (floors[fl][i]) {
      first = min(first, i);
      last = max(last, i);
    }
  int steps;
  if (stair == 0) {
    steps = last;
    if (fl) {
      int try1 = dequi(fl - 1, 0);
      int try2 = dequi(fl - 1, 1);
      int higher = 0;
      if (try1 && try2) {
        higher = min(try1 + last, try2 + m - last + 1);
        steps += 1 + higher;
      }
    }
  } else {
    steps = m - first + 1;
    if (fl) {
      int try1 = dequi(fl - 1, 0);
      int try2 = dequi(fl - 1, 1);
      int higher = 0;
      if (try1 && try2) {
        higher = min(try1 + first, try2 + m - first + 1);
        steps += 1 + higher;
      }
    }
  }
  return steps;
}
int main() {
  scanf("%d %d\n", &n, &m);
  for (int i = 0; i < n; i++) {
    gets(line);
    for (int j = 1; j <= m; j++) {
      if (line[j] == '1') floors[i][j] = 1;
    }
  }
  int ans = dequi(n - 1, 0);
  printf("%d", ans);
  return 0;
}
