#include <bits/stdc++.h>
using namespace std;
void Fastio() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long fact[1000006], inv[1000006];
long long fp(long long base, long long exp) {
  if (exp == 0) return 1;
  long long ans = fp(base, exp / 2);
  ans = (ans * ans) % 998244353;
  if (exp % 2 != 0) ans = (ans * (base % 998244353)) % 998244353;
  return ans;
}
void calcFacAndInv(long long n) {
  fact[0] = inv[0] = 1;
  for (long long i = 1; i <= n; i++) {
    fact[i] = (i * fact[i - 1]);
    inv[i] = fp(fact[i], 998244353 - 2);
  }
}
long long ncr(long long n, long long r) {
  return ((fact[n] * inv[r]) % 998244353 * inv[n - r]) % 998244353;
}
long long npr(long long n, long long r) {
  return (fact[n] * inv[n - r]) % 998244353;
}
long double NCR(int n, int m) {
  long double res = 1;
  m = min(m, n - m);
  for (int i = 0; i < m; i++) {
    res = res * (n - i) / (i + 1);
  }
  return res;
}
long long tol(string a) {
  long long res = 0;
  while (a[0] == '0') {
    a.erase(a.begin());
  }
  if (a == "") a = "0";
  for (int i = 0; i < a.size(); i++) {
    res *= 10;
    res += a[i] - '0';
  }
  return res;
}
int n, m, sum;
char arr[20][105];
int solve(int x, int y, int c) {
  if (y == 0) {
    int pos = 0;
    for (int i = 0; i < m; i++) {
      if (arr[x][i] == '1') {
        c++;
        pos = i;
      }
    }
    if (c == sum) return pos;
    return min(solve(x - 1, 0, c) + 2 * pos + 1, solve(x - 1, m - 1, c) + m);
  }
  int pos = m - 1;
  for (int i = m - 2; i > 0; i--) {
    if (arr[x][i] == '1') {
      c++;
      pos = i;
    }
  }
  if (c == sum) return m - pos - 1;
  return min(solve(x - 1, m - 1, c) + 2 * (m - pos - 1) + 1,
             solve(x - 1, 0, c) + m);
}
int main() {
  Fastio();
  cin >> n >> m;
  m += 2;
  for (int i = 0; i < n; i++) {
    for (int y = 0; y < m; y++) {
      cin >> arr[i][y];
      sum += arr[i][y] - '0';
    }
  }
  cout << solve(n - 1, 0, 0) << '\n';
  return 0;
}
