#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
const int MAXN = 1e5 + 5;
const int MAXM = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 2e9;
const ll LLINF = 2e18;
const ld EPS = 1e-9;
int m, n, k, ans, cnt, x;
string s;
int grid[105][105], dp[105][2];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  m += 2;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < m; j++) {
      grid[i][j] = s[j] - '0';
    }
  }
  int i = n - 1, j = 0;
  for (int k = 0; k < m; k++) {
    if (grid[i][k]) {
      j = k;
    }
  }
  dp[n - 1][0] = 2 * j;
  dp[n - 1][1] = m - 1;
  ans = j;
  for (int k = n - 2; k >= 0; k--) {
    int left = -1, right = m;
    int idx = 0;
    while (idx < m && grid[k][idx] == 0) {
      idx++;
    }
    if (idx == m) {
      dp[k][0] = dp[k + 1][0] + 1;
      dp[k][1] = dp[k + 1][1] + 1;
      continue;
    }
    left = idx;
    while (idx < m) {
      if (grid[k][idx]) {
        right = idx;
      }
      idx++;
    }
    ans = min(dp[k + 1][0] + 1 + right, dp[k + 1][1] + m - left);
    dp[k][0] = min(dp[k + 1][0] + 1 + 2 * right, dp[k + 1][1] + m);
    dp[k][1] = min(dp[k + 1][0] + m, dp[k + 1][1] + 1 + 2 * (m - left - 1));
  }
  cout << ans << '\n';
  return 0;
}
