#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000 + 10;
const int MAXM = 100 + 10;
const int MOD = 1000000007;
const int INF = 1000000001;
int a[20][120];
int l[20], r[20];
int f[20], g[20];
bool w[20];
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int L = m + 1, R = 0;
  for (int i = n; i >= 1; i--) {
    l[i] = m + 1;
    for (int j = 0; j <= m + 1; j++) {
      char c;
      cin >> c;
      a[i][j] = c - '0';
      w[i] |= a[i][j];
      if (a[i][j]) {
        r[i] = j;
        if (l[i] == m + 1) l[i] = j;
      }
    }
  }
  while (n > 1 && !w[n]) n--;
  for (int i = 0; i <= m + 1; i++)
    if (a[n][i]) {
      if (i < L) L = i;
      if (i > R) R = i;
    }
  f[1] = r[1] * 2;
  g[1] = m + 1;
  f[0] = -1;
  g[0] = 1000000000;
  for (int i = 2; i <= n; i++) {
    f[i] = g[i - 1] + m + 2;
    g[i] = f[i - 1] + m + 2;
    f[i] = min(f[i], f[i - 1] + 1 + r[i] * 2);
    g[i] = min(g[i], g[i - 1] + 1 + (m + 1 - l[i]) * 2);
  }
  cout << min(f[n - 1] + 1 + R, g[n - 1] + 1 + m + 1 - L) << endl;
  return 0;
}
