#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
//C++11
#if __cplusplus == 201103L
#include <tuple>
#endif
 
using namespace std;
 
#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
 
template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; }
 
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int W;
bool used[5][410] = {};
vector<string> grid;
int min_ans;
int dfs(int p, int bx){
  if(min_ans <= (10 - p) + bx + W - 1) return INF;
  if(p == 10) {
    return min_ans = bx + W - 1;
  }
  int res = INF;
  for(int dx = 0; dx <= W; dx++){
    if(p == 0 && dx > 0) break;
    bool bad = false;
    REP(y, 5) REP(x, W) if(grid[y][x] == 'X' && used[y][bx + dx + x]) bad = true;
    if(bad) continue;
    REP(y, 5) REP(x, W) if(grid[y][x] == 'X') used[y][bx + dx + x] = true;
    res = min(res, dfs(p + 1, bx + dx + 1));
    REP(y, 5) REP(x, W) if(grid[y][x] == 'X') used[y][bx + dx + x] = false;
  }
  return res;
}
 
int main(){
  while(cin>>W && W){
    grid.assign(5, "");
    REP(i, 5) cin>>grid[i];
    bool empty = true;
    REP(y, 5) REP(x, W) if(grid[y][x] != '.') empty = false;
    assert(not empty);
    memset(used, 0, sizeof(used));
    min_ans = INF;
    cout << dfs(0, 0) << endl;
  }
  return 0;
}
