#include <bits/stdc++.h>
using namespace std;
void chmax(int& a, int b) { a = max(a, b); }
void chmin(int& a, int b) { a = min(a, b); }
class BSagheerTheHausmeister {
 public:
  int N, M;
  vector<string> S;
  vector<int> mx, mn;
  vector<vector<int>> dp;
  void input(std::istream& in, std::ostream& out) {
    ios_base::sync_with_stdio(false);
    in >> N >> M;
    M += 2;
    S = vector<string>(N);
    mx = vector<int>(N, 0);
    mn = vector<int>(N, M - 1);
    dp = vector<vector<int>>(N + 1, vector<int>(2, 100010));
    for (int(i) = (0); (i) < (N); ++(i)) in >> S[N - i - 1];
  }
  void solve(std::istream& in, std::ostream& out) {
    input(in, out);
    int F = 0;
    for (int(i) = (0); (i) < (N); ++(i)) {
      for (int(j) = (0); (j) < (M); ++(j)) {
        if (S[i][j] == '1') chmax(mx[i], j), chmin(mn[i], j);
      }
      if (mx[i]) F = i + 1;
    }
    if (F == 0) {
      out << "0\n";
      return;
    }
    if (F == 1) {
      out << mx[0] << '\n';
      return;
    }
    dp[0][0] = dp[0][1] = 0;
    for (int(i) = (0); (i) < (F); ++(i)) {
      if (i < F - 1) {
        chmin(dp[i + 1][0], dp[i][0] + 2 * mx[i] + 1);
        chmin(dp[i + 1][1], dp[i][0] + M);
        if (i) {
          chmin(dp[i + 1][0], dp[i][1] + M);
          chmin(dp[i + 1][1], dp[i][1] + 2 * (M - mn[i]) - 1);
        }
      } else {
        chmin(dp[i + 1][0], dp[i][0] + mx[i]);
        chmin(dp[i + 1][1], dp[i][1] + M - mn[i] - 1);
      }
    }
    out << min(dp[F][0], dp[F][1]) << '\n';
  }
};
int main() {
  BSagheerTheHausmeister solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
