#include <bits/stdc++.h>
using namespace std;
int v1[110], v2[110];
char sir[20][110];
int main() {
  int n, m;
  scanf("%d%d\n", &n, &m);
  m += 2;
  for (int i = 1; i <= n; i++) gets(sir[i] + 1);
  int poz = 0;
  for (int i = 1; i <= n; i++) {
    int maxx = 0, minn = 1e9;
    for (int j = 1; j <= m; j++)
      if (sir[i][j] == '1') {
        maxx = max(j, maxx);
        minn = min(j, minn);
        if (poz == 0) poz = i;
      }
    if (minn != 1e9) v1[i] = minn;
    v2[i] = maxx;
  }
  if (poz == 0) {
    printf("0");
    return 0;
  }
  int sol = 1e9;
  for (int mask = 0; mask < (1 << n); mask++) {
    int s = 0, p = 1;
    for (int i = n; i >= 1; i--)
      if (mask & (1 << (i - 1))) {
        if (i == 1) s--;
        if (p == 1) {
          s += m;
          p = 2;
        } else {
          s += m;
          p = 1;
        }
        if (poz == i) break;
      } else {
        if (p == 1) {
          if (v2[i] != 0) s += v2[i] - 1;
          if (poz == i) break;
          if (v2[i] != 0) s += v2[i] - 1;
          s++;
        } else {
          if (v1[i] != 0) s += m - v1[i];
          if (poz == i) break;
          if (v1[i] != 0) s += m - v1[i];
          s++;
        }
      }
    sol = min(sol, s);
  }
  printf("%d", sol);
  return 0;
}
