#include <bits/stdc++.h>
using namespace std;
int n, m;
bool a[15][102];
int run(bool left, int f) {
  if (f >= n) return 0;
  int s = -1, e;
  for (int i = 0; i < m; i++) {
    if (a[f][i]) {
      if (s == -1) s = i;
      e = i;
    }
  }
  if (s == -1) return run(left, f + 1) + 1;
  int d = left ? e : m - 1 - s;
  if (f == n - 1) return d;
  int pos = left ? e : s;
  int next = min(pos + run(true, f + 1), m - 1 - pos + run(false, f + 1));
  return d + 1 + next;
}
int main() {
  cin >> n >> m;
  m += 2;
  int top = -1;
  for (int i = n - 1; i >= 0; i--) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      a[i][j] = s[j] == '1';
      if (top == -1 && a[i][j]) top = i;
    }
  }
  n = top + 1;
  cout << run(true, 0);
  return 0;
}
