#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lp(i,m) for(int i =0 ;i<m;i++)
#define lp1(i,b,e) for(int i = b;i<e;i++)
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define Endl '\n'
const int N = 1e6 + 10;
#define pi acos(-1)
#define eps 1e-9
int A[N],seg[4*N];
int dp[100][100];
int ans = 0;
int n;
void Build(int n, int s, int e) {
	if (s == e) {
		seg[n] = A[s];
		return;
	}
	Build(2 * n, s, (s + e) / 2);
	Build(2 * n + 1, (s + e) / 2 + 1, e);

	seg[n] = min(seg[2 * n], seg[2 * n + 1]);
}

int get(int n, int s, int e, int rl, int rr) {
	if (s > rr || e < rl) return 1e9;
	if (s >= rl && e <= rr) {
		return seg[n];
	}

	return min(get(n * 2, s, (s + e) / 2, rl, rr), get(n * 2 + 1, (s + e) / 2 + 1, e, rl, rr));
}
int solve(int index, int sz) {
	if (index == n + 1) return 0;
	int c1 = 1e9, sum = 0;

	for (int i = index; i <= n; ++i) {
		sum += A[i];
	}
}
int main() {
	int n, k;
	string s;
	cin >> n >> k;
	cin >> s;
	set<char> t(s.begin(), s.end());
	if (n < k) {
		cout << s;
		lp(i, k - n) cout << *t.begin();
		return 0;
	}
	int idx = -1;
	string m;
	char mx = *t.rbegin();
	for (int i = 0; i < k; ++i) {
		if (mx != s[i]) {
			idx = i;
		}

	}
	m = s;
	sort(m.begin(),m.end());
	for (int i = 0; i < m.size(); ++i) {
		if (m[i] > s[idx]) {
			mx = m[i];
			break;
		}
	}
	for (int i = 0; i < idx; ++i) {
		cout << s[i];
	}
	cout << mx;
	for (int i = idx + 1; i < k; ++i) {
		cout << *t.begin();
	}
	// Think and hold on before coding
	// Think about Reverse Thinking
	// OverFlow ? long long or int
	// keep your mind of array size
	// 2 Minutes Tracing Better Than 20 Minute Penalty
	// Programming Your Mind on Success
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lp(i,m) for(int i =0 ;i<m;i++)
#define lp1(i,b,e) for(int i = b;i<e;i++)
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define Endl '\n'
const int N = 1e6 + 10;
#define pi acos(-1)
#define eps 1e-9
int A[N],seg[4*N];
int dp[100][100];
int ans = 0;
int n;
void Build(int n, int s, int e) {
	if (s == e) {
		seg[n] = A[s];
		return;
	}
	Build(2 * n, s, (s + e) / 2);
	Build(2 * n + 1, (s + e) / 2 + 1, e);

	seg[n] = min(seg[2 * n], seg[2 * n + 1]);
}

int get(int n, int s, int e, int rl, int rr) {
	if (s > rr || e < rl) return 1e9;
	if (s >= rl && e <= rr) {
		return seg[n];
	}

	return min(get(n * 2, s, (s + e) / 2, rl, rr), get(n * 2 + 1, (s + e) / 2 + 1, e, rl, rr));
}
int solve(int index, int sz) {
	if (index == n + 1) return 0;
	int c1 = 1e9, sum = 0;

	for (int i = index; i <= n; ++i) {
		sum += A[i];
	}
}
int main() {
	int n, k;
	string s;
	cin >> n >> k;
	cin >> s;
	set<char> t(s.begin(), s.end());
	if (n < k) {
		cout << s;
		lp(i, k - n) cout << *t.begin();
		return 0;
	}
	int idx = -1;
	string m;
	char mx = *t.rbegin();
	for (int i = 0; i < k; ++i) {
		if (mx != s[i]) {
			idx = i;
		}
		m += s[i];
	}
	sort(m.begin(),m.end());
	for (int i = 0; i < m.size(); ++i) {
		if (m[i] > s[idx]) {
			mx = m[i];
			break;
		}
	}
	for (int i = 0; i < idx; ++i) {
		cout << s[i];
	}
	cout << mx;
	for (int i = idx + 1; i < k; ++i) {
		cout << *t.begin();
	}
	// Think and hold on before coding
	// Think about Reverse Thinking
	// OverFlow ? long long or int
	// keep your mind of array size
	// 2 Minutes Tracing Better Than 20 Minute Penalty
	// Programming Your Mind on Success
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lp(i,m) for(int i =0 ;i<m;i++)
#define lp1(i,b,e) for(int i = b;i<e;i++)
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define Endl '\n'
const int N = 1e6 + 10;
#define pi acos(-1)
#define eps 1e-9
int A[N],seg[4*N];
int dp[100][100];
int ans = 0;
int n;
void Build(int n, int s, int e) {
	if (s == e) {
		seg[n] = A[s];
		return;
	}
	Build(2 * n, s, (s + e) / 2);
	Build(2 * n + 1, (s + e) / 2 + 1, e);

	seg[n] = min(seg[2 * n], seg[2 * n + 1]);
}

int get(int n, int s, int e, int rl, int rr) {
	if (s > rr || e < rl) return 1e9;
	if (s >= rl && e <= rr) {
		return seg[n];
	}

	return min(get(n * 2, s, (s + e) / 2, rl, rr), get(n * 2 + 1, (s + e) / 2 + 1, e, rl, rr));
}
int solve(int index, int sz) {
	if (index == n + 1) return 0;
	int c1 = 1e9, sum = 0;

	for (int i = index; i <= n; ++i) {
		sum += A[i];
	}
}
int main() {
	int n, k;
	string s;
	cin >> n >> k;
	cin >> s;
	set<char> t(s.begin(), s.end());
	if (n < k) {
		cout << s;
		lp(i, k - n) cout << *t.begin();
		return 0;
	}
	int idx = -1;
	char mx = *t.rbegin();
	for (int i = 0; i < n; ++i) {
		if (mx != s[i]) {
			idx = i;
		}
	}
	string m = s;
	sort(m.begin(), m.end());
	for (int i = 0; i < m.size(); ++i) {
		if (m[i] > s[idx]) {
			mx = m[i];
			break;
		}
	}
	for (int i = 0; i < idx; ++i) {
		cout << s[i];
	}
	cout << mx;
	for (int i = idx + 1; i < k; ++i) {
		cout << *t.begin();
	}
	// Think and hold on before coding
	// Think about Reverse Thinking
	// OverFlow ? long long or int
	// keep your mind of array size
	// 2 Minutes Tracing Better Than 20 Minute Penalty
	// Programming Your Mind on Success
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lp(i,m) for(int i =0 ;i<m;i++)
#define lp1(i,b,e) for(int i = b;i<e;i++)
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define Endl '\n'
const int N = 1e6 + 10;
#define pi acos(-1)
#define eps 1e-9
int A[N],seg[4*N];
int dp[100][100];
int ans = 0;
int n;
void Build(int n, int s, int e) {
	if (s == e) {
		seg[n] = A[s];
		return;
	}
	Build(2 * n, s, (s + e) / 2);
	Build(2 * n + 1, (s + e) / 2 + 1, e);

	seg[n] = min(seg[2 * n], seg[2 * n + 1]);
}

int get(int n, int s, int e, int rl, int rr) {
	if (s > rr || e < rl) return 1e9;
	if (s >= rl && e <= rr) {
		return seg[n];
	}

	return min(get(n * 2, s, (s + e) / 2, rl, rr), get(n * 2 + 1, (s + e) / 2 + 1, e, rl, rr));
}
int solve(int index, int sz) {
	if (index == n + 1) return 0;
	int c1 = 1e9, sum = 0;

	for (int i = index; i <= n; ++i) {
		sum += A[i];
	}
}
int main() {
	int n, k;
	string s;
	cin >> n >> k;
	cin >> s;
	set<char> t(s.begin(), s.end());
	if (n < k) {
		cout << s;
		lp(i, k - n) cout << *t.begin();
		return 0;
	}
	int idx = -1;
	char mx = *t.rbegin();
	for (int i = 0; i < n; ++i) {
		if (mx != s[i]) {
			idx = i;
		}
	}
	string m = s;
	sort(m.begin(), m.end());
	for (int i = 0; i < m.size(); ++i) {
		if (m[i] > mx) {
			mx = m[i];
			break;
		}
	}
	for (int i = 0; i < idx; ++i) {
		cout << s[i];
	}
	cout << mx;
	for (int i = idx + 1; i < k; ++i) {
		cout << *t.begin();
	}
	// Think and hold on before coding
	// Think about Reverse Thinking
	// OverFlow ? long long or int
	// keep your mind of array size
	// 2 Minutes Tracing Better Than 20 Minute Penalty
	// Programming Your Mind on Success
	// Corner Case [n == 1 , n == 0 ]
}


/*     !ya khoda!     */
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

#define ll long long
#define ld long double

#define pp push
#define po pop()
#define pb push_back
#define pf push_front
#define pob pop_back()
#define pof pop_front()

#define F first
#define S second

#define mkp make_pair
#define pll pair<ll,ll>

#define coutghat(x)      cout<<tolower(x);
#define coutashar(x,d)   cout<<fixed<<setprecision(x)<<(ld)d
#define IOS ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
//#define poow(a,b,M)   b?(poow((a*a)%M,b/2)%M * (b&1?a))%M:1 ;
/*==========================================================================*/
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using ordered_multiset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
//#pragma optimize O3
// 107,109, 211,223, 307, 401,409, 503,509, 601,607, 701,709, 809, 907, 1009
/*==========================================================================*/
const int N=5e5+10 , M=30 , LG=26 , base=2;
const int MOD=1e9+7 , mod=1e9+9;
const ll  INF=1e9   , inf=1e18 ;
ll n,k , vis[M];
string s,ans;
vector<char>v;
/*==========================================================================*/
int main()
{
    IOS;

    cin>>n>>k>>s;
    for(char c:s)
        if(!vis[c-'a'])
            vis[c-'a']=1 , v.pb(c);

    sort(v.begin(),v.end());
    if(n<k)
    {
        cout<<s;
        for(int i=n;i<k;i++)
            cout<<v[0];
        return 0;
    }

    for(int i=0;i<k;i++)
        ans.pb(s[i]);

    ll j=k-1;
    while(j>=0 && ans[j]==v.back())
        ans[j]=v[0] , j--;

    if(j<0)
        return -1;

    for(int i=ans[j]-'a'+1;i<26;i++)
    {
        if(vis[i])
        {
            ans[j]=char(i+'a');
            break;
        }
    }

    cout<<ans;
    return 0;
}


/*     !ya khoda!     */
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

#define ll long long
#define ld long double

#define pp push
#define po pop()
#define pb push_back
#define pf push_front
#define pob pop_back()
#define pof pop_front()

#define F first
#define S second

#define mkp make_pair
#define pll pair<ll,ll>

#define coutghat(x)      cout<<tolower(x);
#define coutashar(x,d)   cout<<fixed<<setprecision(x)<<(ld)d
#define IOS ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
//#define poow(a,b,M)   b?(poow((a*a)%M,b/2)%M * (b&1?a))%M:1 ;
/*==========================================================================*/
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using ordered_multiset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
//#pragma optimize O3
// 107,109, 211,223, 307, 401,409, 503,509, 601,607, 701,709, 809, 907, 1009
/*==========================================================================*/
const int N=5e5+10 , M=30 , LG=26 , base=2;
const int MOD=1e9+7 , mod=1e9+9;
const ll  INF=1e9   , inf=1e18 ;
ll n,k , low_char,x,vis[M];
string s,t;
/*==========================================================================*/
int main()
{
    IOS;
    cin>>n>>k>>s;
    for(int i=0;i<n;i++)   vis[s[i]-'a']=1;
    for(int i=0;i<min(n,k);i++)   t+=s[i];
    for(int i=0;i<M;i++)
    {
        if(vis[i]==1)
        {
            low_char=i;
            break;
        }
    }
    if(n<k)
        for(int i=n;i<k;i++)
            t+=" ";
    for(int i=k-1;i>=0;i--)
    {
        x=t[i]-'a';
        for(int j=x+1;j<M;j++)
        {
            if(vis[j]==1)
            {
                t[i]=j+'a';
                for(int l=i+1;l<k;l++)
                    t[l]=low_char+'a';
                cout<<t;
                return 0;
            }
        }
    }

    return 0;
}


/*     !ya khoda!     */
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

#define ll long long
#define ld long double

#define pp push
#define po pop()
#define pb push_back
#define pf push_front
#define pob pop_back()
#define pof pop_front()

#define F first
#define S second

#define mkp make_pair
#define pll pair<ll,ll>

#define coutghat(x)      cout<<tolower(x);
#define coutashar(x,d)   cout<<fixed<<setprecision(x)<<(ld)d
#define IOS ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
//#define poow(a,b,M)   b?(poow((a*a)%M,b/2)%M * (b&1?a))%M:1 ;
/*==========================================================================*/
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using ordered_multiset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
//#pragma optimize O3
// 107,109, 211,223, 307, 401,409, 503,509, 601,607, 701,709, 809, 907, 1009
/*==========================================================================*/
const int N=5e5+10 , M=30 , LG=26 , base=2;
const int MOD=1e9+7 , mod=1e9+9;
const ll  INF=1e9   , inf=1e18 ;
ll n,k , low_char,x,vis[M];
string s,t;
/*==========================================================================*/
int main()
{
    IOS;
    cin>>n>>k>>s;
    for(int i=0;i<n;i++)   vis[s[i]-'a']=1;
    for(int i=0;i<k;i++)   t+=s[i];
    for(int i=0;i<M;i++)
    {
        if(vis[i]==1)
        {
            low_char=i;
            break;
        }
    }
    if(n<k)
        for(int i=n;i<k;i++)
            t+=(low_char+'a');
    for(int i=k-1;i>=0;i--)
    {
        x=t[i]-'a';
        for(int j=x+1;j<M;j++)
        {
            if(vis[j]==1)
            {
                t[i]=j+'a';
                for(int l=i+1;l<k;l++)
                    t[l]=low_char+'a';
                cout<<t;
                return 0;
            }
        }
    }

    return 0;
}


/*     !ya khoda!     */
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

#define ll long long
#define ld long double

#define pp push
#define po pop()
#define pb push_back
#define pf push_front
#define pob pop_back()
#define pof pop_front()

#define F first
#define S second

#define mkp make_pair
#define pll pair<ll,ll>

#define coutghat(x)      cout<<tolower(x);
#define coutashar(x,d)   cout<<fixed<<setprecision(x)<<(ld)d
#define IOS ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
//#define poow(a,b,M)   b?(poow((a*a)%M,b/2)%M * (b&1?a))%M:1 ;
/*==========================================================================*/
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using ordered_multiset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
//#pragma optimize O3
// 107,109, 211,223, 307, 401,409, 503,509, 601,607, 701,709, 809, 907, 1009
/*==========================================================================*/
const int N=5e5+10 , M=30 , LG=26 , base=2;
const int MOD=1e9+7 , mod=1e9+9;
const ll  INF=1e9   , inf=1e18 ;
ll n,k , low_char,x,vis[M];
string s,t;
/*==========================================================================*/
int main()
{
    IOS;
    cin>>n>>k>>s;
    for(int i=0;i<n;i++)   vis[s[i]-'a']=1;
    for(int i=0;i<k;i++)   t+=s[i];
    for(int i=0;i<M;i++)
    {
        if(vis[i]==1)
        {
            low_char=i;
            break;
        }
    }

    for(int i=k-1;i>=0;i--)
    {
        x=t[i]-'a';
        for(int j=x+1;j<M;j++)
        {
            if(vis[j]==1)
            {
                t[i]=j+'a';
                for(int l=i+1;l<k;l++)
                    t[l]=low_char+'a';
                cout<<t;
                return 0;
            }
        }
    }

    return 0;
}


/*     !ya khoda!     */
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

#define ll long long
#define ld long double

#define pp push
#define po pop()
#define pb push_back
#define pf push_front
#define pob pop_back()
#define pof pop_front()

#define F first
#define S second

#define mkp make_pair
#define pll pair<ll,ll>

#define coutghat(x)      cout<<tolower(x);
#define coutashar(x,d)   cout<<fixed<<setprecision(x)<<(ld)d
#define IOS ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);
//#define poow(a,b,M)   b?(poow((a*a)%M,b/2)%M * (b&1?a))%M:1 ;
/*==========================================================================*/
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using ordered_multiset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
//#pragma optimize O3
// 107,109, 211,223, 307, 401,409, 503,509, 601,607, 701,709, 809, 907, 1009
/*==========================================================================*/
const int N=5e5+10 , M=30 , LG=26 , base=2;
const int MOD=1e9+7 , mod=1e9+9;
const ll  INF=1e9   , inf=1e18 ;
ll n,k , low_char,x,vis[M];
string s,t;
/*==========================================================================*/
int main()
{
    IOS;
    cin>>n>>k>>s , t=s;
    for(int i=0;i<n;i++)   vis[s[i]-'a']=1;
    for(int i=0;i<M;i++)
    {
        if(vis[i]==1)
        {
            low_char=i;
            break;
        }
    }
    if(n<k)
        for(int i=n;i<k;i++)
            t+=" ";
    for(int i=k-1;i>=0;i--)
    {
        x=t[i]-'a';
        for(int j=x+1;j<M;j++)
        {
            if(vis[j]==1)
            {
                t[i]=j+'a';
                for(int l=i+1;l<k;l++)
                    t[l]=low_char+'a';
                cout<<t;
                return 0;
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M INT_MAX
#define N (int)1e6+10
typedef long long ll;
bool mp[100];
int main(void)
{
	int n, m;
	scanf("%d%d", &n, &m);
	int i, j;
	string arr;
	string res;
	cin >> arr;
	string temp = arr;
	vector<char> jt;
	for (i = 0; i < temp.size(); i++)
	{
		if (!mp[temp[i]])
		{
			jt.push_back(temp[i]);
			mp[temp[i]] = true;
		}
	}
	sort(jt.begin(), jt.end());
	if (m > n)
	{
		res += arr;
		res += jt[0];
		cout << res;
		return 0;
	}
	else
	{
		for (i = 0; i < m; i++)
		{
			if (i+1 < m && arr[i+1] != jt[jt.size() - 1])
			{
				res += arr[i];
			}
			else
			{
				for (j = 0; j < jt.size(); j++)
				{
					if (jt[j] > arr[i]) break;
				}
				res += jt[j];
				break;
			}
		}
		int len = res.size();
		for (i = 0; i < m - len; i++)
		{
			res += jt[0];
		}
	}
	cout << res;
}


#include <bits/stdc++.h>
using namespace std;
#define M INT_MAX
#define N (int)1e6+10
typedef long long ll;
bool mp[100];
int main(void)
{
	int n, m;
	scanf("%d%d", &n, &m);
	int i, j;
	string arr;
	string res;
	cin >> arr;
	string temp = arr;
	vector<char> jt;
	int index = m - 1;

	for (i = 0; i < temp.size(); i++)
	{
		if (!mp[temp[i]])
		{
			jt.push_back(temp[i]);
			mp[temp[i]] = true;
		}
	}
	sort(jt.begin(), jt.end());
	for (i = m - 1; i >= 0; i--)
		{
			if (arr[i] != jt[jt.size() - 1])
			{
				index = i;
				break;
			}
		}
	if (m > n)
	{
		res += arr;
		for (i = 0; i < m - n; i++)
		{
			res += jt[0];
		 }
		cout << res;
		return 0;
	}
	else
	{
		for (i = 0; i < index; i++)
		{
			res += arr[i];
		}
		for (i = 0; i < jt.size(); i++)
		{
			if (jt[i] > arr[index]) break;
		}
		res += jt[i];
		int len = res.size();
		for (i = 0; i < m - len; i++)
		{
			res += jt[0];
		}
	}
	cout << res;
}


#include <bits/stdc++.h>
using namespace std;
#define M INT_MAX
#define N (int)1e6+10
typedef long long ll;
bool mp[100];
int main(void)
{
	int n, m;
	scanf("%d%d", &n, &m);
	int i, j;
	string arr;
	string res;
	cin >> arr;
	string temp = arr;
	vector<char> jt;
	int index = m - 1;

	for (i = 0; i < temp.size(); i++)
	{
		if (!mp[temp[i]])
		{
			jt.push_back(temp[i]);
			mp[temp[i]] = true;
		}
	}
	sort(jt.begin(), jt.end());
	for (i = m - 1; i >= 0; i--)
		{
			if (arr[i] != jt[jt.size() - 1])
			{
				index = i;
				break;
			}
		}
	if (m > n)
	{
		res += arr;
		for (i = 0; i < m - n; i++)
			res += jt[0];
		cout << res;
		return 0;
	}
	else
	{
		for (i = 0; i < index; i++)
		{
			res += arr[i];
		}
		for (i = 0; i < jt.size(); i++)
		{
			if (jt[i] > arr[index]) break;
		}
		res += jt[i];
		int len = res.size();
		for (i = 0; i < m - len; i++)
		{
			res += jt[0];
		}
	}
	cout << res;
}


#include <bits/stdc++.h>
using namespace std;
#define M INT_MAX
#define N (int)1e6+10
typedef long long ll;
bool mp[100];
int main(void)
{
	int n, m;
	scanf("%d%d", &n, &m);
	int i, j;
	string arr;
	string res;
	cin >> arr;
	string temp = arr;
	vector<char> jt;
	int index;

	for (i = 0; i < temp.size(); i++)
	{
		if (!mp[temp[i]])
		{
			jt.push_back(temp[i]);
			mp[temp[i]] = true;
		}
	}
	sort(jt.begin(), jt.end());
	for (i = arr.size() - 1; i >= 0; i--)
	{
		if (arr[i] != jt[jt.size() - 1])
		{
			index = i;
			break;
		}
	}
	if (m > n)
	{
		res += arr;
		res += jt[0];
		cout << res;
		return 0;
	}
	else if (m < n)
	{
		for (i = m - 1; i >= 0; i--)
		{
			if (arr[i] != jt[jt.size() - 1])
			{
				index = i;
				break;
			}
		}
		for (i = 0; i < index; i++)
		{
			res += arr[i];
		}
		for (i = 0; i < jt.size(); i++)
		{
			if (jt[i] > arr[index]) break;
		}
		res += jt[i];
		int len = res.size();
		for (i = 0; i < m - len; i++)
		{
			res += jt[0];
		}
	}
	else
	{
		for (i = 0; i < index; i++)
		{
			res += arr[i];
		}
		for (i = 0; i < jt.size(); i++)
		{
			if (jt[i] > arr[index]) break;
		}
		res += jt[i];
		int len = res.size();
		for (i = 0; i < m - len; i++)
		{
			res += jt[0];
		}
	}
	cout << res;
}


#include <bits/stdc++.h>
using namespace std;
#define M INT_MAX
#define N (int)1e6+10
typedef long long ll;
bool mp[100];
int main(void)
{
	int n, m;
	scanf("%d%d", &n, &m);
	int i, j;
	string arr;
	string res;
	cin >> arr;
	string temp = arr;
	vector<char> jt;
	int index;

	for (i = 0; i < temp.size(); i++)
	{
		if (!mp[temp[i]])
		{
			jt.push_back(temp[i]);
			mp[temp[i]] = true;
		}
	}
	sort(jt.begin(), jt.end());
	for (i = m - 1; i >= 0; i--)
		{
			if (arr[i] != jt[jt.size() - 1])
			{
				index = i;
				break;
			}
		}
	if (m > n)
	{
		res += arr;
		for (i = 0; i < m - n; i++)
			res += jt[0];
		cout << res;
		return 0;
	}
	else
	{
		for (i = 0; i < index; i++)
		{
			res += arr[i];
		}
		for (i = 0; i < jt.size(); i++)
		{
			if (jt[i] > arr[index]) break;
		}
		res += jt[i];
		int len = res.size();
		for (i = 0; i < m - len; i++)
		{
			res += jt[0];
		}
	}
	cout << res;
}


#include <bits/stdc++.h>
using namespace std;
#define M INT_MAX
#define N (int)1e6+10
typedef long long ll;
bool mp[100];
int main(void)
{
	int n, m;
	scanf("%d%d", &n, &m);
	int i, j;
	string arr;
	string res;
	cin >> arr;
	string temp = arr;
	vector<char> jt;
	int index = m - 1;

	for (i = 0; i < temp.size(); i++)
	{
		if (!mp[temp[i]])
		{
			jt.push_back(temp[i]);
			mp[temp[i]] = true;
		}
	}
	sort(jt.begin(), jt.end());
	for (i = m - 1; i >= 0; i--)
		{
			if (arr[i] != jt[jt.size() - 1])
			{
				index = i;
				break;
			}
		}
	if (m > n)
	{
		res += arr;
		for (i = 0; i < m - n; i++)
		{
			res += jt[0];
		 }
		cout << res;
	}
	else
	{
		for (i = 0; i < index; i++)
		{
			res += arr[i];
		}
		for (i = 0; i < jt.size(); i++)
		{
			if (jt[i] > arr[index]) break;
		}
		res += jt[i];
		int len = res.size();
		for (i = 0; i < m - len; i++)
		{
			res += jt[0];
		}
		cout << res;
	}

}


#include <bits/stdc++.h>
using namespace std;
#define M INT_MAX
#define N (int)1e6+10
typedef long long ll;
bool mp[100];
int main(void)
{
	int n, m;
	scanf("%d%d", &n, &m);
	int i, j;
	string arr;
	string res;
	cin >> arr;
	string temp = arr;
	vector<char> jt;
	int index = m - 1;

	for (i = 0; i < temp.size(); i++)
	{
		if (!mp[temp[i]])
		{
			jt.push_back(temp[i]);
			mp[temp[i]] = true;
		}
	}
	sort(jt.begin(), jt.end());
	for (i = m - 1; i >= 0; i--)
		{
			if (arr[i] != jt[jt.size() - 1])
			{
				index = i;
				break;
			}
		}
	if (m > n)
	{
		res += arr;
		for (i = 0; i < m - n; i++)
			res += jt[0];
		cout << res;
		return 0;
	}
	else
	{
		for (i = 0; i < index; i++)
		{
			res += arr[i];
		}
		for (i = 0; i < jt.size(); i++)
		{
			if (jt[i] > arr[index]) break;
		}
		res += jt[i];
		int len = res.size();
		for (i = 0; i < m - len; i++)
		{
			res += jt[0];
		}
	}
	int asd;
	cout << res;
}


#include <bits/stdc++.h>
using namespace std;
#define M INT_MAX
#define N (int)1e6+10
typedef long long ll;
bool mp[100];
int main(void)
{
	int n, m;
//	scanf("%d%d", &n, &m);
	cin >> n >> m;
	int i, j;
	string arr;
	string res;
	cin >> arr;
	string temp = arr;
	vector<char> jt;
	int index = m - 1;

	for (i = 0; i < temp.size(); i++)
	{
		if (!mp[temp[i]])
		{
			jt.push_back(temp[i]);
			mp[temp[i]] = true;
		}
	}
	sort(jt.begin(), jt.end());
	if (m > n)
	{
		res += arr;
		for (i = 0; i < m - n; i++)
		{
			res += jt[0];
		 }
		cout << res;
		return 0;
	}
	for (i = m - 1; i >= 0; i--)
		{
			if (arr[i] != jt[jt.size() - 1])
			{
				index = i;
				break;
			}
		}
		for (i = 0; i < index; i++)
		{
			res += arr[i];
		}
		for (i = 0; i < jt.size(); i++)
		{
			if (jt[i] > arr[index]) break;
		}
		res += jt[i];
		int len = res.size();
		for (i = 0; i < m - len; i++)
		{
			res += jt[0];
		}
		cout << res;
}


#include<bits/stdc++.h>
using namespace std;
int find(long long int v[],long long int p);
int find1(long long int v[],long long int q);
int main()
{
	string a;
	long long int n,k,i,t,p,h=0,q,m,m1;
	cin>>n>>k>>a;
	long long int out[k];

	long long int v[27] = {0};

	for(i=0;i<n;i++)
	{
		t = (int)a[i];
		t= t - 96;
		v[t]++;
	}

	for(i=1;i<26;i++)
	{
		if(v[i] > 0)
		{
			m1 = i;
			break;
		}
	}

	if(n>=k)
	{
		for(i=0;i<k;i++)
		{
			q = (int)a[i];       // current iterator
			q = q - 96;

			p = (int)a[i+1];     //next immediate iterator
			p = p - 96;

			h = find(v,p);           //returns whether a (int)alpahbet immediately greater than the current one is there
			                        // h=1,element present/h=0,greater alapahaet aint fucking present
			if(i == n-1)          //applicable only for last fucking element
			{
			   out[i] = m1+96;
			}
			else
			{
			  if(h==1)
		    	{
			    	out[i] = q+96;
			    }
			  else if(h==0)
			    {
			    	m = find1(v,q);
			     	out[i] = m+96;
			    }
		    }
		}
	}
	else
	{
	  for(i=0;i<k;i++)
	  {
	  	if(i>=n)
	  	{
	  	 	out[i] = m1+96;
		}
		else
	  	out[i] = a[i];
	  }
	}

	for(i=0;i<k;i++)
	cout<<(char)out[i];
}

int find(long long int v[],long long int p)
{
	int i,l;
	for(i=1;i<27;i++)
	{
		if(v[i]>0 && i>p)
		{
			return 1;
		}
	}
	return 0;
}

int find1(long long int v[],long long int q)
{
	int i,p,fuck,flag=0;
	for(i=1;i<27;i++)
	{
		if(v[i]>0)
		{
			fuck = i;
			break;
		}
	}
	for(i=1;i<27;i++)
	{
		if(i>q && v[i]>0)
		{
		   p = i;
		   flag = 1;
		   break;
		}
	}
	if(flag==1)
	return p;
	else
	return fuck;
}


#include<bits/stdc++.h>
using namespace std;
int find(long long int v[],long long int p);
int find1(long long int v[],long long int q);
int main()
{
	string a;
	long long int n,k,i,t,p,h=0,q,m,m1;
	cin>>n>>k>>a;
	long long int out[k];

	long long int v[27] = {0};

	for(i=0;i<n;i++)
	{
		t = (int)a[i];
		t= t - 96;
		v[t]++;
	}

	for(i=1;i<26;i++)
	{
		if(v[i] == 1)
		{
			m1 = i;
			break;
		}
	}

	if(n>=k)
	{
		for(i=0;i<k;i++)
		{
			q = (int)a[i];       // current iterator
			q = q - 96;

			p = (int)a[i+1];     //next immediate iterator
			p = p - 96;

			h = find(v,p);           //returns whether a (int)alpahbet immediately greater than the current one is there
			                        // h=1,element present/h=0,greater alapahaet aint fucking present
			if(i == n-1)          //applicable only for last fucking element
			{
			   out[i] = m1+96;
			}
			else
			{
			  if(h==1)
		    	{
			    	out[i] = q+96;
			    }
			  else if(h==0)
			    {
			    	m = find1(v,q);
			     	out[i] = m+96;
			    }
		    }
		}
	}
	else
	{
	  for(i=0;i<k;i++)
	  {
	  	if(i+1 >= n)
	  	{
	  	out[i] = m1+96;
		}
		else
		{
	  	q = (int)a[i];       // current iterator
	    out[i] = q;
	    }
	  }
	}

	for(i=0;i<k;i++)
	cout<<(char)out[i];
}

int find(long long int v[],long long int p)
{
	int i,l;
	for(i=1;i<27;i++)
	{
		if(v[i]>0 && i>p)
		{
			return 1;
		}
	}
	return 0;
}

int find1(long long int v[],long long int q)
{
	int i,p,fuck,flag=0;
	for(i=1;i<27;i++)
	{
		if(v[i]>0)
		{
			fuck = i;
			break;
		}
	}
	for(i=1;i<27;i++)
	{
		if(i>q && v[i]>0)
		{
		   p = i;
		   flag = 1;
		   break;
		}
	}
	if(flag==1)
	return p;
	else
	return fuck;
}


#include<bits/stdc++.h>
using namespace std;
int check(long long int v[],int y);
int find(long long int v[],int y);
int main()
{
	long long int m1,h,i,n,k,v[27] = {0},t;    //declaration shit
	string a;

	cin>>n>>k;                              //input
	cin>>a;

	long long int out[k];               //declaration 2

	for(i=0;i<n;i++)                      // for keeping count of the alphabets in input string
	{
		t = (int)a[i];
		t = t-97;
		v[t]++;
	}

	for(i=0;i<26;i++)                     //for finding out the smallest element in input string
	{
		if(v[i] > 0)
		{
			m1 = i+97;
			break;
		}
	}

	int y;

	if(n>=k)
	{
		for(i=k-1;i>=0;i--)
		{
			int y = (int)a[i];
			y = y-97;
			h = check(v,y);
			if(h!=1)
			continue;
			else
			{
				y = find(v,y);
				out[i] = y;
				for(int f=0;f<i;f++)
				{
				out[f] = a[f];
			    }
			    for(int f=i+1;f<k;f++)
			    {
			    	out[f] = m1;
				}
				break;
			}
		}
	}
	else
	{
	for(i=0;i<k;i++)
	  {
	  	if(i>=n)
	  	{
	  	 	out[i] = m1;
		}
		else
	  	out[i] = a[i];
	  }
	}

	for(i=0;i<k;i++)
	cout<<(char)out[i];
}

int check(long long int v[],int y)
{
	int k,fuck =0;
	for(k=0;k<27;k++)
	{
		if(v[k]>0 && k>y)
		{
			fuck = 1;
			break;
		}
	}
	return fuck;
}

int find(long long int v[],int y)
{
	int k,batman;
	for(k=0;k<27;k++)
	{
		if(v[k]>0 && k>y)
		{
			batman = k+97;
			break;
		}
	}
	return batman;

}


#include<bits/stdc++.h>
using namespace std;
int find(long long int v[],long long int p);
int find1(long long int v[],long long int q);
int main()
{
	string a;
	long long int n,k,i,t,p,h=0,q,m,m1;
	cin>>n>>k>>a;
	long long int out[k];

	long long int v[27] = {0};

	for(i=0;i<n;i++)
	{
		t = (int)a[i];
		t= t - 96;
		v[t]++;
	}

	for(i=1;i<26;i++)
	{
		if(v[i] == 1)
		{
			m1 = i;
			break;
		}
	}

	if(n>=k)
	{
		for(i=0;i<k;i++)
		{
			q = (int)a[i];       // current iterator
			q = q - 96;

			p = (int)a[i+1];     //next immediate iterator
			p = p - 96;

			h = find(v,p);           //returns whether a (int)alpahbet immediately greater than the current one is there
			                        // h=1,element present/h=0,greater alapahaet aint fucking present
			if(i == n-1)          //applicable only for last fucking element
			{
			   out[i] = m1+96;
			}
			else
			{
			  if(h==1)
		    	{
			    	out[i] = q+96;
			    }
			  else if(h==0)
			    {
			    	m = find1(v,q);
			     	out[i] = m+96;
			    }
		    }
		}
	}
	else
	{
	  for(i=0;i<k;i++)
	  {
	  	if(i+1 > n)
	  	{
	  	out[i] = m1+96;
		}
		else
		{
	  	q = (int)a[i];       // current iterator
	    out[i] = q;
	    }
	  }
	}

	for(i=0;i<k;i++)
	cout<<(char)out[i];
}

int find(long long int v[],long long int p)
{
	int i,l;
	for(i=1;i<27;i++)
	{
		if(v[i]>0 && i>p)
		{
			return 1;
		}
	}
	return 0;
}

int find1(long long int v[],long long int q)
{
	int i,p,fuck,flag=0;
	for(i=1;i<27;i++)
	{
		if(v[i]>0)
		{
			fuck = i;
			break;
		}
	}
	for(i=1;i<27;i++)
	{
		if(i>q && v[i]>0)
		{
		   p = i;
		   flag = 1;
		   break;
		}
	}
	if(flag==1)
	return p;
	else
	return fuck;
}


#include<bits/stdc++.h>
using namespace std;
int find(long long int v[],long long int p);
int find1(long long int v[],long long int q);
int main()
{
	string a;
	long long int n,k,i,t,p,h=0,q,m,m1;
	cin>>n>>k>>a;
	long long int out[k];

	long long int v[27] = {0};

	for(i=0;i<n;i++)
	{
		t = (int)a[i];
		t= t - 96;
		v[t]++;
	}

	for(i=1;i<26;i++)
	{
		if(v[i] == 1)
		{
			m1 = i;
			break;
		}
	}

	if(n>=k)
	{
		for(i=0;i<k;i++)
		{
			q = (int)a[i];       // current iterator
			q = q - 96;

			p = (int)a[i+1];     //next immediate iterator
			p = p - 96;

			h = find(v,p);           //returns whether a (int)alpahbet immediately greater than the current one is there
			                        // h=1,element present/h=0,greater alapahaet aint fucking present
			if(i == n-1)          //applicable only for last fucking element
			{
			   out[i] = m1+96;
			}
			else
			{
			  if(h==1)
		    	{
			    	out[i] = q+96;
			    }
			  else if(h==0)
			    {
			    	m = find1(v,q);
			     	out[i] = m+96;
			    }
		    }
		}
	}
	else
	{
	  for(i=0;i<k;i++)
	  {
	  	if(i+1 >= n)
	  	{
	  	 out[i] = m1+96;
		}
		else
		{
	  	 out[i] = a[i];
	    }
	  }
	}

	for(i=0;i<k;i++)
	cout<<(char)out[i];
}

int find(long long int v[],long long int p)
{
	int i,l;
	for(i=1;i<27;i++)
	{
		if(v[i]>0 && i>p)
		{
			return 1;
		}
	}
	return 0;
}

int find1(long long int v[],long long int q)
{
	int i,p,fuck,flag=0;
	for(i=1;i<27;i++)
	{
		if(v[i]>0)
		{
			fuck = i;
			break;
		}
	}
	for(i=1;i<27;i++)
	{
		if(i>q && v[i]>0)
		{
		   p = i;
		   flag = 1;
		   break;
		}
	}
	if(flag==1)
	return p;
	else
	return fuck;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#define ll long long
using namespace std;
ll n,k;
char s[100010],mmax,mmin;
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        scanf("%s",s+1);
        if(n<k)
        {
            printf("%s",s+1);
            mmin=s[1];
            for(ll i=1;i<=n;i++)
            {
               if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=1;i<=k-n;i++)printf("%c",mmin);
            printf("\n");
        }
        else if(n>k)
        {
            ll f=0;
            char c;
            for(ll i=1;i<k;i++)printf("%c",s[i]);
            c=s[k];
            for(ll i=1;i<=n;i++)
            {
                if(s[i]>c)
                {
                    if(f==0)
                    {
                        f=1;
                        c=s[i];
                    }
                    else
                    {
                        if(s[i]<c)c=s[i];
                    }
                }
            }
            printf("%c\n",c);
        }
        else
        {
            mmax=s[1];
            mmin=s[1];
            ll p,f=0;
            for(ll i=1;i<=n;i++)
            {
                if(s[i]>mmax)mmax=s[i];
                if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=n;i>=1;i--)
            {
                if(s[i]==mmax)continue;
                p=i;
                break;
            }
            char c=s[p];
            for(ll i=1;i<=n;i++)
            {
                if(s[i]>c)
                {
                    if(f==0)
                    {
                        f=1;
                        c=s[i];
                    }
                    else
                    {
                        if(s[i]<c)c=s[i];
                    }
                }
            }
            for(ll i=1;i<p;i++)printf("%c",s[i]);
            printf("%c",c);
            for(ll i=p+1;i<=n;i++)printf("%c",mmin);
            printf("\n");
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
char s[100010],mmin,mmax,p,tmp;
ll n,k;
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        scanf("%s",s+1);
        mmax='a';
        mmin='z';
        if(k>n)
        {
            printf("%s",s+1);
            mmin=s[1];
            for(ll i=1;i<=n;i++)mmin=min(mmin,s[i]);
            for(ll i=1;i<=k-n;i++)printf("%c",mmin);
            printf("\n");
            continue;
        }
        else
        {
            for(ll i=1;i<=n;i++)
            {
                mmax=max(mmax,s[i]);
                mmin=min(mmin,s[i]);
            }
            ll flag=0;
            p=0;
            for(ll i=k;i>=1;i--)
            {
                if(s[i]==mmax)continue;
                tmp=s[i];
                for(ll j=1;j<=n;j++)
                {
                    if(s[j]>tmp)
                    {
                        if(flag==0)
                        {
                            flag=1;
                            tmp=s[j];
                        }
                        else
                        {
                            tmp=min(tmp,s[j]);
                        }
                    }
                }
                s[i]=tmp;
                p=i;
                break;
            }
            for(ll i=p+1;i<=k;i++)s[i]=mmin;
            for(ll i=1;i<=k;i++)printf("%c",s[i]);
            printf("\n");
            continue;
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#define ll long long
using namespace std;
ll n,k,vis[30];
char s[100010],mmax,mmin;
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        scanf("%s",s+1);
        memset(vis,0,sizeof(vis));
        for(ll i=1;i<=n;i++)vis[s[i]-'a'+1]=1;
        if(n<k)
        {
            printf("%s",s+1);
            mmin=s[1];
            for(ll i=1;i<=n;i++)
            {
               if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=1;i<=k-n;i++)printf("%c",mmin);
            printf("\n");
        }
        else if(n>k)
        {
            ll c=s[k]-'0'+1;
            for(ll i=1;i<k;i++)printf("%c",s[i]);
            for(ll i=c+1;i<=26;i++)
            {
                if(vis[i]==1)
                {
                    c=i;
                    break;
                }
            }
            printf("%c\n",c+'a'-1);
        }
        else
        {
            mmax=s[1];
            ll p,c;
            for(ll i=1;i<=n;i++)
            {
                if(s[i]>mmax)mmax=s[i];
                if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=n;i>=1;i--)
            {
                if(s[i]==mmax)continue;
                p=i;
                break;
            }
            c=s[p]-'a'+1;
            for(ll i=c+1;i<=26;i++)
            {
                if(vis[i]==1)
                {
                    c=i;
                    break;
                }
            }
            for(ll i=1;i<p;i++)printf("%c",s[i]);
            printf("%c",c+'a'-1);
            for(ll i=p+1;i<=n;i++)printf("%c",mmin);
            printf("\n");
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
char s[100010],tmp;
ll n,k;
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        scanf("%s",s+1);
        if(k>n)
        {
            printf("%s",s+1);
            tmp=s[1];
            for(ll i=1;i<=n;i++)tmp=min(s[i],tmp);
            for(ll i=1;i<=k-n;i++)printf("%c",tmp);
            printf("\n");
            continue;
        }
        else if(k==n)
        {
            next_permutation(s+1,s+n+1);
            tmp=s[n];
            for(ll i=1;i<=n;i++)tmp=min(s[i],tmp);
            for(ll i=n;i>1;i--)
            {
                if(s[i]==s[i-1])s[i]=tmp;
                else
                {
                    s[i]=tmp;
                    break;
                }
            }
            printf("%s\n",s+1);
            continue;
        }
        else
        {
            for(ll i=1;i<k;i++)printf("%c",s[i]);
            printf("%c\n",s[n]);
            continue;
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#define ll long long
using namespace std;
ll n,k,vis[30];
char s[100010],mmax,mmin;
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        scanf("%s",s+1);
        memset(vis,0,sizeof(vis));
        for(ll i=1;i<=n;i++)vis[s[i]-'a'+1]=1;
        if(n<k)
        {
            printf("%s",s+1);
            mmin=s[1];
            for(ll i=1;i<=n;i++)
            {
               if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=1;i<=k-n;i++)printf("%c",mmin);
            printf("\n");
        }
        else if(n>k)
        {
            ll c=s[k]-'a'+1;
            for(ll i=1;i<k;i++)printf("%c",s[i]);
            for(ll i=c+1;i<=26;i++)
            {
                if(vis[i]==1)
                {
                    c=i;
                    break;
                }
            }
            printf("%c\n",c+'a'-1);
        }
        else
        {
            mmax=s[1];
            mmin=s[1];
            ll p,c;
            for(ll i=1;i<=n;i++)
            {
                if(s[i]>mmax)mmax=s[i];
                if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=n;i>=1;i--)
            {
                if(s[i]==mmax)continue;
                p=i;
                break;
            }
            c=s[p]-'a'+1;
            for(ll i=c+1;i<=26;i++)
            {
                if(vis[i]==1)
                {
                    c=i;
                    break;
                }
            }
            for(ll i=1;i<p;i++)printf("%c",s[i]);
            printf("%c",c+'a'-1);
            for(ll i=p+1;i<=n;i++)printf("%c",mmin);
            printf("\n");
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
char s[100010],mmin,mmax,tmp;
ll n,k;
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        scanf("%s",s+1);
        mmax='a';
        mmin='z';
        if(k>n)
        {
            printf("%s",s+1);
            mmin=s[1];
            for(ll i=1;i<=n;i++)mmin=min(mmin,s[i]);
            for(ll i=1;i<=k-n;i++)printf("%c",mmin);
            printf("\n");
            continue;
        }
        else
        {
            for(ll i=1;i<=n;i++)
            {
                mmax=max(mmax,s[i]);
                mmin=min(mmin,s[i]);
            }
            ll flag=0;
            for(ll i=k;i>=1;i--)
            {
                if(s[i]==mmax){s[i]=mmin;continue;}
                tmp=s[i];
                for(ll j=1;j<=n;j++)
                {
                    if(s[j]>tmp)
                    {
                        if(flag==0)
                        {
                            flag=1;
                            tmp=s[j];
                        }
                        else
                        {
                            tmp=min(tmp,s[j]);
                        }
                    }
                }
                s[i]=tmp;
                break;
            }
            for(ll i=1;i<=k;i++)printf("%c",s[i]);
            printf("\n");
            continue;
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#define ll long long
using namespace std;
ll n,k,vis[30];
char s[100010],mmax,mmin;
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        scanf("%s",s+1);
        memset(vis,0,sizeof(vis));
        for(ll i=1;i<=n;i++)vis[s[i]-'a'+1]=1;
        if(n<k)
        {
            printf("%s",s+1);
            mmin=s[1];
            for(ll i=1;i<=n;i++)
            {
               if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=1;i<=k-n;i++)printf("%c",mmin);
            printf("\n");
        }
        else if(n>k)
        {
            ll c=s[k]-'0'+1;
            for(ll i=1;i<k;i++)printf("%c",s[i]);
            for(ll i=c+1;i<=26;i++)
            {
                if(vis[i]==1)
                {
                    c=i;
                    break;
                }
            }
            printf("%c\n",c+'a'-1);
        }
        else
        {
            mmax=s[1];
            mmin=s[1];
            ll p,c;
            for(ll i=1;i<=n;i++)
            {
                if(s[i]>mmax)mmax=s[i];
                if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=n;i>=1;i--)
            {
                if(s[i]==mmax)continue;
                p=i;
                break;
            }
            c=s[p]-'a'+1;
            for(ll i=c+1;i<=26;i++)
            {
                if(vis[i]==1)
                {
                    c=i;
                    break;
                }
            }
            for(ll i=1;i<p;i++)printf("%c",s[i]);
            printf("%c",c+'a'-1);
            for(ll i=p+1;i<=n;i++)printf("%c",mmin);
            printf("\n");
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#define ll long long
using namespace std;
ll n,k,vis[30],ss;
char s[100010],mmax,mmin;
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        scanf("%s",s+1);
        memset(vis,0,sizeof(vis));
        for(ll i=1;i<=n;i++)vis[s[i]-'a'+1]=1;
        if(n<k)
        {
            printf("%s",s+1);
            mmin=s[1];
            for(ll i=1;i<=n;i++)
            {
               if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=1;i<=k-n;i++)printf("%c",mmin);
            printf("\n");
        }
        else if(n>k)
        {
            ll c=s[k]-'0'+1;
            for(ll i=1;i<k;i++)printf("%c",s[i]);
            for(ll i=c+1;i<=26;i++)
            {
                if(vis[i]==1)
                {
                    c=i;
                    break;
                }
            }
            char c1=c-1+'a';
            printf("%c\n",c1);
        }
        else
        {
            mmax=s[1];
            ll p,c;
            for(ll i=1;i<=n;i++)
            {
                if(s[i]>mmax)mmax=s[i];
                if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=n;i>=1;i--)
            {
                if(s[i]==mmax)continue;
                p=i;
                break;
            }
            c=s[p]-'a'+1;
            for(ll i=c+1;i<=26;i++)
            {
                if(vis[i]==1)
                {
                    c=i;
                    break;
                }
            }
            for(ll i=1;i<p;i++)printf("%c",s[i]);
            char c1=c-1+'a';
            printf("%c",c1);
            for(ll i=p+1;i<=n;i++)printf("%c",mmin);
            printf("\n");
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
char s[100010],s1[100010],mmin,mmax,tmp;
ll n,k;
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        scanf("%s",s+1);
        mmax='a';
        mmin='z';
        if(k>n)
        {
            printf("%s",s+1);
            mmin=s[1];
            for(ll i=1;i<=n;i++)mmin=min(mmin,s[i]);
            for(ll i=1;i<=k-n;i++)printf("%c",mmin);
            printf("\n");
            continue;
        }
        else
        {
            for(ll i=1;i<=n;i++)
            {
                mmax=max(mmax,s[i]);
                mmin=min(mmin,s[i]);
            }
            strcpy(s1+1,s+1);
            ll flag=0;
            for(ll i=k;i>=1;i--)
            {
                if(s[i]==mmax){s[i]=mmin;continue;}
                tmp=s[i];
                for(ll j=1;j<=n;j++)
                {
                    if(s1[j]>tmp)
                    {
                        if(flag==0)
                        {
                            flag=1;
                            tmp=s1[j];
                        }
                        else
                        {
                            tmp=min(tmp,s1[j]);
                        }
                    }
                }
                s[i]=tmp;
                break;
            }
            for(ll i=1;i<=k;i++)printf("%c",s[i]);
            printf("\n");
            continue;
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#define ll long long
using namespace std;
ll n,k,vis[30],ss;
char s[100010],mmax,mmin;
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        scanf("%s",s+1);
        memset(vis,0,sizeof(vis));
        for(ll i=1;i<=n;i++)vis[s[i]-'a'+1]=1;
        if(n<k)
        {
            printf("%s",s+1);
            mmin=s[1];
            for(ll i=1;i<=n;i++)
            {
               if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=1;i<=k-n;i++)printf("%c",mmin);
            printf("\n");
        }
        else if(n>k)
        {
            ll c=s[k]-'0'+1;
            for(ll i=1;i<k;i++)printf("%c",s[i]);
            for(ll i=c+1;i<=26;i++)
            {
                if(vis[i]==1)
                {
                    c=i;
                    break;
                }
            }
            printf("%c\n",c+'a'-1);
        }
        else
        {
            mmax=s[1];
            ll p,c;
            for(ll i=1;i<=n;i++)
            {
                if(s[i]>mmax)mmax=s[i];
                if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=n;i>=1;i--)
            {
                if(s[i]==mmax)continue;
                p=i;
                break;
            }
            c=s[p]-'a'+1;
            for(ll i=c+1;i<=26;i++)
            {
                if(vis[i]==1)
                {
                    c=i;
                    break;
                }
            }
            for(ll i=1;i<p;i++)printf("%c",s[i]);
            printf("%c",c+'a'-1);
            for(ll i=p+1;i<=n;i++)printf("%c",mmin);
            printf("\n");
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
char s[100010],mmin,mmax,p,tmp;
ll n,k;
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        scanf("%s",s+1);
        mmax='a';
        mmin='z';
        if(k>n)
        {
            printf("%s",s+1);
            mmin=s[1];
            for(ll i=1;i<=n;i++)mmin=min(mmin,s[i]);
            for(ll i=1;i<=k-n;i++)printf("%c",mmin);
            printf("\n");
            continue;
        }
        else
        {
            for(ll i=1;i<=n;i++)
            {
                mmax=max(mmax,s[i]);
                mmin=min(mmin,s[i]);
            }
            ll flag=0;
            for(ll i=k;i>=1;i--)
            {
                if(s[i]==mmax)continue;
                tmp=s[i];
                for(ll j=1;j<=n;j++)
                {
                    if(s[j]>tmp)
                    {
                        if(flag==0)
                        {
                            flag=1;
                            tmp=s[j];
                        }
                        else
                        {
                            tmp=min(tmp,s[j]);
                        }
                    }
                }
                s[i]=tmp;
                p=i;
                break;
            }
            for(ll i=p+1;i<=k;i++)s[i]=mmin;
            for(ll i=1;i<=k;i++)printf("%c",s[i]);
            printf("\n");
            continue;
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#define ll long long
using namespace std;
ll n,k,vis[30],ss;
char s[100010],mmax,mmin;
int main()
{
    while(~scanf("%lld%lld",&n,&k))
    {
        scanf("%s",s+1);
        memset(vis,0,sizeof(vis));
        for(ll i=1;i<=n;i++)vis[s[i]-'a'+1]=1;
        if(n<k)
        {
            printf("%s",s+1);
            mmin=s[1];
            for(ll i=1;i<=n;i++)
            {
               if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=1;i<=k-n;i++)printf("%c",mmin);
            printf("\n");
        }
        else if(n>k)
        {
            ll c=s[k]-'0'+1;
            for(ll i=1;i<k;i++)printf("%c",s[i]);
            for(ll i=c+1;i<=26;i++)
            {
                if(vis[i]==1)
                {
                    c=i;
                    break;
                }
            }
            char c1=c+'a'-1;
            printf("%c\n",c1);
        }
        else
        {
            mmax=s[1];
            ll p,c;
            for(ll i=1;i<=n;i++)
            {
                if(s[i]>mmax)mmax=s[i];
                if(s[i]<mmin)mmin=s[i];
            }
            for(ll i=n;i>=1;i--)
            {
                if(s[i]==mmax)continue;
                p=i;
                break;
            }
            c=s[p]-'a'+1;
            for(ll i=c+1;i<=26;i++)
            {
                if(vis[i]==1)
                {
                    c=i;
                    break;
                }
            }
            for(ll i=1;i<p;i++)printf("%c",s[i]);
            char c1=c+'a'-1;
            printf("%c",c1);
            for(ll i=p+1;i<=n;i++)printf("%c",mmin);
            printf("\n");
        }
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef __int64 ll;

const int N = 5000+5;
int n, dp[N];
ll a[N];

int get(ll &cur) {
    int ret = 0;
    while(cur%2 == 0)
        cur /= 2, ret++;
    return ret;
}

int main() {
    scanf("%d", &n);
    for(int i = 1;i <= n; i++) {
        scanf("%I64d", &a[i]);
    }
    for(int i = 1;i <= n; i++) {
        dp[i] = i-1;
        for(int j = 1;j < i; j++) {
            ll cur1 = a[i], cur2 = a[j];
            int cnt1 = get(cur1), cnt2 = get(cur2);
            if(cur2%cur1)   continue;
            if(cnt1 == 0) {
                if(cnt2 > 0)    dp[i] = min(dp[i], dp[j]+i-j-1);
            }
            else if(cnt2 + i-j == cnt1) {
                dp[i] = min(dp[i], dp[j]+i-j-1);
            }
        }
    }
    int ans = n;
    for(int i = 1;i <= n; i++)  ans = min(ans, dp[i] + n-i);
    printf("%d\n", ans);
    return 0;
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<ll,pll> pLL;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<27)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-5
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5001];
short dp[5001][5001][3];
/*
  dp[i][j][k] ... k==0 - > A[i]=default_A[j]
                  k==1 - > A[i]=default_A[j]/2
                  k==2 - > A[i]=default_A[j]*2
*/
ll sol(int id,int p)
{
	if(p==0)return A[id];
	if(p==1)return A[id]/2;
	if(p==2)return A[id]*2;
}
ll sol_t[5001][3];

int main()
{
	scanf("%d",&N);
	rep(i,N)scanf("%I64d",&A[i]);
	A[N++]=1;
	rep(i,N)rep(k,3)sol_t[i][k]=sol(i,k);
	rep(i,N)rep(j,N)rep(k,3)dp[i][j][k]=31000;
	dp[N-1][N-1][0]=0;
	for(int i=N-2;i>=0;i--)
	{
		repn(j,i+1,N)rep(k,3)if(dp[i+1][j][k]<31000)
		{
			dp[i][j][k]=dp[i+1][j][k]+1;
			ll p1=sol_t[j][k];
			if(p1%2==0)
			{
				rep(l,3)
				{
					if(l==1&&A[i]%2==1)continue;
					if(sol_t[i][l]%(p1/2)!=0)continue;
					short nw=dp[i+1][j][k]+((l!=0)?1:0);
					if(dp[i][i][l]>nw)dp[i][i][l]=nw;
				}
			}
			else
			{
				rep(l,3)
				{
					if(l==1&&A[i]%2==1)continue;
					if(sol_t[i][l]%p1!=0)continue;
					short nw=dp[i+1][j][k]+((l!=0)?1:0);
					if(dp[i][i][l]>nw)dp[i][i][l]=nw;
				}
			}
		}
	}
	short ans=31100;
	rep(j,N)rep(k,3)MIN(ans,dp[0][j][k]);
	printf("%d\n",ans);
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<pii,ll> ppl;
typedef pair<ll,pii> plp;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<double,int> pdi;
typedef pair<double,pii> pdp;
typedef pair<double,double> pdd;
typedef pair<pdd,double> pd3;
typedef vector<double> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<28)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-8
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5000];
int dp[5000];
int p2[5000];

int main()
{
    scanf("%d",&N);
    rep(i,N)cin>>A[i];
    rep(i,N/2)swap(A[i],A[N-i-1]);
    rep(i,N)
    {
        ll tt=A[i];
        while(tt%2==0)tt/=2,p2[i]++;
    }
    rep(i,N)dp[i]=i;
    rep(i,N)
    {
        repn(j,i+1,N)
        {
            if(j<=p2[i]+i)
            {
                ll s=A[i]>>ll(p2[i]-(j-i));
                if(A[j]%(s*2)==s)MIN(dp[j],dp[i]+j-i-1);
            }
            else
            {
                ll s=A[i]>>ll(p2[i]);
                if(A[j]%s==0)MIN(dp[j],dp[i]+j-i-1);
            }
        }
    }
    int res=INF;
    rep(i,N)MIN(res,dp[i]+N-i-1);
    printf("%d\n",res);
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<ll,pll> pLL;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<27)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-5
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5001];
short dp[5001][5001][3];
/*
  dp[i][j][k] ... k==0 - > A[i]=default_A[j]
                  k==1 - > A[i]=default_A[j]/2
                  k==2 - > A[i]=default_A[j]*2
*/
ll solve(int id,int p)
{
	if(p==0)return A[id];
	if(p==1)return A[id]/2;
	if(p==2)return A[id]*2;
}

int main()
{
	scanf("%d",&N);
	rep(i,N)scanf("%I64d",&A[i]);
	A[N++]=1;
	rep(i,N)rep(j,N)rep(k,3)dp[i][j][k]=31000;
	dp[N-1][N-1][0]=0;
	for(int i=N-2;i>=0;i--)
	{
		repn(j,i+1,N)rep(k,3)if(dp[i+1][j][k]<31000)
		{
			dp[i][j][k]=dp[i+1][j][k]+1;
			ll p1=solve(j,k);
			rep(l,3)
			{
				if(l==1&&A[i]%2==1)continue;
				ll p2=solve(i,l);
				if(p1%2==0&&p2%(p1/2)!=0)continue;
				if(p1%2==1&&p2%p1!=0)continue;
				short nw=dp[i+1][j][k]+short((l!=0)?1:0);
				if(dp[i][i][l]>nw)dp[i][i][l]=nw;
			}
		}
	}
	short ans=31100;
	rep(j,N)rep(k,3)MIN(ans,dp[0][j][k]);
	printf("%d\n",ans);
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<ll,pll> pLL;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<27)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-5
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5001];
int dp[2][5001][3];
/*
  dp[i][j][k] ... k==0 - > A[i]=default_A[j]
                  k==1 - > A[i]=default_A[j]/2
                  k==2 - > A[i]=default_A[j]*2
*/
ll sol(int id,int p)
{
	if(p==0)return A[id];
	if(p==1)return A[id]/2;
	if(p==2)return A[id]*2;
}
ll sol_t[5001][3];

int main()
{
	scanf("%d",&N);
	rep(i,N)scanf("%I64d",&A[i]);
	A[N++]=1;
	rep(i,N)rep(k,3)sol_t[i][k]=sol(i,k);
	rep(j,N)rep(k,3)dp[0][j][k]=31000;
	dp[0][N-1][0]=0;
	int cur=0,nxt=1;
	for(int i=N-2;i>=0;i--)
	{
		rep(j,N)rep(k,3)dp[nxt][j][k]=31000;
		bool cant=(A[i]%2==1);
		repn(j,i+1,N)rep(k,3)if(dp[cur][j][k]<31000)
		{
			dp[nxt][j][k]=dp[cur][j][k]+1;
			ll p1=sol_t[j][k];
			if(p1%2==0)
			{
				p1/=2;
				rep(l,3)if((l!=1||!cant)&&sol_t[i][l]%p1==0)
					MIN(dp[nxt][i][l],dp[cur][j][k]+(l!=0));
			}
			else
			{
				rep(l,3)if((l!=1||!cant)&&sol_t[i][l]%p1==0)
					MIN(dp[nxt][i][l],dp[cur][j][k]+(l!=0));
			}
		}
		swap(cur,nxt);
	}
	int ans=31100;
	rep(j,N)rep(k,3)MIN(ans,dp[cur][j][k]);
	printf("%d\n",ans);
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<ll,pll> pLL;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<27)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-5
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5001];
short dp[2][5001][3];
/*
  dp[i][j][k] ... k==0 - > A[i]=default_A[j]
                  k==1 - > A[i]=default_A[j]/2
                  k==2 - > A[i]=default_A[j]*2
*/
ll sol(int id,int p)
{
	if(p==0)return A[id];
	if(p==1)return A[id]/2;
	if(p==2)return A[id]*2;
}
ll sol_t[5001][3];

int main()
{
	scanf("%d",&N);
	rep(i,N)scanf("%I64d",&A[i]);
	A[N++]=1;
	rep(i,N)rep(k,3)sol_t[i][k]=sol(i,k);
	rep(j,N)rep(k,3)dp[0][j][k]=31000;
	dp[0][N-1][0]=0;
	int cur=0,nxt=1;
	for(int i=N-2;i>=0;i--)
	{
		rep(j,N)rep(k,3)dp[nxt][j][k]=31000;
		bool cant=(A[i]%2==1);
		repn(j,i+1,N)rep(k,3)if(dp[cur][j][k]<31000)
		{
			dp[nxt][j][k]=dp[cur][j][k]+1;
			ll p1=sol_t[j][k];
			if(p1%2==0)
			{
				p1/=2;
				rep(l,3)if((l!=1||!cant)&&sol_t[i][l]%p1==0)
				{
					short nw=dp[cur][j][k]+((l!=0)?1:0);

					if(dp[nxt][i][l]>nw)dp[nxt][i][l]=nw;
				}
			}
			else
			{
				rep(l,3)if((l!=1||!cant)&&sol_t[i][l]%p1==0)
				{
					short nw=dp[cur][j][k]+((l!=0)?1:0);
					if(dp[nxt][i][l]>nw)dp[nxt][i][l]=nw;
				}
			}
		}
		swap(cur,nxt);
	}
	short ans=31100;
	rep(j,N)rep(k,3)MIN(ans,dp[cur][j][k]);
	printf("%d\n",ans);
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<pii,ll> ppl;
typedef pair<ll,pii> plp;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<double,int> pdi;
typedef pair<double,pii> pdp;
typedef pair<double,double> pdd;
typedef pair<pdd,double> pd3;
typedef vector<double> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<28)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-8
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5000];
int dp[5000];
int p2[5000];

int main()
{
    scanf("%d",&N);
    rep(i,N)cin>>A[i];
    rep(i,N/2)swap(A[i],A[N-i-1]);
    rep(i,N)
    {
        ll tt=A[i];
        while(tt%2==0)tt/=2,p2[i]++;
    }
    rep(i,N)dp[i]=i;
    rep(i,N)
    {
        repn(j,i+1,N)
        {
            if(j<=p2[i]+i)
            {
                ll s=A[i]>>ll(j-i);
                if(A[j]%(s*2)==s)MIN(dp[j],dp[i]+j-i-1);
            }
            else
            {
                ll s=A[i]>>ll(p2[i]);
                if(A[j]%s==0)MIN(dp[j],dp[i]+j-i-1);
            }
        }
    }
    int res=INF;
    rep(i,N)MIN(res,dp[i]+N-i-1);
    printf("%d\n",res);
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<pii,ll> ppl;
typedef pair<ll,pii> plp;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<double,int> pdi;
typedef pair<double,pii> pdp;
typedef pair<double,double> pdd;
typedef pair<pdd,double> pd3;
typedef vector<double> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<28)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-8
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5000];
int dp[5000];
int p2[5000];

int main()
{
    scanf("%d",&N);
    rep(i,N)cin>>A[i];
    reverse(A,A+N);
    rep(i,N)
    {
        ll tt=A[i];
        while(tt%2==0)tt/=2,p2[i]++;
    }
    rep(i,N)dp[i]=i;
    rep(i,N)
    {
        repn(j,i+1,N)
        {
            if(j<=p2[i]+i)
            {
                ll s=A[i]>>ll(p2[i]-(j-i));
                if(A[j]%(s*2)==s)MIN(dp[j],dp[i]+j-i-1);
            }
            else
            {
                ll s=A[i]>>ll(p2[i]);
                if(A[j]%s==0)MIN(dp[j],dp[i]+j-i-1);
            }
        }
    }
    int res=INF;
    rep(i,N)MIN(res,dp[i]+N-i-1);
    printf("%d\n",res);
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<ll,pll> pLL;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<27)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-5
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5001];
short dp[5001][5001][3];
/*
  dp[i][j][k] ... k==0 - > A[i]=default_A[j]
                  k==1 - > A[i]=default_A[j]/2
                  k==2 - > A[i]=default_A[j]*2
*/
ll solve(int id,int p)
{
	if(p==0)return A[id];
	if(p==1)return A[id]/2;
	if(p==2)return A[id]*2;
}

int main()
{
	scanf("%d",&N);
	rep(i,N)scanf("%I64d",&A[i]);
	A[N++]=1;
	rep(i,N)rep(j,N)rep(k,3)dp[i][j][k]=31000;
	dp[N-1][N-1][0]=0;
	for(int i=N-2;i>=0;i--)
	{
		rep(j,N)rep(k,3)if(dp[i+1][j][k]<31000)
		{
			dp[i][j][k]=dp[i+1][j][k]+1;
			ll p1=solve(j,k);
			rep(l,3)
			{
				if(l==1&&A[i]%2==1)continue;
				ll p2=solve(i,l);
				if(p1%2==0&&p2%(p1/2)!=0)continue;
				if(p1%2==1&&p2%p1!=0)continue;
				short nw=dp[i+1][j][k]+short((l!=0)?1:0);
				if(dp[i][i][l]>nw)dp[i][i][l]=nw;
			}
		}
	}
	short ans=31100;
	rep(j,N)rep(k,3)MIN(ans,dp[0][j][k]);
	printf("%d\n",ans);
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<ll,pll> pLL;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<27)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-5
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5001];
int dp[5001][5001][3];
/*
  dp[i][j][k] ... k==0 - > A[i]=default_A[j]
                  k==1 - > A[i]=default_A[j]/2
                  k==2 - > A[i]=default_A[j]*2
*/
ll solve(int id,int p)
{
	if(p==0)return A[id];
	if(p==1)return A[id]/2;
	if(p==2)return A[id]*2;
}

int main()
{
	scanf("%d",&N);
	rep(i,N)scanf("%I64d",&A[i]);
	A[N++]=1;
	rep(i,N)rep(j,N)rep(k,3)dp[i][j][k]=INF;
	dp[N-1][N-1][0]=0;
	for(int i=N-2;i>=0;i--)
	{
		rep(j,N)rep(k,3)if(dp[i+1][j][k]<INF)
		{
			dp[i][j][k]=min(dp[i][j][k],dp[i+1][j][k]+1);
			ll p1=solve(j,k);
			rep(l,3)
			{
				if(l==1&&A[i]%2==1)continue;
				ll p2=solve(i,l);
				if(p1%2==0&&p2%(p1/2)!=0)continue;
				if(p1%2==1&&p2%p1!=0)continue;
				dp[i][i][l]=min(dp[i][i][l],dp[i+1][j][k]+(l!=0));
			}
		}
	}
	int ans=INF;
	rep(j,N)rep(k,3)MIN(ans,dp[0][j][k]);
	printf("%d\n",ans);
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<pii,ll> ppl;
typedef pair<ll,pii> plp;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<double,int> pdi;
typedef pair<double,pii> pdp;
typedef pair<double,double> pdd;
typedef pair<pdd,double> pd3;
typedef vector<double> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<28)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-8
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5000];
int dp[5000];
int p2[5000];

int main()
{
    scanf("%d",&N);
    rep(i,N)cin>>A[i];
    reverse(A,A+N);
    rep(i,N)
    {
        ll tt=A[i];
        while(tt%2==0)tt/=2,p2[i]++;
    }
    rep(i,N)dp[i]=1;
    rep(i,N)
    {
        repn(j,i+1,N)
        {
            if(j<=p2[i]+i)
            {
                ll s=A[i]>>(p2[i]-(j-i));
                if(A[j]%(s*2)==s)MAX(dp[j],dp[i]+1);
            }
            else
            {
                ll s=A[i]>>p2[i];
                if(A[j]%s==0)MAX(dp[j],dp[i]+1);
            }
        }
    }
    int res=INF;
    rep(i,N)MIN(res,N-dp[i]);
    printf("%d\n",res);
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<pii,ll> ppl;
typedef pair<ll,pii> plp;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<double,int> pdi;
typedef pair<double,pii> pdp;
typedef pair<double,double> pdd;
typedef pair<pdd,double> pd3;
typedef vector<double> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<28)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-8
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5000];
int dp[5000];
int p2[5000];

int main()
{
    scanf("%d",&N);
    rep(i,N)cin>>A[i];
    reverse(A,A+N);
    rep(i,N)
    {
        ll tt=A[i];
        while(tt%2==0)tt/=2,p2[i]++;
    }
    rep(i,N)dp[i]=i;
    rep(i,N)
    {
        repn(j,i+1,N)
        {
            if(j<=p2[i]+i)
            {
                ll s=A[i]>>(p2[i]-(j-i));
                if(A[j]%(s+2)==s)MIN(dp[j],dp[i]+j-i-1);
            }
            else
            {
                ll s=A[i]>>p2[i];
                if(A[j]%s==0)MIN(dp[j],dp[i]+j-i-1);
            }
        }
    }
    int res=INF;
    rep(i,N)MIN(res,dp[i]+N-i-1);
    printf("%d\n",res);
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<ll,pll> pLL;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<27)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-5
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5001];
short dp[5001][5001][3];
/*
  dp[i][j][k] ... k==0 - > A[i]=default_A[j]
                  k==1 - > A[i]=default_A[j]/2
                  k==2 - > A[i]=default_A[j]*2
*/
ll sol(int id,int p)
{
	if(p==0)return A[id];
	if(p==1)return A[id]/2;
	if(p==2)return A[id]*2;
}
ll sol_t[5001][3];

int main()
{
	scanf("%d",&N);
	rep(i,N)scanf("%I64d",&A[i]);
	A[N++]=1;
	rep(i,N)rep(k,3)sol_t[i][k]=sol(i,k);
	rep(i,N)rep(j,N)rep(k,3)dp[i][j][k]=31000;
	dp[N-1][N-1][0]=0;
	for(int i=N-2;i>=0;i--)
	{
		bool cant=(A[i]%2==1);
		repn(j,i+1,N)rep(k,3)if(dp[i+1][j][k]<31000)
		{
			dp[i][j][k]=dp[i+1][j][k]+1;
			ll p1=sol_t[j][k];
			if(p1%2==0)
			{
				rep(l,3)if((l!=1||!cant)&&sol_t[i][l]%(p1/2)==0)
				{
					short nw=dp[i+1][j][k]+((l!=0)?1:0);
					if(dp[i][i][l]>nw)dp[i][i][l]=nw;
				}
			}
			else
			{
				rep(l,3)if((l!=1||!cant)&&sol_t[i][l]%p1==0)
				{
					short nw=dp[i+1][j][k]+((l!=0)?1:0);
					if(dp[i][i][l]>nw)dp[i][i][l]=nw;
				}
			}
		}
	}
	short ans=31100;
	rep(j,N)rep(k,3)MIN(ans,dp[0][j][k]);
	printf("%d\n",ans);
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<ll,pll> pLL;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<27)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-5
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5001];
short dp[5001][5001][2];
/*
  dp[i][j][k] ... k==0 - > A[i]=default_A[j]
                  k==1 - > A[i]=default_A[j]/2
                  k==2 - > A[i]=default_A[j]*2
*/
ll sol(int id,int p)
{
	if(p==0)return A[id];
	if(p==1)return A[id]/2;
}
ll sol_t[5001][2];

int main()
{
	scanf("%d",&N);
	rep(i,N)scanf("%I64d",&A[i]);
	A[N++]=1;
	rep(i,N)rep(k,2)sol_t[i][k]=sol(i,k);
	rep(i,N)rep(j,N)rep(k,2)dp[i][j][k]=31000;
	dp[N-1][N-1][0]=0;
	for(int i=N-2;i>=0;i--)
	{
		repn(j,i+1,N)rep(k,2)if(dp[i+1][j][k]<31000)
		{
			dp[i][j][k]=dp[i+1][j][k]+1;
			ll p1=sol_t[j][k];
			rep(l,2)
			{
				if(l==1&&A[i]%2==1)continue;
				ll p2=sol_t[i][l];
				if(p1%2==0&&p2%(p1/2)!=0)continue;
				if(p1%2==1&&p2%p1!=0)continue;
				short nw=dp[i+1][j][k]+short((l!=0)?1:0);
				if(dp[i][i][l]>nw)dp[i][i][l]=nw;
			}
		}
	}
	short ans=31100;
	rep(j,N)rep(k,2)MIN(ans,dp[0][j][k]);
	printf("%d\n",ans);
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <functional>
#include <algorithm>
#include <math.h>
#include <bitset>
#include <set>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <complex>
#include <numeric>
#include <map>
#include <time.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
typedef pair<ll,pll> pLL;
typedef pair<double,int> pdi;
typedef pair<double,double> pdd;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)
#define repn(i,a,n) for (int (i) = (a); (i) < (n); (i)++)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
#define SORT(x) sort((x).begin(),(x).end())
#define SORTN(x,n) sort((x),(x)+(n))
#define ERASE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define COUNT(x,c) count((x).begin(),(x).end(),(c))
#define REMOVE(x,c) (x).erase(remove((x).begin(),(x).end(),(c)),(x).end())
#define REVERSE(x) reverse((x).begin(),(x).end())
#define FORIT(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))-(x).begin()
#define lb(x,a) lower_bound((x).begin(),(x).end(),(a))
#define LBN(x,a,n) lower_bound((x),(x)+(n),(a))-(x)
#define lbN(x,a,n) lower_bound((x),(x)+(n),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))-(x).begin()
#define ub(x,a) upper_bound((x).begin(),(x).end(),(a))
#define BS(x,a) binary_search((x).begin(),(x).end(),(a))
#define BS2(x,n,a) binary_search((x),(x)+(n),(a))
#define NB(x) (((x)&~((x)+((x)&-(x))))/((x)&-(x))>>1)|((x)+((x)&-(x)))
#define NP(x) next_permutation((x).begin(),(x).end())
#define MM(x,p) memset((x),(p),sizeof(x))
#define SQ(x) (x)*(x)
#define SC(c1,c2) strcmp(c1,c2)==0
#define mp make_pair
#define INF (1<<27)
#define INFL (1LL<<60)
#define fi first
#define se second
#define MOD 1000000007
#define EPS 1e-5
#define MIN(x,a) x=min(x,a)
#define MAX(x,a) x=max(x,a)
#define madd(x,a) x=(x+a)%MOD
#define msub(x,a) x=(x+MOD-a)%MOD

int N;
ll A[5001];
short dp[5001][5001][3];
/*
  dp[i][j][k] ... k==0 - > A[i]=default_A[j]
                  k==1 - > A[i]=default_A[j]/2
                  k==2 - > A[i]=default_A[j]*2
*/
ll sol(int id,int p)
{
	if(p==0)return A[id];
	if(p==1)return A[id]/2;
	if(p==2)return A[id]*2;
}
ll sol_t[5001][3];

int main()
{
	scanf("%d",&N);
	rep(i,N)scanf("%I64d",&A[i]);
	A[N++]=1;
	rep(i,N)rep(k,3)sol_t[i][k]=sol(i,k);
	rep(i,N)rep(j,N)rep(k,3)dp[i][j][k]=31000;
	dp[N-1][N-1][0]=0;
	for(int i=N-2;i>=0;i--)
	{
		repn(j,i+1,N)rep(k,3)if(dp[i+1][j][k]<31000)
		{
			dp[i][j][k]=dp[i+1][j][k]+1;
			ll p1=sol_t[j][k];
			rep(l,3)
			{
				if(l==1&&A[i]%2==1)continue;
				ll p2=sol_t[i][l];
				if(p1%2==0&&p2%(p1/2)!=0)continue;
				if(p1%2==1&&p2%p1!=0)continue;
				short nw=dp[i+1][j][k]+short((l!=0)?1:0);
				if(dp[i][i][l]>nw)dp[i][i][l]=nw;
			}
		}
	}
	short ans=31100;
	rep(j,N)rep(k,3)MIN(ans,dp[0][j][k]);
	printf("%d\n",ans);
}