#include <bits/stdc++.h>
using namespace std;
long long M = 1e9 + 7;
long long powerm(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = (res * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return res % M;
}
long long power(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
long long max3(long long a, long long b, long long c) {
  return max(max(a, b), c);
}
long long gcd(long long a, long long b) {
  if (b > a) return gcd(b, a);
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  string s[n + 1];
  for (int i = n; i >= 1; i--) cin >> s[i];
  vector<pair<int, int> > v(n + 1);
  set<int> st;
  for (int i = 1; i <= n; i++) {
    int L = 0, R = m + 1;
    while (L < m + 2 && s[i][L] == '0') L++;
    while (R >= 0 && s[i][R] == '0') R--;
    if (L == m + 2) L = m + 1;
    if (R == -1) R = 0, st.insert(i);
    v[i].first = L;
    v[i].second = R;
    if (i == 1) v[i].first = R;
  }
  int dp[n + 1][2];
  dp[0][0] = dp[0][1] = 0;
  dp[1][0] = v[1].second;
  dp[1][1] = v[1].second;
  for (int i = 2; i <= n; i++) {
    int g1 = dp[i - 1][0] + v[i - 1].second + 1 + v[i].second;
    int g2 = dp[i - 1][1] + v[i - 1].first + 1 + v[i].second;
    dp[i][0] = min(g1, g2);
    g1 = dp[i - 1][0] + m + 2 - v[i - 1].second + m + 1 - v[i].first;
    g2 = dp[i - 1][1] + m + 2 - v[i - 1].first + m + 1 - v[i].first;
    dp[i][1] = min(g1, g2);
  }
  int id = n;
  while (id >= 0) {
    if (st.find(id) != st.end()) {
      id--;
      continue;
    }
    cout << min(dp[id][0], dp[id][1]);
    return 0;
  }
  return 0;
}
