#include <bits/stdc++.h>
using namespace std;
int N, M;
int nums[20][120];
int go[20][2];
int high_to_go = 0;
vector<vector<int>> cal = vector<vector<int>>(20, vector<int>(2, -1));
void read() {
  cin >> N >> M;
  char c;
  high_to_go = N;
  for (int i = 0; i < N; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < M + 2; j++) nums[i][j] = str[j] - '0';
  }
}
int calculate(int fl, int stair) {
  if (high_to_go == N) return 0;
  if (cal[fl][stair] != -1) return cal[fl][stair];
  cal[fl][stair] = 0;
  if (fl == high_to_go) {
    cal[fl][stair] = go[high_to_go][stair];
    return cal[fl][stair];
  }
  cal[fl][stair] = std::min(calculate(fl - 1, stair) + 2 * go[fl][stair] + 1,
                            calculate(fl - 1, 1 - stair) + M + 2);
  return cal[fl][stair];
}
void calculate() {
  for (int i = 0; i < N; i++) {
    bool first = false;
    for (int j = 1; j <= M; j++) {
      if (first == false && nums[i][j] == 1) {
        go[i][1] = M + 1 - j;
        first = true;
      }
      if (nums[i][j] == 1) go[i][0] = j;
    }
    if (high_to_go == N && (go[i][1] >= 1 || go[i][0] >= 1)) high_to_go = i;
  }
}
int main() {
  read();
  calculate();
  cout << calculate(N - 1, 0) << endl;
  return 0;
}
