#include <bits/stdc++.h>
using namespace std;
int cost[15][2][2];
string a[15];
int dp[15][2], topFloor;
int memoize(int floor, int dir) {
  if (floor > topFloor) {
    return 0;
  }
  if (floor == topFloor) {
    return cost[floor][dir][dir];
  }
  if (dp[floor][dir] != -1) return dp[floor][dir];
  int ans = 2000000000;
  ans = min(memoize(floor + 1, dir ^ 1) + 1 + cost[floor][dir][dir ^ 1],
            memoize(floor + 1, dir) + 1 + cost[floor][dir][dir]);
  return dp[floor][dir] = ans;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  reverse(a, a + n);
  m += 2;
  topFloor = -1;
  for (int i = 0; i < n; i++) {
    bool present = false;
    int mini = m + 5, maxi = -1;
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '1') {
        present = true;
        mini = min(mini, j);
        maxi = max(maxi, j);
      }
    }
    if (present) {
      topFloor = i;
      cost[i][0][0] = maxi * 2;
      cost[i][1][1] = (m - 1 - mini) * 2;
    }
    cost[i][0][1] = m - 1;
    cost[i][1][0] = m - 1;
  }
  if (topFloor != -1) {
    cost[topFloor][0][0] /= 2;
    cost[topFloor][1][1] /= 2;
  }
  memset(dp, -1, sizeof(dp));
  int ans = memoize(0, 0);
  printf("%d\n", ans);
  return 0;
}
