#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 10;
const int INF = 0x3f3f3f3f;
int n, m, L[N], R[N], dp[N][N];
char S[N][N];
int main() {
  scanf("%d%d", &n, &m);
  m += 2;
  for (int i = n; i >= 1; --i) scanf("%s", S[i] + 1);
  for (int i = 1; i <= n; ++i) {
    L[i] = m;
    R[i] = 1;
    for (int j = 1; j <= m; ++j) {
      if (S[i][j] == '0') continue;
      L[i] = min(L[i], j);
      R[i] = max(R[i], j);
    }
  }
  int highest = 0;
  for (int i = n; i >= 1; --i)
    if (L[i] != m) {
      highest = i;
      break;
    }
  if (highest == 0) {
    puts("0");
    return 0;
  }
  memset(dp, INF, sizeof dp);
  dp[1][0] = 0;
  for (int i = 1; i < highest; ++i) {
    if (dp[i][0] > dp[i][1])
      dp[i][0] = min(dp[i][0], dp[i][1] + m - 1);
    else
      dp[i][1] = min(dp[i][1], dp[i][0] + m - 1);
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + 2 * (R[i] - 1) + 1);
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + m);
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + 2 * (m - L[i]) + 1);
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + m);
  }
  int ans = INF;
  ans = min(ans, dp[highest][0] + R[highest] - 1);
  ans = min(ans, dp[highest][1] + m - L[highest]);
  printf("%d", ans);
  return 0;
}
