#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
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;
}
