#include <bits/stdc++.h>
using namespace std;
int l[100001], r[100001], dp[110][2];
char a[100001];
int main() {
  int fn, n = 0, m;
  cin >> fn >> m;
  int sum = 0;
  for (int i = fn; i >= 1; i--) {
    scanf("%s", a);
    for (int j = 0; j <= m + 1; j++) {
      if (a[j] == '1') {
        if (l[i] == 0) l[i] = j;
        r[i] = j;
        sum = 1;
      }
    }
    if (!l[i]) l[i] = m + 1;
    if (sum && (n == 0)) n = i;
  }
  if (sum == 0) {
    cout << "0" << endl;
    return 0;
  }
  if (n == 1) {
    cout << r[1] << endl;
    return 0;
  }
  dp[1][0] = 2 * r[1];
  dp[1][1] = m + 1;
  for (int i = 2; i <= n - 1; i++) {
    dp[i][0] = min(dp[i - 1][0] + 2 * r[i], dp[i - 1][1] + m + 1) + 1;
    dp[i][1] = min(dp[i - 1][0] + m + 1, dp[i - 1][1] + 2 * (m - l[i] + 1)) + 1;
  }
  cout << min(dp[n - 1][0] + r[n], dp[n - 1][1] + m - l[n] + 1) + 1 << endl;
  return 0;
}
