#include <bits/stdc++.h>
using namespace std;
void read(long long& x) { cin >> x; }
const long long MOD = 1e9 + 7;
const long long mod = 1e9 + 7;
long long n, m;
long long temp;
long long cnt;
long long ans;
long long a[(long long)1e6 + 6];
long long dp[(long long)1e5 + 6];
void test_case() {
  read(n);
  read(m);
  long long arr[n + 1][m + 2];
  long long last = -1;
  for (long long i = n; i >= 1; i--) {
    bool fon = false;
    string s;
    cin >> s;
    for (long long j = 0; j <= m + 1; j++) {
      arr[i][j] = s[j] == '1';
      if (arr[i][j] == 1) {
        fon = true;
      }
    }
    if (fon && last == -1) {
      last = i;
    }
  }
  long long dp[n + 89][2];
  memset(dp, mod, sizeof(dp));
  long long ans = 0;
  if (last == -1) {
    cout << ans << "\n";
    return;
  }
  dp[1][0] = 0;
  dp[1][1] = m + 1;
  for (long long i = 1; i <= n - 1; i++) {
    vector<long long> temp;
    for (long long j = 0; j <= m + 1; j++) {
      if (arr[i][j]) {
        temp.push_back(j);
      }
    }
    if (temp.size() == 0) {
      dp[i + 1][0] = min(dp[i][0], dp[i + 1][1] + m + 1);
      dp[i + 1][1] = min(dp[i][1], dp[i][0] + m + 1);
    } else {
      long long left = temp.front();
      long long right = temp.back();
      dp[i + 1][0] = min(dp[i][0] + 2 * right, dp[i][1] + m + 1);
      dp[i + 1][1] = min(dp[i][1] + 2 * (m + 1 - left), dp[i][0] + m + 1);
    }
    dp[i + 1][0]++;
    dp[i + 1][1]++;
  }
  vector<long long> temp;
  for (long long j = 0; j <= m + 1; j++) {
    if (arr[last][j]) {
      temp.push_back(j);
    }
  }
  ans = min(dp[last][0] + temp.back(), dp[last][1] + (m + 1 - temp.front()));
  cout << ans << "\n";
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  long long t = 1;
  while (t--) {
    test_case();
  }
}
