#include <bits/stdc++.h>
using namespace std;
int check(vector<string> v, int n, int m) {
  int ind = 0;
  for (int i = 0; i < n; i++) {
    for (int k = 0; k < m; k++) {
      if (v[i][k] == '1') {
        ind = i;
        break;
      }
    }
  }
  return ind;
}
int solve(vector<string>& v, int i, int dp[][3], int n, int m, int side) {
  if (i == n - 1) {
    if (side == 0) {
      int step = 0;
      for (int k = 0; k < m; k++) {
        if (v[i][k] == '1') step = k;
      }
      return step;
    } else {
      int step = 0;
      for (int k = m - 1; k >= 0; k--) {
        if (v[i][k] == '1') step = m - k - 1;
      }
      return step;
    }
  }
  if (dp[i][side] != -1) return dp[i][side];
  if (side == 0) {
    int step = 0;
    for (int k = 0; k < m; k++) {
      if (v[i][k] == '1') step = k;
    }
    int left_cost = 2 * step + 1 + solve(v, i + 1, dp, n, m, 0);
    int right_cost = m + solve(v, i + 1, dp, n, m, 1);
    return dp[i][side] = min(right_cost, left_cost);
  } else {
    int step = 0;
    for (int k = m - 1; k >= 0; k--) {
      if (v[i][k] == '1') step = m - k - 1;
    }
    int right_cost = 2 * step + 1 + solve(v, i + 1, dp, n, m, 1);
    int left_cost = m + solve(v, i + 1, dp, n, m, 0);
    return dp[i][side] = min(right_cost, left_cost);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<string> v;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    v.push_back(s);
  }
  reverse(v.begin(), v.end());
  int ind = check(v, n, m + 2);
  int dp[n + 1][3];
  memset(dp, -1, sizeof(dp));
  cout << solve(v, 0, dp, ind + 1, m + 2, 0);
  return 0;
}
