#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:667772160")
template <class T1>
void deb(T1 e1) {
  cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
  cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
  cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
       << endl;
}
int lft[20], rgt[20];
int main() {
  int n, m, a;
  scanf("%d %d", &n, &m);
  m += 2;
  for (int i = 0; i < n; i++) {
    lft[n - i - 1] = m - 1;
    rgt[n - i - 1] = 0;
    for (int j = 0; j < m; j++) {
      scanf("%1d", &a);
      if (a) {
        lft[n - i - 1] = min(lft[n - i - 1], j);
        rgt[n - i - 1] = max(rgt[n - i - 1], j);
      }
    }
  }
  while (n > 0 && lft[n - 1] == m - 1) n--;
  if (n == 0)
    printf("0\n");
  else if (n == 1)
    printf("%d\n", rgt[0]);
  else {
    int all = 1 << (n - 1), pre = 0, ans = n * m + 100000, atmp;
    for (int i = 0; i < all; i++) {
      atmp = 0;
      pre = 0;
      for (int j = 1; j < n; j++) {
        if (i & (1 << (j - 1))) {
          if (pre)
            atmp += (m - lft[j - 1] - 1) * 2;
          else
            atmp += m - 1;
        } else {
          if (pre)
            atmp += m - 1;
          else
            atmp += rgt[j - 1] * 2;
        }
        pre = i & (1 << (j - 1));
      }
      if (pre)
        atmp += (m - lft[n - 1] - 1);
      else
        atmp += rgt[n - 1];
      ans = min(ans, atmp);
    }
    ans += n - 1;
    printf("%d\n", ans);
  }
  return 0;
}
