#include <bits/stdc++.h>
using namespace std;
long long binPow(long long a, long long b, long long Mo) {
  long long ans = 1;
  for (; b; b >>= 1, a = a * a % Mo)
    if (b & 1) ans = ans * a % Mo;
  return ans;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<string> r;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    r.push_back(s);
  }
  vector<string> v;
  for (int i = n - 1; i >= 0; i--) {
    v.push_back(r[i]);
  }
  int cnt[n];
  int lastpos[n];
  int firstpos[n];
  int laston = 0;
  for (int i = 0; i < n; i++) {
    cnt[i] = 0;
    lastpos[i] = 0;
    firstpos[i] = -1;
  }
  m += 2;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (v[i][j] == '1') {
        cnt[i]++;
        lastpos[i] = j;
        if (firstpos[i] == -1) firstpos[i] = j;
        laston = i;
      }
    }
    if (firstpos[i] == -1) firstpos[i] = (m - 1);
  }
  int ans[n + 1][2];
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < 2; j++) {
      ans[i][j] = 0;
    }
  }
  if (n == 1) {
    cout << lastpos[0] << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2; j++) {
      if (i == 0 && j == 1) {
        ans[i][j] = 99999999;
        continue;
      }
      if (i == 0 && j == 0) {
        ans[i][j] = 0;
        continue;
      }
      int val1, val2;
      if (j == 0) {
        val1 = ans[i - 1][0] + lastpos[i - 1] * 2;
        val2 = ans[i - 1][1] + m - 1;
      } else {
        val1 = ans[i - 1][0] + m - 1;
        val2 = ans[i - 1][1] + (m - 1 - firstpos[i - 1]) * 2;
      }
      ans[i][j] = min(val1, val2);
    }
  }
  int v1, v2;
  if (n > 1) {
    ans[n][0] = ans[n - 1][0] + lastpos[n - 1];
    ans[n][1] = ans[n - 1][1] + (m - 1 - firstpos[n - 1]);
  }
  if (laston != n - 1) {
    ans[n][0] = ans[laston][0] + lastpos[laston];
    ans[n][1] = ans[laston][1] + (m - 1 - firstpos[laston]);
  }
  int fin = min(ans[n][0], ans[n][1]);
  fin += laston;
  cout << fin << endl;
  return 0;
}
