#include <bits/stdc++.h>
using namespace std;
int dp[30][3];
char ch[30][200];
int n, m;
int main() {
  int i, j;
  scanf("%d%d", &n, &m);
  int maxx = 1;
  for (i = 1; i <= n; i++) {
    scanf("%s", ch[i] + 1);
  }
  for (i = 1; i <= n; i++) {
    int flog = 0;
    for (j = 1; j <= m + 2; j++) {
      if (ch[i][j] == '1') {
        flog = 1;
        break;
      }
    }
    if (!flog) {
      maxx = i + 1;
    } else
      break;
  }
  if (maxx > n) {
    printf("0\n");
    return 0;
  }
  int le, ri = 1;
  for (i = 1; i <= m + 2; i++) {
    if (ch[n][i] == '1') {
      ri = i;
    }
  }
  if (maxx == n) {
    printf("%d\n", ri - 1);
    return 0;
  }
  dp[n][1] = (ri - 1) * 2;
  dp[n][2] = m + 1;
  for (i = n - 1; i >= maxx + 1; i--) {
    le = m + 2, ri = 1;
    for (j = 1; j <= m + 2; j++) {
      if (ch[i][j] == '1') {
        le = min(le, j);
        ri = max(ri, j);
      }
    }
    dp[i][1] = min(dp[i + 1][1] + ri * 2 - 1, dp[i + 1][2] + m + 2);
    dp[i][2] = min(dp[i + 1][2] + (m + 3 - le) * 2 - 1, dp[i + 1][1] + m + 2);
  }
  le = 100000, ri = -1;
  for (i = 1; i <= m + 2; i++) {
    if (ch[maxx][i] == '1') {
      le = min(le, i);
      ri = max(ri, i);
    }
  }
  int ans = min(dp[maxx + 1][1] + ri, dp[maxx + 1][2] + m + 2 - le + 1);
  printf("%d\n", ans);
  return 0;
}
