#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long mod) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}
long long gcd(long long a, long long b) {
  while (b) b ^= a ^= b ^= a %= b;
  return a;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int dp[30][2] = {0};
  int mat[30][200];
  for (int i = 1; i <= n; i++) {
    string str = "";
    cin >> str;
    for (int j = 0; j < m + 2; j++) {
      if (str[j] == '0')
        mat[i][j] = 0;
      else
        mat[i][j] = 1;
    }
  }
  int temp = 0;
  for (int i = 1; i <= n; i++) {
    int f = 0;
    for (int j = 1; j <= m; j++) {
      if (mat[i][j]) {
        f = 1;
        break;
      }
    }
    if (f) break;
    temp = i;
  }
  int gr = n;
  n = n - temp;
  if (n == 0) {
    cout << 0 << endl;
    return 0;
  }
  int res = 1e9;
  for (int i = 0; i < (1 << (n - 1)); i++) {
    int pos = 0;
    int ans = 0;
    for (int j = 1; j <= m; j++) {
      if (mat[gr][j]) pos = j;
    }
    ans = pos;
    for (int j = 0, l = gr - 1; j < (n - 1); j++, l--) {
      if (i & (1 << j)) {
        ans += m + 1 - pos + 1;
        pos = m + 1;
        for (int k = m; k >= 1; k--) {
          if (mat[l][k]) {
            pos = k;
          }
        }
        ans += m + 1 - pos;
      } else {
        ans += pos + 1;
        pos = 0;
        for (int k = 1; k <= m; k++) {
          if (mat[l][k]) pos = k;
        }
        ans += pos;
      }
    }
    res = min(res, ans);
  }
  cout << res << endl;
  return 0;
}
