#include <bits/stdc++.h>
using namespace std;
int n, m, last;
int lr[2][20];
int memo[20][2];
int dp(int f, int d) {
  if (f == last) return lr[d][f];
  if (memo[f][d] != -1) return memo[f][d];
  return memo[f][d] =
             min(dp(f + 1, d) + 2 * lr[d][f] + 1, dp(f + 1, !d) + m + 1 + 1);
}
int main() {
  scanf("%d%d", &n, &m);
  last = 1;
  for (int i = 1; i <= n + 1; ++i) lr[0][i] = 0, lr[1][i] = 0;
  int arr[n + 5][m + 5];
  for (int i = n; i; --i) {
    bool flag = false;
    for (int j = 0; j <= m + 1; ++j) {
      char c;
      scanf(" %c", &c);
      if ((int)c - '0' == 1) {
        flag = true;
        lr[0][i] = j;
        if (lr[1][i] == 0) lr[1][i] = m + 1 - j;
      }
    }
    if (flag) last = max(last, i);
  }
  memset(memo, -1, sizeof memo);
  printf("%d\n", dp(1, 0));
}
