#include <bits/stdc++.h>
using namespace std;
int i, j, n, m, rs = 1e9 + 69 * 69, last, mask, fst[20], snd[20];
bool has[20];
string a[20];
int solve(int mask) {
  int ans = 0, now = 0;
  for (int i = 0; i < n - 1; ++i)
    if (mask & (1 << i)) {
      if (!now)
        ans += m + 1, now = 1;
      else if (has[i])
        ans += 2 * (m + 1 - fst[i]);
    } else {
      if (now)
        ans += m + 1, now = 0;
      else if (has[i])
        ans += 2 * snd[i];
    }
  i = n - 1;
  if (!now)
    ans += snd[i];
  else
    ans += m + 1 - fst[i];
  return ans + n - 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (i = 0; i < n; ++i) cin >> a[n - i - 1];
  for (i = 0; i < n; ++i) {
    for (j = 0; j < a[i].size(); ++j) {
      if (a[i][j] == '0') continue;
      snd[i] = j;
      has[i] = 1;
    }
    for (j = a[i].size() - 1; j >= 0; --j) {
      if (a[i][j] == '0') continue;
      fst[i] = j;
    }
    if (has[i]) last = i;
  }
  n = last + 1;
  for (mask = 0; mask < (1 << n); ++mask) rs = min(rs, solve(mask));
  cout << rs << '\n';
  return 0;
}
