function bomb(x){
   var s="";
   var xLength=x.length;
   for(var i=0;i<xLength;i++)s+=x[i];
   var L=0;
   while(L!=s.length){
      L=s.length;
      s=s.replace(/1111+|2222+|3333+/,"");
   }
   if(min>s.length)min=s.length;
}
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var Arr=(input.trim()).split("\n");
while(true){
   var N=Arr.shift()-0;
   if(N==0)break;
   var arr=[];
   for(var i=0;i<N;i++){
      var v=Arr.shift()-0;
      arr.push(v);
   }
   var min=Infinity;
   for(var i=0;i<N;i++){
      var memo=arr[i];
      if(i<N-1){
         arr[i]=arr[i+1];
         bomb(arr);
      }
      if(i>0){
         arr[i]=arr[i-1];
         bomb(arr);
      }
      arr[i]=memo;
   }
   console.log(min);
}