#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
const int maxn = 1e3 + 5;
const int maxx = 1e5 + 100;
const double EPS = 1e-9;
const int MOD = 1000000007;
template <class T>
inline T min(T a, T b, T c) {
  return min(min(a, b), c);
}
template <class T>
inline T max(T a, T b, T c) {
  return max(max(a, b), c);
}
template <class T>
inline T min(T a, T b, T c, T d) {
  return min(min(a, b), min(c, d));
}
template <class T>
inline T max(T a, T b, T c, T d) {
  return max(max(a, b), max(c, d));
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  a %= b;
  return gcd(b, a);
}
inline int Scan() {
  int res = 0, ch, flag = 0;
  if ((ch = getchar()) == '-')
    flag = 1;
  else if (ch >= '0' && ch <= '9')
    res = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9') res = res * 10 + ch - '0';
  return flag ? -res : res;
}
int n, m;
char ch[20][120];
int l[20], r[20], visit[20], dp[20][2];
int main() {
  while (~scanf("%d%d", &n, &m)) {
    memset(visit, 0, sizeof visit);
    memset(r, 0, sizeof r);
    memset(l, 0, sizeof l);
    for (int i = n; i >= 1; i--) {
      scanf("%s", ch[i] + 1);
      for (int j = 2; j <= m + 1; j++)
        if (ch[i][j] == '1') {
          l[i] = j;
          visit[i] = 1;
          break;
        }
      for (int j = m + 1; j >= 2; j--)
        if (ch[i][j] == '1') {
          r[i] = j;
          break;
        }
    }
    int x = n;
    while (!visit[x--] && x >= 1) n--;
    if (n == 1) {
      printf("%d\n", r[1] ? r[1] - 1 : 0);
      continue;
    }
    memset(dp, INF, sizeof dp);
    dp[1][0] = r[1] ? (r[1] - 1) * 2 : 0, dp[1][1] = m + 1;
    for (int i = 2; i < n; i++) {
      if (!visit[i]) {
        dp[i][0] = dp[i - 1][0] + 1;
        dp[i][1] = dp[i - 1][1] + 1;
        continue;
      }
      dp[i][0] = min(dp[i - 1][0] + 2 * (r[i] - 1) + 1, dp[i][0]);
      dp[i][0] = min(dp[i - 1][1] + m + 1 + 1, dp[i][0]);
      dp[i][1] = min(dp[i - 1][1] + 2 * (m + 2 - l[i]) + 1, dp[i][1]);
      dp[i][1] = min(dp[i - 1][0] + m + 1 + 1, dp[i][1]);
    }
    int ans = min(dp[n - 1][0] + r[n], dp[n - 1][1] + 1 + m + 2 - l[n]);
    printf("%d\n", ans);
  }
  return 0;
}
