#include <bits/stdc++.h>
using namespace std;
template <class t>
t lcm(t a, t b) {
  return ((a * b) / __gcd(a, b));
}
bool isprime(long long num) {
  if (num == 1) return false;
  long long limit = sqrt(num);
  if (num == 2) return true;
  if (num % 2 == 0) return false;
  for (long long i = 3; i <= limit; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }
  return true;
}
long long n, m;
char arr[20][200];
long long counter[20];
long long ans;
long long total;
void bruteForce(long long cur, bool lef, long long moved, long long done) {
  if (cur == n) {
    ans = min(ans, moved);
    return;
  }
  if (done == total) {
    ans = min(ans, moved);
    return;
  }
  long long don = done;
  done += counter[cur];
  if (lef) {
    if (counter[cur] == 0) {
      if (cur != (n - 1)) {
        bruteForce(cur + 1, true, moved + 1, done);
        bruteForce(cur + 1, false, moved + m + 2, done);
      } else {
        bruteForce(cur + 1, true, moved, done);
        bruteForce(cur + 1, false, moved, done);
      }
    } else {
      long long maxi = 0;
      for (long long j = 0; j < (m + 2); j++) {
        if (arr[cur][j] == '1') maxi = j;
      }
      if (cur != (n - 1) && done != total) {
        bruteForce(cur + 1, true, moved + 2 * maxi + 1, done);
        bruteForce(cur + 1, false, moved + m + 2, done);
      } else {
        bruteForce(cur + 1, true, moved + maxi, done);
        bruteForce(cur + 1, false, moved + m + 1, done);
      }
    }
  } else {
    if (counter[cur] == 0) {
      if (cur != (n - 1)) {
        bruteForce(cur + 1, true, moved + m + 2, done);
        bruteForce(cur + 1, false, moved + 1, done);
      } else {
        bruteForce(cur + 1, true, moved, done);
        bruteForce(cur + 1, false, moved, done);
      }
    } else {
      long long maxi = 0;
      for (long long j = (m + 1); j >= 0; j--) {
        if (arr[cur][j] == '1') maxi = j;
      }
      maxi = m + 1 - maxi;
      if (cur != (n - 1) && done != total) {
        bruteForce(cur + 1, true, moved + m + 2, done);
        bruteForce(cur + 1, false, moved + 2 * maxi + 1, done);
      } else {
        bruteForce(cur + 1, true, moved + m + 1, done);
        bruteForce(cur + 1, false, moved + maxi, done);
      }
    }
  }
}
int main() {
  ans = LONG_LONG_MAX;
  cin >> n >> m;
  for (long long i = (n - 1); i >= 0; i--) {
    counter[i] = 0;
    for (long long j = 0; j < (m + 2); j++) {
      cin >> arr[i][j];
      if (arr[i][j] == '1') {
        total++;
        counter[i]++;
      }
    }
  }
  bruteForce(0, true, 0, 0);
  cout << ans;
  return 0;
}
