#include <bits/stdc++.h>
using namespace std;
char arr[1005][1005];
long long int dp[1005][3];
long long int topmostfloor = 0, r, c;
long long int solve(long long int floor, long long int side) {
  if (dp[floor][side] != -1) return dp[floor][side];
  if (floor == topmostfloor - 1) return 0;
  long long int firstlight, lastlight, k1 = 1e15, k2 = 1e15, k3 = 1e15,
                                       k4 = 1e15, flag2 = 0;
  for (long long int i = 1; i <= c; i++) {
    if (arr[floor][i] == '1') {
      firstlight = i;
      flag2 = 1;
      break;
    }
  }
  for (long long int i = 1; i <= c; i++) {
    if (arr[floor][i] == '1') {
      flag2 = 1;
      lastlight = i;
    }
  }
  if (side == -1) {
    if (flag2 == 0) {
      k2 = 1 + solve(floor - 1, -1);
    } else if (floor == topmostfloor) {
      k1 = lastlight - 1 + solve(floor - 1, 1);
    } else if (floor != topmostfloor) {
      k1 = c + solve(floor - 1, 1);
      k2 = 2 * lastlight - 1 + solve(floor - 1, -1);
    }
  } else {
    if (flag2 == 0) {
      k3 = 1 + solve(floor - 1, 1);
    } else if (floor == topmostfloor) {
      k4 = c - firstlight + solve(floor - 1, -1);
    } else if (floor != topmostfloor) {
      k3 = 2 * (c - firstlight) + 1 + solve(floor - 1, 1);
      k4 = c + solve(floor - 1, -1);
    }
  }
  dp[floor][side] = min(k1, min(k2, min(k3, k4)));
  return dp[floor][side];
}
int main() {
  long long int flag = 0;
  scanf("%lld%lld", &r, &c);
  c += 2;
  for (long long int i = 1; i <= r; i++) {
    for (long long int j = 1; j <= c; j++) {
      cin >> arr[i][j];
      if (arr[i][j] == '1' && flag == 0) {
        topmostfloor = i;
        flag = 1;
      }
    }
  }
  if (topmostfloor == 0) {
    cout << "0";
    return 0;
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(r, -1);
}
