#include <bits/stdc++.h>
using namespace std;
char s1[20][1000];
int st[20], en[20], val[20][1000];
int main() {
  int i, j, t1, t2, t3, t4, n, m, ans, ind;
  scanf("%d %d", &n, &m);
  for (i = 0; i < n; i++) {
    scanf("%s", s1[i]);
    st[i] = en[i] = -1;
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m + 2; j++) {
      val[i][j] = -1;
      if (s1[i][j] == '1') {
        if (st[i] == -1) {
          st[i] = en[i] = j;
        } else {
          en[i] = j;
        }
      }
    }
  }
  ans = t1 = 0;
  if (st[n - 1] != -1) {
    val[n - 1][0] = 2 * en[n - 1];
    val[n - 1][m + 1] = m + 1;
  } else {
    val[n - 1][0] = 0;
    val[n - 1][m + 1] = m + 1;
  }
  for (i = n - 2; i >= 0; i--) {
    if (st[i] == -1) {
      val[i][0] = val[i + 1][0] + 1;
      val[i][m + 1] = val[i + 1][m + 1] + 1;
    } else {
      val[i][0] =
          min(1 + val[i + 1][m + 1] + m + 1, 1 + val[i + 1][0] + 2 * en[i]);
      val[i][m + 1] = min(1 + val[i + 1][0] + m + 1,
                          1 + val[i + 1][m + 1] + 2 * (m + 1 - st[i]));
    }
  }
  ind = n;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m + 2; j++) {
      if (st[i] != -1) {
        ind = i;
        break;
      }
    }
    if (ind != n) break;
  }
  ind++;
  if (ind >= n) {
    if (st[n - 1] == -1)
      ans = 0;
    else
      ans = en[n - 1];
  } else {
    ans = min(1 + val[ind][0] + en[ind - 1],
              1 + val[ind][m + 1] + (m + 1 - st[ind - 1]));
  }
  printf("%d\n", ans);
  return 0;
}
