#include <bits/stdc++.h>
using namespace std;
int n, m, a[20][105];
int fir(int x) {
  if (find(a[x], a[x] + m + 2, 1) == a[x] + m + 2) return m + 1;
  for (int i = 1; i <= m; ++i)
    if (a[x][i]) return i;
}
int last(int x) {
  if (find(a[x], a[x] + m + 2, 1) == a[x] + m + 2) return 0;
  for (int i = m; i; --i)
    if (a[x][i]) return i;
}
int main() {
  cin >> n >> m;
  for (int i = n; i; --i)
    for (int j = 0; j <= m + 1; ++j) {
      char c;
      cin >> c;
      a[i][j] = c - '0';
    }
  while (n && find(a[n], a[n] + m + 2, 1) == a[n] + m + 2) --n;
  if (!n) cout << 0 << endl, exit(0);
  int x = 0, y = m + 1;
  for (int i = 1; i < n; ++i) {
    int u = fir(i), v = last(i);
    int tx = min(x + v * 2 + 1, y + m + 2);
    int ty = min(y + (m + 1 - u) * 2 + 1, x + m + 2);
    x = tx, y = ty;
  }
  int u = fir(n), v = last(n);
  cout << min(x + v, y + m + 1 - u) << endl;
  return 0;
}
