#include <bits/stdc++.h>
using namespace std;
string a[100005];
long long l[100005], r[100005], dp[100005][2];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long TESTS = 1;
  while (TESTS--) {
    long long n, m;
    cin >> n >> m;
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      size_t f = a[i].find('1');
      size_t rf = a[i].rfind('1');
      if (f == string::npos)
        l[i] = m + 1, r[i] = 0;
      else
        l[i] = f, r[i] = rf;
    }
    reverse(r, r + n), reverse(l, l + n);
    long long flag = 0;
    for (long long i = n - 1; i >= 0; i--)
      if (r[i] != 0) {
        n = i + 1;
        flag = 1;
        break;
      }
    if (!flag) n = 1;
    dp[0][0] = r[0], dp[0][1] = 1000000007;
    for (long long i = 1; i < n; i++) {
      dp[i][0] =
          min(dp[i - 1][0] + r[i - 1], dp[i - 1][1] + l[i - 1]) + r[i] + 1;
      dp[i][1] = min(dp[i - 1][0] + m + 1 - r[i - 1],
                     dp[i - 1][1] + m + 1 - l[i - 1]) +
                 m + 1 - l[i] + 1;
    }
    cout << min(dp[n - 1][0], dp[n - 1][1]);
  }
  return 0;
}
