#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
int row, col;
int bud[20][123];
int lt[20], rt[20];
int flolim;
int ans[20][123];
void solve() {
  for (int i = 1; i <= row; ++i) lt[i] = inf, rt[i] = inf;
  for (int r = 1; r <= row; ++r) {
    for (int c = 1; c <= col; ++c) {
      if (bud[r][c]) {
        rt[r] = col + 1 - c;
        break;
      }
    }
    for (int c = col; c >= 1; --c) {
      if (bud[r][c]) {
        lt[r] = c;
        break;
      }
    }
  }
  flolim = inf;
  for (int r = row; r >= 1; --r) {
    if (lt[r] != inf) {
      flolim = r;
      break;
    }
  }
  if (flolim == inf) {
    printf("0\n");
  } else if (flolim == 1) {
    printf("%d\n", lt[1]);
  } else {
    if (lt[1] != inf)
      ans[1][0] = lt[1] * 2;
    else
      ans[1][0] = 0;
    ans[1][col + 1] = col + 1;
    for (int i = 2; i <= flolim - 1; ++i) {
      ans[i][0] = ans[i - 1][0] + 1;
      ans[i][col + 1] = ans[i - 1][col + 1] + 1;
      if (lt[i] == inf) {
        ans[i][0] = min(ans[i][0], ans[i - 1][col + 1] + 1 + col + 1);
        ans[i][col + 1] = min(ans[i][col + 1], ans[i - 1][0] + 1 + col + 1);
      } else {
        ans[i][0] =
            min(ans[i][0] + lt[i] * 2, ans[i - 1][col + 1] + 1 + col + 1);
        ans[i][col + 1] =
            min(ans[i][col + 1] + rt[i] * 2, ans[i - 1][0] + 1 + col + 1);
      }
    }
    ans[flolim][0] = ans[flolim - 1][0] + 1;
    ans[flolim][col + 1] = ans[flolim - 1][col + 1] + 1;
    int res =
        min(ans[flolim][0] + lt[flolim], ans[flolim][col + 1] + rt[flolim]);
    printf("%d\n", res);
  }
}
int main() {
  char str[123];
  scanf("%d %d", &row, &col);
  for (int r = row; r >= 1; --r) {
    scanf("%s", str);
    for (int c = 0; c <= col + 1; ++c) bud[r][c] = str[c] - '0';
  }
  solve();
  return 0;
}
