#include <bits/stdc++.h>
using namespace std;
const int N = 17;
const int M = 105;
int n, m;
char a[N][M];
int f[N][3];
int lm[N], rm[N];
void init() {
  for (int i = 1; i <= n; ++i) {
    lm[i] = m + 1;
    for (int j = 1; j <= m; ++j)
      if (a[i][j] == '1') {
        lm[i] = min(lm[i], j);
        rm[i] = j;
      }
  }
}
int getTop() {
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (a[i][j] == '1') return i;
  return n + 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%s", a[i]);
  init();
  int least = getTop();
  if (least == n + 1) return puts("0"), 0;
  if (least == n) return printf("%d\n", rm[n]), 0;
  f[n][0] = rm[n] * 2;
  f[n][1] = m + 1;
  for (int i = n - 1; i; --i) {
    f[i][0] = min(f[i + 1][1] + m + 2, f[i + 1][0] + 1 + rm[i] * 2);
    f[i][1] = min(f[i + 1][0] + m + 2, f[i + 1][1] + 1 + (m + 1 - lm[i]) * 2);
  }
  int l = least;
  printf("%d\n",
         min(f[l + 1][0] + 1 + rm[l], f[l + 1][1] + 1 + (m + 1 - lm[l])));
  return 0;
}
