#include <bits/stdc++.h>
using namespace std;
const int N = 30;
int cnt[N], Left[N], Right[N], dp[N][2];
string s[N];
long long power_mod(long long x, long long y) {
  x %= 1000000007;
  long long res = 1;
  while (y) {
    if (y & 1) {
      res = (res * x) % 1000000007;
    }
    y /= 2;
    x = (x * x) % 1000000007;
  }
  return res;
}
void solve() {
  int n, m, total = 0, ans = INT_MAX;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    Left[i] = m + 2;
    for (int j = 1; j <= m; j++) {
      cnt[i] += (s[i][j] == '1');
      if (s[i][j] == '1') {
        Right[i] = j;
        Left[i] = min(Left[i], j);
      }
    }
    total += cnt[i];
  }
  dp[n + 1][0] = -1, dp[n + 1][1] = m;
  int ind = n;
  if (total == 0) {
    ans = 0;
  }
  for (int i = n; i >= 1 && total > 0; i--) {
    dp[i][0] = dp[i + 1][0] + 1;
    dp[i][1] = dp[i + 1][1] + 1;
    if (total - cnt[i] == 0) {
      ans = min(dp[i][0] + Right[i], dp[i][1] + (m + 1 - Left[i]));
      break;
    }
    if (cnt[i]) {
      int left = dp[i][0] + Right[i] * 2;
      int right = dp[i][1] + (m + 1 - Left[i]) * 2;
      int cur_left = dp[i][0];
      int cur_right = dp[i][1];
      dp[i][0] = min(left, cur_right + m + 1);
      dp[i][1] = min(right, cur_left + m + 1);
    } else {
      int cur_left = dp[i][0];
      int cur_right = dp[i][1];
      dp[i][0] = min(dp[i][0], cur_right + m + 1);
      dp[i][1] = min(dp[i][1], cur_left + m + 1);
    }
    total -= cnt[i];
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int tc = 1;
  while (tc--) {
    solve();
  }
  return 0;
}
