#include <bits/stdc++.h>
using namespace std;
int d[1005][2];
char M[1005][1005];
struct Node {
  int l = 0, r = 0;
  int p;
} L[1005], R[1005];
int n, m;
int v[1005];
void Cal(int i) {
  L[i].l = L[i].p;
  R[i].l = R[i].p;
  L[i].r = m - 1 - L[i].p;
  R[i].r = m - 1 - R[i].p;
}
int main() {
  scanf("%d%d", &n, &m);
  m += 2;
  memset(v, 0, sizeof(v));
  int Layer = -1;
  for (int i = 0; i < n; i++) {
    scanf("%s", M[i]);
    L[i].p = 0;
    R[i].p = n;
    for (int j = 0; j < m; j++) {
      if (M[i][j] == '1') {
        if (!L[i].p) L[i].p = j;
        R[i].p = j;
        if (Layer == -1) Layer = i;
        v[i] = 1;
      }
    }
    Cal(i);
  }
  d[n][0] = -1;
  d[n][1] = m - 1;
  if (Layer == -1) {
    cout << "0" << endl;
    return 0;
  }
  for (int i = n - 1; i > Layer; i--) {
    if (!v[i]) {
      d[i][0] = d[i + 1][0] + 1;
      d[i][1] = d[i + 1][1] + 1;
      continue;
    }
    d[i][0] = min(d[i + 1][1] + m - 1, d[i + 1][0] + 2 * R[i].l) + 1;
    d[i][1] = min(d[i + 1][0] + m - 1, d[i + 1][1] + 2 * L[i].r) + 1;
  }
  printf("%d\n",
         1 + min(d[Layer + 1][0] + R[Layer].l, d[Layer + 1][1] + L[Layer].r));
}
