#include <bits/stdc++.h>
using namespace std;
char s[20][110];
int l[20], r[20];
int dp[20][2];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) {
    int n, m;
    cin >> n >> m;
    m += 2;
    for (int i = 0; i < n + 1; i++) l[i] = m - 1;
    memset(dp, 1000000007, sizeof dp);
    int mx = 0;
    for (int i = n - 1; i >= 0; i--) cin >> s[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i][j] == '1') {
          l[i] = min(l[i], j);
          r[i] = max(r[i], j);
          mx = i;
        }
      }
    }
    if (mx == 0) {
      cout << r[0];
      return 0;
    }
    dp[0][0] = 0;
    for (int i = 1; i < mx + 1; i++) {
      if (dp[i - 1][0] != 1000000007) {
        dp[i][0] = min(dp[i][0], dp[i - 1][0] + r[i - 1] * 2 + 1);
        dp[i][1] = min(dp[i][1], dp[i - 1][0] + m);
      }
      if (dp[i - 1][1] != 1000000007) {
        dp[i][1] = min(dp[i][1], dp[i - 1][1] + (m - l[i - 1] - 1) * 2 + 1);
        dp[i][0] = min(dp[i][0], dp[i - 1][1] + m);
      }
    }
    cout << min(dp[mx][0] + r[mx], dp[mx][1] + (m - l[mx] - 1));
  }
  return 0;
}
