#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, lprev = 0, lcurr, rprev = 0, rcurr, l, r, a[101];
  bool flag, flag1;
  scanf("%d%d", &n, &m);
  flag1 = false;
  for (int i = 0; i < n; i++) {
    flag = false;
    for (int j = 0; j < m + 2; j++) {
      scanf("%1d", &a[j]);
      if (a[j]) flag = true;
    }
    if (!flag && !flag1) continue;
    for (int j = 1; j < m + 1; j++)
      if (a[j]) {
        r = j;
        break;
      }
    for (int j = m; j > 0; j--)
      if (a[j]) {
        l = j;
        break;
      }
    if (!flag1) {
      lprev = l;
      rprev = m + 1 - r;
      flag1 = true;
      continue;
    }
    if (!flag) {
      lcurr = min(rprev + m + 2, lprev + 1);
      rcurr = min(lprev + m + 2, rprev + 1);
    } else {
      lcurr = min(rprev + m + 2, lprev + 1 + l * 2);
      rcurr = min(lprev + m + 2, rprev + 1 + (m + 1 - r) * 2);
    }
    lprev = lcurr;
    rprev = rcurr;
  }
  cout << lprev;
  return 0;
}
