#include <bits/stdc++.h>
using namespace std;
int rr[20], ll[20], d[20][4];
int main() {
  int n, m, i;
  string a[20];
  cin >> n >> m;
  for (int i = n; i >= 1; i--) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == '1') {
        rr[i] = (m - j + 1) * 2;
        break;
      }
    }
    for (int j = m; j >= 1; j--) {
      if (a[i][j] == '1') {
        ll[i] = j * 2;
        break;
      }
    }
  }
  int k = 0;
  for (i = n; i >= 1; i--)
    if (ll[i]) {
      k = i;
      break;
    }
  if (k == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (k == 1) {
    cout << ll[1] / 2 << endl;
    return 0;
  }
  n = k;
  d[1][0] = ll[1];
  d[1][1] = m + 1;
  for (int i = 2; i < n; i++) {
    d[i][0] = 1 + min(d[i - 1][0] + ll[i], d[i - 1][1] + m + 1);
    d[i][1] = 1 + min(d[i - 1][1] + rr[i], d[i - 1][0] + m + 1);
  }
  d[n][1] = d[n - 1][1] + rr[n] / 2 + 1;
  d[n][0] = d[n - 1][0] + ll[n] / 2 + 1;
  cout << min(d[n][1], d[n][0]) << endl;
}
