#include <bits/stdc++.h>
using namespace std;
int N, M, dp[16][2], up[16];
char gr[16][105];
int solve(int r, int fl) {
  int &res = dp[r][fl];
  if (res != -1) return res;
  res = 0;
  int mi = M, mx = 0;
  for (int i = 0; i <= M; i++)
    if (gr[r][i] == '1') {
      mi = min(mi, i);
      mx = max(mx, i);
    }
  if (!fl) {
    res += mx;
    if (up[r + 1])
      res += min(mx + 1 + solve(r + 1, 0), M - mx + 1 + solve(r + 1, 1));
  } else {
    res += (M - mi);
    if (up[r + 1])
      res += min(M - mi + 1 + solve(r + 1, 1), mi + 1 + solve(r + 1, 0));
  }
  return res;
}
int main() {
  scanf("%d", &N);
  scanf("%d", &M);
  M = M + 1;
  for (int i = N - 1; i >= 0; i--) {
    scanf("%s", gr[i]);
    for (int j = 0; j <= M; j++)
      if (gr[i][j] == '1') up[i] = 1;
    up[i] |= up[i + 1];
  }
  memset(dp, -1, sizeof(dp));
  printf("%d\n", solve(0, 0));
}
