#include <bits/stdc++.h>
using namespace std;
char mp[110][110];
int L[20], R[20];
void debug(vector<int> v) {
  printf(": ");
  for (int i = 0; i < v.size(); i++) {
    printf("%d ", v[i]);
  }
  printf("\n");
}
struct Node {
  int y;
  int side;
  int step;
};
int BFS(int n, int m, int lastRow) {
  int step, mi = 0x3f3f3f3f;
  queue<Node> q;
  Node u;
  u.y = n;
  u.step = 0;
  u.side = 0;
  q.push(u);
  while (!q.empty()) {
    Node cur = q.front();
    q.pop();
    if (L[cur.y] == 0x3f3f3f3f && cur.y != lastRow) {
      cur.y--;
      cur.step++;
      q.push(cur);
      continue;
    }
    if (cur.y == lastRow) {
      if (cur.side == 0) {
        step = cur.step + R[cur.y];
      } else {
        step = cur.step + m + 1 - L[cur.y];
      }
      mi = min(mi, step);
      continue;
    }
    if (cur.y < lastRow) {
      mi = 0;
      continue;
    }
    Node lnext, rnext;
    lnext.y = cur.y - 1;
    rnext.y = cur.y - 1;
    rnext.side = 1;
    lnext.side = 0;
    if (cur.side == 0) {
      lnext.step = cur.step + R[cur.y];
      rnext.step = cur.step + R[cur.y];
      if (cur.y != lastRow) {
        lnext.step += R[cur.y];
        rnext.step += m + 1 - R[cur.y];
        lnext.step++;
        rnext.step++;
      }
    } else {
      lnext.step = cur.step + m + 1 - L[cur.y];
      rnext.step = cur.step + m + 1 - L[cur.y];
      if (cur.y != lastRow) {
        lnext.step += L[cur.y];
        rnext.step += m + 1 - L[cur.y];
        lnext.step++;
        rnext.step++;
      }
    }
    q.push(lnext);
    q.push(rnext);
  }
  return mi;
}
int main() {
  int n, m;
  while (~scanf("%d %d", &n, &m)) {
    int lastRow = 1, f = 0;
    memset(mp, 0, sizeof(mp));
    memset(R, 0, sizeof(R));
    fill(L, L + 20, 0x3f3f3f3f);
    getchar();
    for (int i = 1; i <= n; i++) {
      scanf("%s", mp[i]);
      for (int j = 0; j <= m + 1; j++) {
        if (mp[i][j] == '1') {
          R[i] = max(j, R[i]);
          L[i] = min(j, L[i]);
          f = 1;
        }
      }
      if (L[i] == 0x3f3f3f3f && f == 0) {
        lastRow = i + 1;
      }
    }
    int ans = BFS(n, m, lastRow);
    printf("%d\n", ans);
  }
}
