#include <bits/stdc++.h>
using namespace std;
int n, m;
char matrix[15][102];
int dp[15][102];
int leftMost[15], rightMost[15];
bool empty[15];
int top = -1;
int rec(int r, int s) {
  if (r == top) {
    if (s == 0) {
      return (rightMost[r]);
    } else {
      return (m + 1 - leftMost[r]);
    }
  }
  if (dp[r][s] != -1) return dp[r][s];
  int res = INT_MAX;
  if (s == 0) {
    res = min(res, 1 + m + 1 + rec(r - 1, m + 1));
    res = min(res, 1 + rightMost[r] * 2 + rec(r - 1, 0));
  } else {
    res = min(res, 1 + m + 1 + rec(r - 1, 0));
    res = min(res, 1 + (m + 1 - leftMost[r]) * 2 + rec(r - 1, m + 1));
  }
  dp[r][s] = res;
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    leftMost[i] = m + 1;
    rightMost[i] = 0;
    for (int j = 0; j < m + 2; ++j) {
      dp[i][j] = -1;
      cin >> matrix[i][j];
      if (matrix[i][j] == '1') {
        leftMost[i] = min(leftMost[i], j);
        rightMost[i] = max(rightMost[i], j);
      }
    }
    if (rightMost[i] == 0) empty[i] = true;
  }
  top = 0;
  for (int i = 0; i < n; i++) {
    if (empty[i] && top == i) top = i + 1;
  }
  if (top > n - 1) top = n - 1;
  cout << rec(n - 1, 0) << endl;
  return 0;
}
