#include <bits/stdc++.h>
using namespace std;
int l[17], r[17], dp[17][4], num[17];
char ro[17][107];
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    memset(dp, 0, sizeof(dp));
    int top = 0;
    for (int i = n; i > 0; i--) {
      scanf("%s", ro[i]);
    }
    for (int i = 1; i <= n; i++) {
      r[i] = 0, l[i] = m + 1;
      for (int j = 1; j <= m + 2; j++) {
        if (ro[i][j] == '1') top = i, r[i] = j;
      }
    }
    for (int i = 1; i <= n; i++)
      for (int j = m + 2; j >= 1; j--)
        if (ro[i][j] == '1') l[i] = j;
    dp[1][0] = r[1] * 2;
    dp[1][1] = m + 1;
    for (int i = 2; i < top; i++) {
      dp[i][0] = min(dp[i - 1][0] + 2 * r[i] + 1, dp[i - 1][1] + m + 1 + 1);
      dp[i][1] =
          min(dp[i - 1][0] + m + 1 + 1, dp[i - 1][1] + 2 * (m + 1 - l[i]) + 1);
    }
    dp[top][0] = r[top] + dp[top - 1][0] + 1;
    dp[top][1] = (m + 1 - l[top]) + dp[top - 1][1] + 1;
    if (top == 0)
      printf("0\n");
    else if (top == 1)
      printf("%d\n", r[1]);
    else
      printf("%d\n", min(dp[top][0], dp[top][1]));
  }
  return 0;
}
