#include <bits/stdc++.h>
using namespace std;
int n, m;
string matrix[17];
int dp[17][2];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = n - 1; i >= 0; i--) {
    cin >> matrix[i];
  }
  int maxf = -1;
  int maxt = 0;
  for (int j = 0; j < m + 2; j++) {
    if (matrix[0][j] == '1') {
      maxt = j;
      maxf = 0;
    }
  }
  dp[0][0] = maxt * 2 + 1;
  dp[0][1] = m + 2;
  int maxsum = maxt;
  for (int i = 1; i < n; i++) {
    maxt = 0;
    int mint = m + 1;
    for (int j = 0; j < m + 2; j++) {
      if (matrix[i][j] == '1') {
        maxt = j;
        maxf = i;
        mint = min(j, mint);
        maxsum = min(dp[i - 1][0] + maxt, dp[i - 1][1] + (m + 1 - mint));
      }
    }
    dp[i][0] = min(dp[i - 1][0] + 2 * maxt + 1, dp[i - 1][1] + m + 2);
    dp[i][1] = min(dp[i - 1][1] + 2 * (m + 1 - mint) + 1, dp[i - 1][0] + m + 2);
  }
  if (maxf == -1)
    printf("0\n");
  else
    printf("%d\n", maxsum);
  return 0;
}
