#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int n, m;
bool a[maxn][maxn];
int l[maxn], r[maxn];
bool lit[maxn];
int dp[maxn][2];
char c;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = n; i >= 1; i--) {
    l[i] = r[i] = 0;
    c = getchar();
    lit[i] = false;
    for (int j = 0; j <= m + 1; j++) {
      c = getchar();
      if (c == '0')
        a[i][j] = false;
      else {
        a[i][j] = true;
        if (l[i] == 0) l[i] = j;
        r[i] = j;
        lit[i] = true;
      }
    }
  }
  while (n > 0 && !lit[n]) n--;
  if (n == 0) {
    printf("%d\n", 0);
    return 0;
  } else if (n == 1) {
    printf("%d\n", r[n]);
    return 0;
  }
  dp[1][0] = lit[1] ? r[1] * 2 : 0;
  dp[1][1] = m + 1;
  for (int i = 2; i < n; i++) {
    dp[i][0] = min(dp[i - 1][0] + 1 + (lit[i] ? (r[i] * 2) : 0),
                   dp[i - 1][1] + 1 + m + 1);
    dp[i][1] = min(dp[i - 1][1] + 1 + (lit[i] ? ((m + 1 - l[i]) * 2) : 0),
                   dp[i - 1][0] + 1 + m + 1);
  }
  printf("%d\n",
         min(dp[n - 1][0] + 1 + r[n], dp[n - 1][1] + 1 + (m + 1 - l[n])));
  return 0;
}
