#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, start, end, r, temp1, temp2, j;
  cin >> n >> m;
  r = m + 2;
  int arr[n][r];
  int st_en[n][2];
  string s;
  for (i = 0; i < n; i++) {
    cin >> s;
    start = -1;
    end = -1;
    for (j = 0; j < r; j++) {
      arr[i][j] = s[j] - '0';
      if (arr[i][j] == 1 && start == -1) {
        start = j;
        end = j;
      } else if (arr[i][j] == 1 && start != -1) {
        end = j;
      }
    }
    st_en[n - 1 - i][0] = start;
    st_en[n - 1 - i][1] = end;
  }
  i = n - 1;
  while (st_en[i][0] == -1 && st_en[i][1] == -1 && i >= 0) {
    i--;
  }
  n = i + 1;
  int dp[2][n];
  if (n == 0) {
    cout << "0";
    return 0;
  }
  if (n == 1) {
    if (st_en[0][0] == -1 && st_en[0][1] == -1) {
      dp[0][0] = 0;
      dp[1][0] = r - 1;
    } else {
      dp[0][0] = st_en[0][1];
      dp[1][0] = r - 1;
    }
  } else {
    if (st_en[0][0] == -1 && st_en[0][1] == -1) {
      dp[0][0] = 0;
      dp[1][0] = r - 1;
    } else {
      dp[0][0] = 2 * st_en[0][1];
      dp[1][0] = r - 1;
    }
  }
  for (i = 1; i < n; i++) {
    if (st_en[i][0] == -1 && st_en[i][1] == -1) {
      dp[0][i] = dp[0][i - 1] + 1;
      dp[1][i] = dp[1][i - 1] + 1;
      continue;
    }
    if (i == n - 1) {
      temp1 = 1 + dp[0][i - 1] + st_en[i][1];
      temp2 = 1 + dp[1][i - 1] + r - 1 - st_en[i][0];
      dp[0][i] = min(temp1, temp2);
      dp[1][i] = dp[0][i];
      break;
    }
    temp1 = 1 + dp[0][i - 1] + 2 * st_en[i][1];
    temp2 = 1 + dp[1][i - 1] + r - 1;
    dp[0][i] = min(temp1, temp2);
    temp1 = 1 + dp[0][i - 1] + r - 1;
    temp2 = 1 + dp[1][i - 1] + 2 * (r - 1 - st_en[i][0]);
    dp[1][i] = min(temp1, temp2);
  }
  cout << min(dp[0][n - 1], dp[1][n - 1]);
  return 0;
}
