#include <bits/stdc++.h>
using namespace std;
int n, m, ans, top, l[20], r[20], f[20][2];
char mp[20][110];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", mp[i]);
  top = n - 1;
  for (int i = 0; i < n; i++) {
    l[i] = m + 1;
    r[i] = 0;
    for (int j = 1; j <= m; j++)
      if (mp[i][j] == '1') {
        l[i] = min(l[i], j);
        r[i] = max(r[i], j);
        top = min(top, i);
      }
  }
  f[n][0] = 0;
  f[n][1] = 0x3f3f3f3f;
  for (int i = n - 1; i >= top; i--) {
    if (i == top) {
      f[i][0] = f[i + 1][0] + r[i];
      f[i][1] = f[i + 1][1] + m + 1 - l[i];
    } else if (i == n - 1) {
      f[i][0] = r[i] * 2;
      f[i][1] = m + 1;
    } else {
      f[i][0] = min(f[i + 1][1] + m + 1, f[i + 1][0] + r[i] * 2);
      f[i][1] = min(f[i + 1][0] + m + 1, f[i + 1][1] + (m + 1 - l[i]) * 2);
    }
  }
  ans += n - 1 - top + min(f[top][0], f[top][1]);
  printf("%d\n", ans);
  return 0;
}
