#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<string> grid(n);
  for (int i = 0; i < n; ++i) {
    cin >> grid[n - i - 1];
  }
  while (grid.size() > 1 &&
         count(grid.back().begin(), grid.back().end(), '1') == 0) {
    grid.pop_back();
  }
  n = grid.size();
  array<int, 2> dp = {0, m + 1};
  for (int i = 0; i < n; ++i) {
    int l = 0;
    int r = m + 1;
    for (int j = 1; j <= m; ++j) {
      if (grid[i][j] == '1') {
        l = max(l, j);
        r = min(r, j);
      }
    }
    if (i + 1 == n) {
      dp = {dp[0] + l, dp[1] + m + 1 - r};
    } else {
      dp = {min(dp[0] + l * 2, dp[1] + m + 1),
            min(dp[0] + m + 1, dp[1] + (m + 1 - r) * 2)};
    }
  }
  cout << min(dp[0], dp[1]) + n - 1 << endl;
}
