#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, m;
  cin >> n >> m;
  long long dp[n][2];
  vector<long long> flag(n, 0);
  long long pos[n][2];
  string ss[n];
  long long last = 0;
  for (long long i = 0; i < n; i++) cin >> ss[i];
  for (long long i = 0; i < n; i++) pos[i][0] = 1213;
  for (long long i = 0; i < n; i++) {
    string s = ss[n - i - 1];
    for (long long j = 1; j < (long long)s.length() - 1; j++)
      if (s[j] == '1') {
        flag[i] = 1;
        last = i;
        break;
      }
    for (long long j = 1; j < (long long)s.length() - 1; j++)
      if (s[j] == '1') pos[i][1] = j, pos[i][0] = min(pos[i][0], j);
  }
  if (last != 0) {
    if (flag[0] == 0)
      dp[0][0] = 0, dp[0][1] = 0;
    else
      dp[0][0] = 2 * pos[0][1], dp[0][1] = m + 1;
  } else {
    if (flag[0] == 0)
      dp[0][0] = dp[0][1] = 0;
    else
      dp[0][0] = dp[0][1] = pos[0][1];
  }
  for (long long i = 1; i <= last; i++) {
    if (i != last) {
      if (flag[i]) {
        dp[i][0] =
            min(dp[i - 1][0] + (2 * pos[i][1]), dp[i - 1][1] + m + 1) + 1;
        dp[i][1] = min(dp[i - 1][1] + (m + 1 + m + 1 - 2 * pos[i][0]),
                       dp[i - 1][0] + m + 1) +
                   1;
      } else {
        dp[i][0] = dp[i - 1][0] + 1;
        dp[i][1] = dp[i - 1][1] + 1;
      }
    } else {
      dp[i][0] = min(dp[i - 1][0] + pos[i][1] + 1,
                     dp[i - 1][1] + m + 1 - pos[i][0] + 1);
      dp[i][1] = min(dp[i - 1][0] + pos[i][1] + 1,
                     dp[i - 1][1] + 1 + m + 1 - pos[i][0]);
    }
  }
  cout << min(dp[last][0], dp[last][1]);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) solve();
}
