#include <bits/stdc++.h>   using namespace std;   vector<int> d[2], f;   vector<vector<bool> > nab;   int n, len = -1;   const int INF = 1 << 29;   int calc(vector<bool> &a, vector<bool> &b) {     vector<bool> c(a.size() + b.size() + 1);     for (int i = 0; i < b.size(); ++i) c[i] = b[i];     for (int i = b.size(); i < c.size(); ++i) c[i + 1] = a[i - (int)b.size()];     vector<int> p(c.size());     p[0] = 0;     for (int i = 1; i < p.size(); ++i) {       int j = p[i - 1];       while (j > 0 && (c[i] != c[j] || i == b.size() || j == b.size()))         j = p[j - 1];       if (c[i] == c[j] && !(i == b.size() || j == b.size())) j++;       p[i] = j;     }     return p[c.size() - 1];   }   vector<vector<int> > p;   inline int suf(vector<bool> &a, int &l) {     int ans = 0;     for (int i = len - l; i < len; ++i) ans |= ((int)a[i] << (i + l - len));     return ans;   }   inline int pref(vector<bool> &a, int &l) {     int ans = 0;     for (int i = 0; i < l; ++i) ans |= ((int)a[i] << i);     return ans;   }   int main() {     cin.sync_with_stdio(0);     cin >> n;     nab.resize(n);     d[0].resize(n + 1);     d[1].resize(n + 1);     string str;     for (int i = 0; i < n; ++i) {       cin >> str;       if (len == -1) len = str.size();       nab[i].resize(len);       for (int j = 0; j < len; ++j) nab[i][j] = (str[j] ==  0  ? 0 : 1);     }     p.resize(len + 1, vector<int>(1 << len, -1));     d[0][0] = d[1][0] = 0;     p[0][0] = 1;     vector<int> f(n);     f[0] = len;     for (int i = 1; i < n; ++i) f[i] = f[i - 1] + len - calc(nab[i - 1], nab[i]);     int suffix[len + 1], prefix[len + 1];     d[1][1] = d[0][1] = len;     int bestp, bestd, now;     for (int i = 2; i <= n; ++i) {       d[0][i] = min(d[0][i - 1], d[1][i - 1]) + f[i - 1] - f[i - 2];       d[1][i] = INF;       for (int j = 1; j <= len; ++j) {         suffix[j] = suf(nab[i - 2], j);         prefix[j] = pref(nab[i - 1], j);       }       bestp = p[0][0];       d[1][i] = d[1][bestp] + f[i - 2] - f[bestp - 1] + len;       for (int j = 1; j <= len; ++j) {         int &pp = p[j][prefix[j]];         if (pp != -1) {           now = d[1][pp] + f[i - 2] - f[pp - 1] + len - j;           if (now < d[1][i]) {             d[1][i] = now;             bestp = pp;           }         }       }       if (d[1][i] < d[1][p[0][0]] + f[i - 1] - f[p[0][0] - 1]) p[0][0] = i;       for (int j = 1; j <= len; ++j) {         int &ps = p[j][suffix[j]];         if (ps == -1 || (d[1][i] < d[1][ps] + f[i - 1] - f[ps - 1])) ps = i;       }     }     cout << min(d[1][n], d[0][n]);     return 0;   }   