#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x_(m + 2);
  vector<vector<int> > arr(n, x_);
  for (int i = n - 1; i >= 0; i--) {
    string s;
    cin >> s;
    for (int j = 0; j < m + 2; j++) {
      arr[i][j] = s[j] - '0';
    }
  }
  vector<int> l(n), r(n, m + 1);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m + 2; j++) {
      if (arr[i][j]) {
        l[i] = j;
      }
    }
    for (int j = m + 1; j >= 0; j--) {
      if (arr[i][j]) {
        r[i] = j;
      }
    }
  }
  int nn = -1;
  for (int i = 0; i < n; i++) {
    if (l[i]) {
      nn = i;
    }
  }
  int types = pow(2, n);
  int mintime = 1e9;
  for (int i = 0; i < types; i++) {
    int time = 0;
    int now = 0;
    for (int j = 0; j <= nn; j++) {
      if (now == 0) {
        time += (l[j]);
      } else {
        time += ((m + 1) - r[j]);
      }
      if (j != nn) {
        int next = ((1 << j) & i);
        if (now == 0) {
          if (next == 0) {
            time += (l[j] + 1);
          } else {
            time += (m + 1 - l[j] + 1);
          }
        } else {
          if (next == 0) {
            time += (r[j] + 1);
          } else {
            time += (m + 1 - r[j] + 1);
          }
        }
        now = (next > 0);
      }
    }
    mintime = min(mintime, time);
  }
  cout << mintime << endl;
}
