#include <bits/stdc++.h>
using namespace std;
int get_min_moves(string a[], int b[], int g[], int n, int m, int total) {
  int ans = 0;
  int sc = 0, end = m + 1;
  for (int i = 0; i < n; ++i) {
    int ok = -1;
    if (g[i] == 0) {
      ok = 0;
      sc = 0;
    } else {
      ok = 1;
      sc = end;
    }
    int counter = 0;
    while (b[i] != 0) {
      if (ok == 0) {
        if (a[i][sc] == '1') {
          --b[i];
          --total;
        }
        if (b[i] == 0 || total == 0) break;
        ++sc;
        ++counter;
      } else {
        if (a[i][sc] == '1') {
          --b[i];
          --total;
        }
        if (b[i] == 0 || total == 0) break;
        --sc;
        ++counter;
      }
    }
    ans += counter;
    if (total == 0) break;
    if (i == n - 1) break;
    ++ans;
    if (g[i + 1] != g[i]) {
      ans -= counter;
      ans += end;
    } else if (g[i + 1] == g[i])
      ans += counter;
  }
  return ans;
}
int main() {
  int n, m;
  cin >> n >> m;
  string a[n];
  for (int i = 0; i < n; ++i) cin >> a[i];
  reverse(a, a + n);
  int b[n];
  for (int i = 0; i < n; ++i) {
    int counter = 0;
    for (auto &x : a[i]) {
      if (x == '1') ++counter;
    }
    b[i] = counter;
  }
  int f[n], total = 0;
  for (int i = 0; i < n; ++i) {
    f[i] = b[i];
    total += b[i];
  }
  int MIN = 1e9 + 7;
  for (int i = 0; i < (1 << n); ++i) {
    int g[n];
    for (int j = 0; j < n; ++j) {
      if ((i & (1 << j)) != 0)
        g[j] = 1;
      else
        g[j] = 0;
    }
    if (g[0] != 0) continue;
    MIN = min(MIN, get_min_moves(a, f, g, n, m, total));
    for (int k = 0; k < n; ++k) f[k] = b[k];
  }
  cout << MIN << endl;
  return 0;
}
