#include <bits/stdc++.h>
using namespace std;
char c[120][120];
int i, j, k, m, n, r, s, t, w;
bool b;
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) scanf("%s", c[i] + 1);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m + 2; j++) c[i][j] = c[i][j] - 48;
  for (i = 1; i <= n / 2; i++)
    for (j = 1; j <= m + 2; j++) swap(c[i][j], c[n - i + 1][j]);
  for (n++; (!b) && (n > 0);) {
    n--;
    for (j = 1; j <= m + 2; j++)
      if (c[n][j]) b = true;
  }
  if (n == 0) {
    printf("0");
    return 0;
  }
  s = n * (m + 2);
  for (i = 0; i < (1 << (n - 1)); i++) {
    r = 0, t = 0;
    for (j = 1; j < n; j++) {
      if (((bool)(i & (1 << (j - 1)))) ^ t)
        r = r + m + 2;
      else {
        if (!t) {
          w = 1;
          for (k = 1; k <= m + 2; k++)
            if (c[j][k]) w = k;
          r = r + 2 * (w - 1) + 1;
        } else {
          w = m + 2;
          for (k = m + 2; k > 0; k--)
            if (c[j][k]) w = k;
          r = r + 2 * (m + 2 - w) + 1;
        }
      }
      t = (bool)(i & (1 << (j - 1)));
    }
    if (!t) {
      w = 1;
      for (j = 1; j <= m + 2; j++)
        if (c[n][j]) w = j;
      r = r + w - 1;
    } else {
      w = m + 2;
      for (j = m + 2; j > 0; j--)
        if (c[n][j]) w = j;
      r = r + m + 2 - w;
    }
    s = min(s, r);
  }
  printf("%d", s);
  return 0;
}
