#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m;
  cin >> n >> m;
  vector<string> v, l, b;
  for (long long int i = 0; i < n; i++) {
    string s;
    cin >> s;
    v.push_back(s);
  }
  long long int c = -1;
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < m + 2; j++) {
      if (v[i][j] == '1') {
        c = i;
        break;
      }
    }
    if (c != -1) break;
  }
  if (c == -1) {
    cout << 0;
    return 0;
  }
  for (long long int i = c; i < n; i++) {
    l.push_back(v[i]);
  }
  n = n - c;
  long long int f = n;
  for (long long int i = n - 1; i >= 0; i--) {
    for (long long int j = 0; j < m + 2; j++) {
      if (l[i][j] == '1') {
        f = i;
        break;
      }
    }
    if (f != n) break;
  }
  if (f == n) {
    cout << 0;
    return 0;
  }
  for (long long int i = 0; i <= f; i++) {
    b.push_back(l[i]);
  }
  long long int g = n;
  n = f + 1;
  long long int k = 0;
  for (long long int i = m + 1; i >= 0; i--) {
    if (b[n - 1][i] == '1') {
      k = i;
      break;
    }
  }
  if (n == 1) {
    cout << k + g - 1 - f;
  } else {
    long long int a[n][m + 2];
    memset(a, 0, sizeof(a));
    a[n - 1][0] = 2 * k;
    a[n - 1][m + 1] = m + 1;
    a[n - 2][0] = 2 * k + 1;
    a[n - 2][m + 1] = m + 2;
    for (long long int i = n - 2; i > 0; i--) {
      long long int x1 = 0;
      long long int x2 = m + 1;
      for (long long int j = m + 1; j >= 0; j--) {
        if (b[i][j] == '1') {
          x1 = j;
          break;
        }
      }
      for (long long int j = 0; j <= (m + 1); j++) {
        if (b[i][j] == '1') {
          x2 = j;
          break;
        }
      }
      a[i - 1][0] = 1 + min(m + 1 + a[i][m + 1], a[i][0] + 2 * x1);
      a[i - 1][m + 1] =
          1 + min(m + 1 + a[i][0], a[i][m + 1] + 2 * (m + 1 - x2));
    }
    long long int x1 = 0;
    long long int x2 = m + 1;
    for (long long int j = m + 1; j >= 0; j--) {
      if (b[0][j] == '1') {
        x1 = j;
        break;
      }
    }
    for (long long int j = 0; j <= (m + 1); j++) {
      if (b[0][j] == '1') {
        x2 = j;
        break;
      }
    }
    cout << min(a[0][0] + x1, a[0][m + 1] + m + 1 - x2) + g - 1 - f << endl;
  }
}
