#include <bits/stdc++.h>
using namespace std;
const int maxn = 205;
int n, m, ans = 1e5, k;
int a[maxn], l[maxn], r[maxn];
char s[maxn];
void dfs(int x, int b, int sum) {
  if (x == k) {
    if (!b) {
      sum += r[x];
      ans = min(ans, sum);
    } else {
      sum += l[x];
      ans = min(ans, sum);
    }
    return;
  }
  if (!b) {
    dfs(x + 1, 0, sum + 2 * r[x]);
    dfs(x + 1, 1, sum + m + 1);
  } else {
    dfs(x + 1, 1, sum + 2 * l[x]);
    dfs(x + 1, 0, sum + m + 1);
  }
}
int main() {
  int i, j;
  scanf("%d%d", &n, &m);
  k = n + 1;
  for (i = n; i >= 1; --i) {
    scanf("%s", s);
    int maxv = 1, minv = m, flag = 0;
    for (j = 1; j <= m; ++j) {
      if (s[j] == '1') {
        flag = 1;
        maxv = max(maxv, j);
        minv = min(minv, j);
      }
    }
    if (!flag) {
      l[i] = r[i] = 0;
      if (k == i + 1) {
        k = i;
      }
    } else
      l[i] = m - minv + 1, r[i] = maxv;
  }
  k--;
  if (k < 1) k = 1;
  dfs(1, 0, 0);
  ans += k - 1;
  printf("%d", ans);
  return 0;
}
