#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e18;
const int mod = 1000000007;
int main() {
  long long int t, n, m, i, j, k = -1, l, x, y, z;
  scanf("%lld %lld", &n, &m);
  char mat[n][m + 5];
  for (i = 0; i < n; ++i) scanf("%s", mat[i]);
  for (i = 0; i < n; ++i) {
    for (j = 1; j <= m; ++j)
      if (mat[i][j] == '1') {
        k = i;
        break;
      }
    if (k != -1) break;
  }
  if (k == -1) {
    puts("0");
    return 0;
  }
  long long int DP[n][2];
  if (k < (n - 1)) {
    DP[n - 1][1] = m + 1;
    DP[n - 1][0] = 0;
    for (i = 1; i <= m; ++i) {
      if (mat[n - 1][i] == '1') DP[n - 1][0] = max(DP[n - 1][0], 2 * i);
    }
    for (i = n - 2; i > k; --i) {
      DP[i][0] = 0;
      DP[i][1] = 0;
      for (j = 1; j <= m; ++j) {
        if (mat[i][j] == '1') {
          DP[i][0] = max(DP[i][0], 2 * j);
          DP[i][1] = max(DP[i][1], 2 * (m + 1 - j));
        }
      }
      DP[i][0] = min(DP[i][0] + DP[i + 1][0] + 1, DP[i + 1][1] + 1 + m + 1);
      DP[i][1] = min(DP[i][1] + DP[i + 1][1] + 1, DP[i + 1][0] + 1 + m + 1);
    }
    DP[i][0] = 0;
    DP[i][1] = 0;
    for (j = 1; j <= m; ++j)
      if (mat[i][j] == '1') {
        DP[i][0] = max(DP[i][0], j);
        DP[i][1] = max(DP[i][1], (m + 1 - j));
      }
    DP[i][0] = min(DP[i][0] + DP[i + 1][0] + 1, DP[i + 1][1] + 1 + m + 1);
    DP[i][1] = min(DP[i][1] + DP[i + 1][1] + 1, DP[i + 1][0] + 1 + m + 1);
  } else {
    i = k;
    DP[i][0] = 0;
    DP[i][1] = m + 1;
    for (j = 1; j <= m; ++j)
      if (mat[i][j] == '1') DP[i][0] = max(DP[i][0], j);
  }
  printf("%lld\n", min(DP[i][0], DP[i][1]));
  return 0;
}
