#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 10;
const int INF = 1000 * 1000 * 1000 + 100;
int dp[2][MAXN];
int l[2][MAXN];
int n, m;
int out, lastFloor;
void smx(int& a, const int& b) {
  if (a < b) a = b;
}
void smn(int& a, const int& b) {
  if (a > b) a = b;
}
void prepare() {
  for (int i = 0; i < MAXN; i++)
    for (int j = 0; j < 2; j++) dp[j][i] = INF;
  out = INF;
}
void check() {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < 2; j++) cout << dp[j][i] << "\t";
    cout << endl;
    for (int j = 0; j < 2; j++) cout << l[j][i] << "\t";
    cout << endl;
    cout << "-------------------------------------------------" << endl;
  }
  cout << endl;
}
void input() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  string s;
  for (int i = n; i >= 1; i--) {
    cin >> s;
    for (int j = 1; j <= m; j++)
      if (s[j] == '1') {
        smx(lastFloor, i);
        smx(l[0][i], j);
        smx(l[1][i], (m - j + 1));
      }
  }
}
void dynamic() {
  if (lastFloor) {
    dp[0][0] = -1;
    for (int i = 1; i < lastFloor; i++)
      for (int j = 0; j < 2; j++) {
        smn(dp[j][i], dp[j][i - 1] + 2 * l[j][i] + 1);
        smn(dp[j][i], dp[(j + 1) % 2][i - 1] + m + 2);
      }
    for (int j = 0; j < 2; j++)
      smn(out, dp[j][lastFloor - 1] + l[j][lastFloor] + 1);
  } else {
    out = 0;
  }
}
void output() { cout << out << endl; }
int main() {
  prepare();
  input();
  dynamic();
  output();
  return 0;
}
