#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char b[16][128];
int x[16], y[16];
int dp[16][2];
int f(int i, int j) {
  if (dp[i][j] >= 0) return dp[i][j];
  int r;
  if (i == k) {
    assert(y[i] >= 0 && x[i] < m);
    if (j == 0)
      r = y[i];
    else
      r = m - 1 - x[i];
  } else {
    if (y[i] < 0)
      r = 1 + f(i - 1, j);
    else {
      r = m + f(i - 1, j ^ 1);
      if (j == 0)
        r = min(r, 2 * y[i] + 1 + f(i - 1, j));
      else
        r = min(r, 2 * (m - 1 - x[i]) + 1 + f(i - 1, j));
    }
  }
  return dp[i][j] = r;
}
int main() {
  scanf("%d%d", &n, &m);
  m += 2;
  for (int i = 0; i < (n); i++) scanf("%s", b[i]);
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (m); j++) b[i][j] -= '0';
  for (int i = 0; i < (n); i++) {
    x[i] = m;
    y[i] = -1;
    for (int j = 0; j < (m); j++)
      if (b[i][j]) x[i] = min(x[i], j), y[i] = max(y[i], j);
  }
  k = 0;
  while (k < n && y[k] < 0) k++;
  if (k == n) {
    puts("0");
    return 0;
  }
  memset(dp, -1, sizeof(dp));
  printf("%d\n", f(n - 1, 0));
  return 0;
}
