// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 10e-7
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
// mylittlepony
using namespace std;
int solve(vector<int> S) {
vector<int> A =S;
int k =0;
/* while(true) {
vector<int> B;
int N =A.size();
for(int i =0; i < N; i++) {
B.push_back(A[i]);
if(A[i] == 1<<k && B.size() > 1 && B[B.size()-2] == 1<<k) {
B.pop_back();
B.pop_back();
B.push_back(1<<(k+1));}
}
if(A.size() == B.size()) break;
A =B;}
*/ int N =A.size();
vector< vector<int> > V[20];
int ret =0;
for(int i =0; i < 20; i++) V[i].resize(N,vector<int>(N+1,0));
for(k =0; k < 19; k++) {
for(int i =0; i < N; i++) if(A[i] == 1<<k && V[k][i][i+1] == 0) V[k][i][i+1] =1;
vector< vector<int> > M =V[k];
for(int j =2; j <= N; j++) for(int i =j-2; i >= 0; i--) M[i][j] =max(M[i][j],M[i+1][j]);
for(int i =0; i < N; i++) {
int x =0;
for(int j =i+1; j < N; j++) if(V[k][i][j] > x) {
for(int l =j+1; l <= N; l++) if(M[j][l] > 0)
V[k+1][i][l] =max(V[k+1][i][l],M[j][l]+V[k][i][j]);
x =V[k][i][j];}
}
for(int i =1; i <= N; i++) ret =max(ret,M[0][i]);}
return ret;}
int main() {
cin.sync_with_stdio(0);
int N;
while(cin >> N) {
if(N == 0) return 0;
vector<int> A(N);
vector< vector<int> > S(500);
for(int i =0; i < N; i++) {
int a; cin >> a;
int b =a;
while(b%2 == 0) b /=2;
S[b].push_back(a/b);}
int ans =0;
for(int i =0; i < 500; i++) if(!S[i].empty()) ans =max(ans,solve(S[i]));
cout << ans << "\n";}
return 0;}
// look at my code
// my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