#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<string> v(n);
  for (long long i = 0; i < n; i++) cin >> v[i];
  vector<pair<long long, long long> > fs(n, make_pair(0ll, 0ll));
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m + 2; j++)
      if (v[i][j] == '1') {
        if (fs[i].first == 0) fs[i].first = j;
        fs[i].second = j;
      }
  long long ans = 0;
  for (long long i = 0; i < n; i++)
    if (fs[i].first == 0ll && fs[i].second == 0)
      ans--;
    else
      break;
  vector<pair<long long, long long> > p;
  for (long long i = 0; i < n; i++)
    if (fs[i] != make_pair(0ll, 0ll))
      p.emplace_back(fs[i]);
    else
      ans++;
  if (p.size() == 0) {
    cout << 0;
    return;
  }
  vector<pair<long long, long long> > dp(p.size());
  n = p.size();
  dp[n - 1].first = p.back().second * 2;
  dp[n - 1].second = m + 1;
  for (long long i = n - 1; i >= 2; i--) {
    dp[i - 1].first =
        min(dp[i].second + m + 2, dp[i].first + p[i - 1].second * 2 + 1);
    dp[i - 1].second =
        min(dp[i].first + m + 2, dp[i].second + (m - p[i - 1].first) * 2 + 3);
  }
  if (p.size() >= 2)
    ans +=
        min(dp[1].first + 1 + p[0].second, dp[1].second + (m - p[0].first) + 2);
  else
    ans = p[0].second;
  cout << ans;
  p[0];
}
int32_t main() {
  solve();
  return 0;
}
