#include <bits/stdc++.h>
using namespace std;
char a[20][120];
bool book[20];
int l[20], r[20];
int ans = 1000000;
int n, m;
void dfs(int k, int ll, int s) {
  int t;
  if (ll == 1) {
    s += (r[k] - 1);
    t = r[k];
  } else {
    s += (m + 2 - l[k]);
    t = l[k];
  }
  if (n == k) {
    ans = min(ans, s);
    return;
  }
  dfs(k + 1, 0, s + (m + 2 - t + 1));
  dfs(k + 1, 1, s + (t));
}
int main() {
  scanf("%d%d", &n, &m);
  getchar();
  for (int i = n; i >= 1; i--) {
    book[i] = false;
    l[i] = m + 2;
    r[i] = 1;
    for (int j = 1; j <= m + 2; j++) {
      scanf("%c", &(a[i][j]));
      if (a[i][j] == '1') {
        if (!book[i]) l[i] = j;
        book[i] = true;
        r[i] = j;
      }
    }
    getchar();
  }
  for (int i = n; i >= 1; i--) {
    if (book[i]) break;
    n--;
  }
  if (n == 0)
    printf("0\n");
  else {
    ans = 1000000;
    dfs(1, 1, 0);
    printf("%d\n", ans);
  }
  return 0;
}
