#include <bits/stdc++.h>
using namespace std;
const int N = 123;
char M[N][N];
int l[N], r[N], vis[N], dp[N][2];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = n; i >= 1; i--) {
    dp[i][0] = dp[i][1] = 0x3f3f3f3f;
    for (int j = 0; j <= m + 1; j++) {
      cin >> M[i][j];
      if (M[i][j] == '1') {
        if (!l[i]) l[i] = j;
        r[i] = j;
        vis[i] = 1;
      }
    }
  }
  int x = n;
  while (!vis[x--] && x >= 1) n--;
  if (n == 1) {
    if (!vis[1])
      cout << 0 << endl;
    else
      cout << r[1] << endl;
    return 0;
  }
  dp[1][0] = r[1] ? 2 * r[1] : 0;
  dp[1][1] = m + 1;
  for (int i = 2; i < n; i++) {
    if (!vis[i]) {
      dp[i][0] = dp[i - 1][0] + 1;
      dp[i][1] = dp[i - 1][1] + 1;
      continue;
    }
    dp[i][0] = min(dp[i - 1][0] + 1 + 2 * r[i], dp[i - 1][1] + 1 + m + 1);
    dp[i][1] =
        min(dp[i - 1][1] + 1 + 2 * (m + 1 - l[i]), dp[i - 1][0] + 1 + m + 1);
  }
  int ans = min(dp[n - 1][0] + r[n] + 1, dp[n - 1][1] + (m + 1 - l[n]) + 1);
  cout << ans << endl;
  return 0;
}
