#include <bits/stdc++.h>
using namespace std;
int N, M;
int mat[100 + 10][100 + 10];
int d[100 + 10][2];
int side[100 + 10][2];
char buf[100 + 10];
int main() {
  scanf("%d%d", &N, &M);
  M += 2;
  int maxH = N;
  for (int i = 0; i < N; i++) {
    scanf("%s", buf);
    for (int j = 0; j < M; j++) mat[i][j] = buf[j] - '0';
    side[i][0] = M - 1;
    for (int j = 0; j < M; j++)
      if (mat[i][j] == 1) {
        if (maxH == N) maxH = i;
        if (side[i][0] > j) side[i][0] = j;
        side[i][1] = j;
      }
  }
  d[N][0] = -1, d[N][1] = 10000000;
  for (int i = N - 1; i > 0; i--) {
    d[i][0] = min(d[i + 1][0] + 2 * side[i][1] + 1, d[i + 1][1] + M);
    d[i][1] = min(d[i + 1][0] + M, d[i + 1][1] + 2 * (M - side[i][0] - 1) + 1);
  }
  if (maxH == N)
    printf("0\n");
  else
    printf("%d\n", min(d[maxH + 1][0] + side[maxH][1] + 1,
                       d[maxH + 1][1] + (M - side[maxH][0])));
}
