#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
char mp[25][250];
int dp[25][3], n, m;
int L[25], R[25], Y[25];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = n; i >= 1; i--) {
    scanf("%s", &mp[i]);
    int l = -1, r = -1;
    for (int j = 1; j <= m; j++) {
      if (mp[i][j] == '1') {
        r = j;
        if (l == -1) l = j;
      }
    }
    if (l == -1) continue;
    L[i] = l, R[i] = r, Y[i] = 1;
  }
  int fl = 0;
  for (int i = 1; i <= n; i++)
    if (Y[i]) fl = i;
  if (fl == 0) {
    printf("0\n");
  } else if (fl == 1) {
    printf("%d\n", R[1]);
  } else {
    dp[1][0] = 2 * R[1], dp[1][1] = m + 1;
    for (int i = 2; i < fl; i++) {
      if (Y[i]) {
        dp[i][0] = min(dp[i - 1][0] + 2 * R[i], dp[i - 1][1] + m + 1) + 1;
        dp[i][1] =
            min(dp[i - 1][1] + 2 * (m - L[i] + 1), dp[i - 1][0] + m + 1) + 1;
      } else {
        dp[i][0] = dp[i - 1][0] + 1;
        dp[i][1] = dp[i - 1][1] + 1;
      }
    }
    int ans = min(dp[fl - 1][0] + R[fl], dp[fl - 1][1] + (m - L[fl] + 1)) + 1;
    printf("%d\n", ans);
  }
  return 0;
}
