#include <bits/stdc++.h>
using namespace std;
const int OO = (int)1e9;
const double eps = (1e-10);
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
int dcmp(double x, double y) { return fabs(x - y) <= eps ? 0 : x < y ? -1 : 1; }
double dist(double x1, double y1, double x2, double y2) {
  return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
unsigned long long power(unsigned long long x, int y) {
  unsigned long long temp;
  if (y == 0) return 1;
  temp = power(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else
    return x * temp * temp;
}
int main() {
  ios::sync_with_stdio(false);
  cout.precision(10);
  int n, m;
  cin >> n >> m;
  char arr[n][m + 2];
  vector<pair<int, int>> z(n);
  int floor = -1;
  bool flag = false;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m + 2; ++j) {
      cin >> arr[i][j];
      if (arr[i][j] == '1' && !flag) {
        floor = i;
        flag = true;
      }
    }
  }
  if (!flag) {
    cout << 0;
    return 0;
  }
  for (int i = 0; i < n; ++i) {
    int x = -1, y = -1;
    for (int j = 0; j < m + 2; ++j) {
      if (arr[i][j] == '1') {
        x = j;
        break;
      }
    }
    for (int j = m + 1; j >= 0; --j) {
      if (arr[i][j] == '1') {
        y = j;
        break;
      }
    }
    z[i] = make_pair(x, y);
  }
  int ans = OO, pos = z[n - 1].second;
  if (pos == -1) pos = 0;
  int Pos = pos;
  for (int i = 0; i <= (1 << (n - 1)) - 1; ++i) {
    int d = Pos, pos = Pos;
    for (int j = n - 1, k = 0; j >= floor + 1; --j, ++k) {
      bool to = i & (1 << k);
      if (!to) {
        d += pos + 1;
        pos = 0;
        if (z[j - 1].second == -1) continue;
        pos = z[j - 1].second;
        d += z[j - 1].second;
      } else {
        d += m + 1 - pos + 1;
        pos = m + 1;
        if (z[j - 1].first == -1) continue;
        pos = z[j - 1].first;
        d += m + 1 - z[j - 1].first;
      }
    }
    ans = min(ans, d);
  }
  cout << ans;
  return 0;
}
