#include <bits/stdc++.h>
using namespace std;
int find(int* l, int* r, int floor, int m, int n, string direction) {
  if (floor == n - 1) {
    if (direction == "left") {
      return l[floor];
    }
    return r[floor];
  }
  if (direction == "left") {
    return min(2 * l[floor] + 1 + find(l, r, floor + 1, m, n, "left"),
               m + 2 + find(l, r, floor + 1, m, n, "right"));
  } else if (direction == "right") {
    return min(2 * r[floor] + 1 + find(l, r, floor + 1, m, n, "right"),
               m + 2 + find(l, r, floor + 1, m, n, "left"));
  }
  return 0;
}
int main() {
  int n, m;
  cin >> n >> m;
  int** arr = new int*[n];
  for (int i = 0; i < n; i++) {
    arr[i] = new int[m + 2];
    string s;
    cin >> s;
    for (int j = 0; j < m + 2; j++) {
      arr[i][j] = s[j] - '0';
    }
  }
  int* l = new int[n];
  int* r = new int[n];
  int k = 0;
  for (int i = n - 1; i >= 0; i--) {
    l[k] = 0;
    for (int j = 1; j <= m; j++) {
      if (arr[i][j] == 1) {
        l[k] = j;
      }
    }
    k++;
  }
  k = 0;
  for (int i = n - 1; i >= 0; i--) {
    int p = 1;
    r[k] = 0;
    for (int j = m; j >= 1; j--) {
      if (arr[i][j] == 1) {
        r[k] = p;
      }
      p++;
    }
    k++;
  }
  int count = n, flag = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= m + 1; j++) {
      if (arr[i][j] == 1) {
        flag = 1;
        break;
      }
    }
    if (flag == 1) {
      break;
    }
    count--;
  }
  if (count == 0) {
    cout << 0;
    return 0;
  }
  cout << (find(l, r, 0, m, count, "left"));
  return 0;
}
