#include <bits/stdc++.h>
using namespace std;
string s;
int l[20], r[20];
const int inf = 0x3f3f3f3f;
int main() {
  int n, m;
  cin >> n >> m;
  int pos = inf;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    int len = s.length();
    for (int j = 0; j < len; j++) {
      if (s[j] == '1') {
        if (!l[i]) l[i] = j + 1;
        r[i] = j + 1;
        pos = min(pos, i);
      }
    }
  }
  int a1, a2, b1, b2;
  int x = 0, y = 0;
  int ans;
  if (pos == n) {
    if (r[n])
      a1 = r[n] - 1;
    else
      a1 = 0;
    cout << a1 << endl;
    return 0;
  }
  for (int i = n; i >= pos; i--) {
    if (i != n) {
      x++;
      y++;
    }
    if (i == pos) {
      if (r[i])
        a1 = x + r[i] - 1;
      else
        a1 = x;
      if (l[i])
        b1 = y + m + 2 - l[i];
      else
        b1 = y;
      ans = min(a1, b1);
    } else if (i == n) {
      if (r[i])
        x = (r[i] - 1) * 2;
      else
        x = 0;
      y = m + 1;
    } else {
      a1 = x + m + 1;
      if (r[i])
        a2 = x + (r[i] - 1) * 2;
      else
        a2 = x;
      b1 = y + m + 1;
      if (l[i])
        b2 = y + (m + 2 - l[i]) * 2;
      else
        b2 = y;
      x = min(b1, a2);
      y = min(a1, b2);
    }
  }
  cout << ans << endl;
  return 0;
}
