//Chirfen 2016.8.19

//
//
//http://acm.hust.edu.cn/vjudge/contest/129201#problem/C

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <map>

#define SIZE 500010
#define ll long long

using namespace std;
int n ,m , flag = 0;
char a[1001][1001];
bool vis[1001][1001];
int record[1001][1001];
char code[4] = {'D','I','M','A'};
int dis[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int dfs(int x, int y , int dep, int res)
{
    if(a[x][y] != code[dep]) return res;
    if(vis[x][y] == true) {flag = 1 ; return 0;}
    if(dep == 3) res++;
    if(record[x][y]) return res+record[x][y];
    int temp = res;
    for(int i = 0; i< 4 ; i++)
    {
        int dx = x + dis[i][0] , dy = y + dis[i][1];
        if(dx>= 0 && dx < n && dy >= 0 && dy < m)
        {
            vis[x][y] = true;
            temp=max(temp,dfs(dx,dy,(dep+1)%4,res));
            vis[x][y] = false;
        }
    }
    record[x][y] = temp -res;
    res = temp;
    return res;
}
int main()
{
    cin >> n >>m;
    bool numOfD = 0;
    int ans = 0;
    getchar();
    for(int i = 0 ; i < n ; i++)
    {
        for(int k = 0 ; k < m ; k++)
        {
            scanf("%c",&a[i][k]);
            if(a[i][k] == 'D') numOfD = 1;
        }
        getchar();
    }

    if(!numOfD) {cout << "Poor Dima!"<< endl; return 0;}
    if(numOfD == n*m) {cout << "Poor Dima!"<< endl; return 0;}
    for(int i = 0 ; i < n ; i++)
        for(int k = 0 ; k < m ; k++)
        {
            memset(vis,0,sizeof vis);
            ans = max(ans,dfs(i,k,0,0));
            if(flag) {cout << "Poor Inna!"<< endl;return 0;}
        }

    if(ans == 0) {cout << "Poor Dima!"<< endl; return 0;}
    else {cout << ans << endl;}
    return 0;
}


/*
ID:
PROG:
LANG: C++
 */
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <climits>
#include <cstdlib>
using namespace std;

const double Pi=acos(-1.0);

typedef long long ll;
typedef pair<int, int> pii;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

#define INF (1<<29)+123
#define LL_INF (1<<59)+123

#define MAX_N 1001

int N, M;
string txt = "DIMA";
int dp[MAX_N][MAX_N];
char grid[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N], inUse[MAX_N][MAX_N];

bool isPoint(int x, int y) {
	return x>=0 && x<M && y>=0 && y<N;
}

int dfs(int x, int y, int prv) {
	static int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};

	if (txt[(prv+1)%4] != grid[y][x]) return 0;
	if (inUse[y][x]) return -1;
	if (vis[y][x]) return dp[y][x];

	vis[y][x] = inUse[y][x] = true;
	int cnt = (grid[y][x] == 'A'), cur = 0;
	for (int i = 0; i < 4; i++) {
		int nxtx = x + dir[i][0];
		int nxty = y + dir[i][1];

		if (isPoint(nxtx, nxty)) {
			int tmp = dfs(nxtx, nxty, (prv+1)%4);
			if (tmp==-1) return -1;

			cur = max(cur, tmp);
		}
	}

	inUse[y][x] = false;
	return dp[y][x] = cnt + cur;
}

int main ()
{
	if (fopen("data.in", "r"))
		Rd("data.in");

	cin >> N >> M;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++)
			cin >> grid[i][j];

	int result = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			if (grid[i][j]=='D' && !vis[i][j]) {
				int tmp = dfs(j, i,-1);
				result = max(result, tmp);

				if (tmp==-1) {
					result = -1;
					break;
				}
			}
		}
	}

	if (!result) cout << "Poor Dima!" << endl;
	else if (result==-1) cout << "Poor Inna!" << endl;
	else cout << result << endl;

	return 0;
}


/*
ID:
PROG:
LANG: C++
 */
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <climits>
#include <cstdlib>
using namespace std;

const double Pi=acos(-1.0);

typedef long long ll;
typedef pair<int, int> pii;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

#define INF (1<<29)+123
#define LL_INF (1<<59)+123

#define MAX_N 1001

int N, M;
string txt = "DIMA";
char grid[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];

bool isPoint(int x, int y) {
	return x>=0 && x<M && y>=0 && y<N;
}

int dfs(int x, int y, int prv) {
	static int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};

	if (txt[(prv+1)%4] != grid[y][x]) return 0;
	if (vis[y][x]) return -1;

	vis[y][x] = true;
	int cnt = (grid[y][x] == 'A'), cur = 0;
	for (int i = 0; i < 4; i++) {
		int nxtx = x + dir[i][0];
		int nxty = y + dir[i][1];

		if (isPoint(nxtx, nxty)) {
			int tmp = dfs(nxtx, nxty, (prv+1)%4);
			if (tmp==-1) return -1;

			cur = max(cur, tmp);
		}
	}

	vis[y][x] = false;
	return cnt + cur;
}

int main ()
{
	if (fopen("data.in", "r"))
		Rd("data.in");

	cin >> N >> M;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++)
			cin >> grid[i][j];

	int result = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			if (grid[i][j]=='D') {
				int tmp = dfs(i,j,-1);
				result = max(result, tmp);

				if (tmp==-1) {
					result = -1;
					break;
				}
			}
		}
	}

	if (!result) cout << "Poor Dima!" << endl;
	else if (result==-1) cout << "Poor Inna!" << endl;
	else cout << result << endl;

	return 0;
}


/*
ID:
PROG:
LANG: C++
 */
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <climits>
#include <cstdlib>
using namespace std;

const double Pi=acos(-1.0);

typedef long long ll;
typedef pair<int, int> pii;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

#define INF (1<<29)+123
#define LL_INF (1<<59)+123

#define MAX_N 1001

int N, M;
string txt = "DIMA";
int dp[MAX_N][MAX_N];
char grid[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N], inUse[MAX_N][MAX_N];

bool isPoint(int x, int y) {
	return x>=0 && x<M && y>=0 && y<N;
}

int dfs(int x, int y, int prv) {
	static int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};

	if (txt[(prv+1)%4] != grid[y][x]) return 0;
	if (inUse[y][x]) return -1;
	if (vis[y][x]) return dp[y][x];

	vis[y][x] = inUse[y][x] = true;
	int cnt = (grid[y][x] == 'A'), cur = 0;
	for (int i = 0; i < 4; i++) {
		int nxtx = x + dir[i][0];
		int nxty = y + dir[i][1];

		if (isPoint(nxtx, nxty)) {
			int tmp = dfs(nxtx, nxty, (prv+1)%4);
			if (tmp==-1) return -1;

			cur = max(cur, tmp);
		}
	}

	dp[y][x] = cnt + cur;
	inUse[y][x] = false;
	return cnt + cur;
}

int main ()
{
	if (fopen("data.in", "r"))
		Rd("data.in");

	cin >> N >> M;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++)
			cin >> grid[i][j];

	int result = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			if (grid[i][j]=='D' && !vis[i][j]) {
				int tmp = dfs(i,j,-1);
				result = max(result, tmp);

				if (tmp==-1) {
					result = -1;
					break;
				}
			}
		}
	}

	if (!result) cout << "Poor Dima!" << endl;
	else if (result==-1) cout << "Poor Inna!" << endl;
	else cout << result << endl;

	return 0;
}


/*
ID:
PROG:
LANG: C++
 */
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <climits>
#include <cstdlib>
using namespace std;

const double Pi=acos(-1.0);

typedef long long ll;
typedef pair<int, int> pii;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

#define INF (1<<29)+123
#define LL_INF (1<<59)+123

#define MAX_N 1001

int N, M;
string txt = "DIMA";
int dp[MAX_N][MAX_N];
char grid[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N], inUse[MAX_N][MAX_N];

bool isPoint(int x, int y) {
	return x>=0 && x<M && y>=0 && y<N;
}

int dfs(int x, int y, int prv) {
	static int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};

	if (txt[(prv+1)%4] != grid[y][x]) return 0;
	if (inUse[y][x]) return -1;
	if (vis[y][x]) return dp[y][x];

	vis[y][x] = inUse[y][x] = true;
	int cnt = (grid[y][x] == 'A'), cur = 0;
	for (int i = 0; i < 4; i++) {
		int nxtx = x + dir[i][0];
		int nxty = y + dir[i][1];

		if (isPoint(nxtx, nxty)) {
			int tmp = dfs(nxtx, nxty, (prv+1)%4);
			if (tmp==-1) return -1;

			cur = max(cur, tmp);
		}
	}

	inUse[y][x] = false;
	return dp[y][x] = cnt + cur;
}

int main ()
{
	if (fopen("data.in", "r"))
		Rd("data.in");

	cin >> N >> M;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++)
			cin >> grid[i][j];

	int result = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			if (grid[i][j]=='D' && !vis[i][j]) {
				int tmp = dfs(j, i,-1);
				result = max(result, tmp);

				if (tmp==-1) {
					cout << "Poor Inna!" << endl;
					return 0;
				}
			}
		}
	}

	if (!result) cout << "Poor Dima!" << endl;
	else cout << result << endl;

	return 0;
}


/*
ID:
PROG:
LANG: C++
 */
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <climits>
#include <cstdlib>
using namespace std;

const double Pi=acos(-1.0);

typedef long long ll;
typedef pair<int, int> pii;

#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)

#define INF (1<<29)+123
#define LL_INF (1<<59)+123

#define MAX_N 1001

int N, M;
string txt = "DIMA";
int dp[MAX_N][MAX_N];
char grid[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];

bool isPoint(int x, int y) {
	return x>=0 && x<M && y>=0 && y<N;
}

int dfs(int x, int y, int prv) {
	static int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};

	if (txt[(prv+1)%4] != grid[y][x]) return 0;
	if (vis[y][x]) {
		if (grid[y][x] == 'D') return -1;
		return dp[y][x];
	}

	vis[y][x] = true;
	int cnt = (grid[y][x] == 'A'), cur = 0;
	for (int i = 0; i < 4; i++) {
		int nxtx = x + dir[i][0];
		int nxty = y + dir[i][1];

		if (isPoint(nxtx, nxty)) {
			int tmp = dfs(nxtx, nxty, (prv+1)%4);
			if (tmp==-1) return -1;

			cur = max(cur, tmp);
		}
	}

	dp[y][x] = cnt+cur;
	return cnt + cur;
}

int main ()
{
	if (fopen("data.in", "r"))
		Rd("data.in");

	cin >> N >> M;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++)
			cin >> grid[i][j];

	int result = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			if (grid[i][j]=='D' && !vis[i][j]) {
				int tmp = dfs(i,j,-1);
				result = max(result, tmp);

				if (tmp==-1) {
					result = -1;
					break;
				}
			}
		}
	}

	if (!result) cout << "Poor Dima!" << endl;
	else if (result==-1) cout << "Poor Inna!" << endl;
	else cout << result << endl;

	return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int a[1005][1005],flag=0;
char A[1005][1005];
int n,m,ans,maxx;
int dfs(int x,int y)
{
    char next;
    if(A[x][y]=='D')
        next='I';
    else if(A[x][y]=='I')
        next='M';
    else if(A[x][y]=='M')
        next='A';
    else
        next='D';
    if(a[x][y+1]==1&&A[x][y+1]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x][y-1]==1&&A[x][y-1]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x+1][y]==1&&A[x+1][y]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x-1][y]==1&&A[x-1][y]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x][y+1]==0&&A[x][y+1]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x,y+1);
        a[x][y]=0;
        ans--;
    }
    if(a[x][y-1]==0&&A[x][y-1]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x,y-1);
        a[x][y]=0;
        ans--;
    }
    if(a[x+1][y]==0&&A[x+1][y]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x+1,y);
        a[x][y]=0;
        ans--;
    }
    if(a[x-1][y]==0&&A[x-1][y]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x-1,y);
        a[x][y]=0;
        ans--;
    }
    return 0;
}
int main()
{
    while(cin>>n>>m)
    {
        maxx=ans=flag=0;
        memset(a,0,sizeof(a));
        memset(A,0,sizeof(A));
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                cin>>A[i][j];
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                if(A[i][j]=='D')
                {
                    ans=1;
                    dfs(i,j);
                }
            }
        if(flag==1)
            cout<<"Poor Inna!"<<endl;
        else
        {
            if(maxx<4)
                cout<<"Poor Dima!"<<endl;
            else
            {
                int sum;
                sum=maxx/4;
                cout<<sum<<endl;
            }
        }
    }

    return 0;
}


#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
int a[1005][1005],flag=0;
char A[1005][1005];
int n,m,ans,maxx;
int dfs(int x,int y)
{
    char next;
    if(A[x][y]=='D')
        next='I';
    else if(A[x][y]=='I')
        next='M';
    else if(A[x][y]=='M')
        next='A';
    else
        next='D';
    if(a[x][y+1]==1&&A[x][y+1]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x][y-1]==1&&A[x][y-1]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x+1][y]==1&&A[x+1][y]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x-1][y]==1&&A[x-1][y]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x][y+1]==0&&A[x][y+1]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x,y+1);
        a[x][y]=0;
        ans--;
    }
    if(a[x][y-1]==0&&A[x][y-1]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x,y-1);
        a[x][y]=0;
        ans--;
    }
    if(a[x+1][y]==0&&A[x+1][y]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x+1,y);
        a[x][y]=0;
        ans--;
    }
    if(a[x-1][y]==0&&A[x-1][y]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x-1,y);
        a[x][y]=0;
        ans--;
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        maxx=ans=flag=0;
        memset(a,0,sizeof(a));
        memset(A,0,sizeof(A));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
                scanf("%c",&A[i][j]);
                getchar();
        }

        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                if(A[i][j]=='D')
                {
                    ans=1;
                    dfs(i,j);
                }
            }
        if(flag==1)
            printf("Poor Inna!\n");
        else
        {
            if(maxx<4)
               printf("Poor Dima!\n");
            else
            {
                int sum;
                sum=maxx/4;
                printf("%d\n",sum);
            }
        }
    }

    return 0;
}


#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
int a[1005][1005];
char A[1005][1005];
int dp[1005][1005];
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
int n,m,ans,maxx,Maxx=0;
int dfs(int x,int y)
{
    char next;
    int i,j,t,tx,ty,Maxx=0;
    if(A[x][y]=='D')
        next='I';
    else if(A[x][y]=='I')
        next='M';
    else if(A[x][y]=='M')
        next='A';
    else
        next='D';
    if(dp[x][y]!=0)
        return dp[x][y];
    for(i=0; i<4; i++)
    {
        tx=x+dx[i];
        ty=y+dy[i];
        if(tx<1||tx>n||ty<1||ty>m||next!=A[tx][ty]) continue ;
        if(a[tx][ty])
        {
            Maxx=100000000;
        }
        else
        {
            a[tx][ty]=1;
            t=dfs(tx,ty);
            a[tx][ty]=0;
            Maxx=max(t,Maxx);
        }
    }
    dp[x][y]=Maxx+1;
    return dp[x][y];
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        maxx=ans=0;
        memset(A,0,sizeof(A));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
                scanf("%c",&A[i][j]);
            getchar();
        }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                if(A[i][j]=='D'&&dp[i][j]==0)
                {
                    a[i][j]=1;
                    ans=dfs(i,j);
                    maxx=max(maxx,ans/4);
                    a[i][j]=0;
                }
            }
        if(maxx==0)
            printf("Poor Dima!\n");
        else
        {
            if(maxx>n*m/4)
                printf("Poor Inna!\n");
            else
            {
                printf("%d\n",maxx);
            }
        }
    }
    return 0;
}


#include<iostream>
#include<string.h>
using namespace std;
int a[2000][2000],flag=0;
char A[2000][2000];
int n,m,ans,maxx;
int dfs(int x,int y)
{
    char next;
    if(A[x][y]=='D')
        next='I';
    else if(A[x][y]=='I')
        next='M';
    else if(A[x][y]=='M')
        next='A';
    else
        next='D';
    if(a[x][y+1]==1&&A[x][y+1]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x][y-1]==1&&A[x][y-1]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x+1][y]==1&&A[x+1][y]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x-1][y]==1&&A[x-1][y]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x][y+1]==0&&A[x][y+1]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x,y+1);
        a[x][y]=0;
        ans--;
    }
    if(a[x][y-1]==0&&A[x][y-1]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x,y-1);
        a[x][y]=0;
        ans--;
    }
    if(a[x+1][y]==0&&A[x+1][y]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x+1,y);
        a[x][y]=0;
        ans--;
    }
    if(a[x-1][y]==0&&A[x-1][y]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x-1,y);
        a[x][y]=0;
        ans--;
    }
    return 0;
}
int main()
{
    while(cin>>n>>m)
    {
        maxx=ans=flag=0;
        memset(a,0,sizeof(a));
        memset(A,0,sizeof(A));
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                cin>>A[i][j];
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                if(A[i][j]=='D')
                {
                    ans=1;
                    dfs(i,j);
                }
            }
        if(flag==1)
            cout<<"Poor Inna!"<<endl;
        else
        {
            if(maxx<4)
                cout<<"Poor Dima!"<<endl;
            else
            {
                int sum;
                sum=maxx/4;
                cout<<sum<<endl;
            }
        }
    }

    return 0;
}


#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
int a[1005][1005];
char A[1005][1005];
int dp[1005][1005];
int n,m,ans,maxx,Maxx=0;
int dfs(int x,int y)
{
    char next;
    int fuck;
    if(A[x][y]=='D')
        next='I';
    else if(A[x][y]=='I')
        next='M';
    else if(A[x][y]=='M')
        next='A';
    else
        next='D';
    if(dp[x][y]!=0)
        return dp[x][y];
    if(A[x][y+1]==next)
    {
        if(a[x][y+1]==1)
        {
            Maxx=100000000;
        }
        else
        {
            a[x][y+1]=1;
            fuck=dfs(x,y+1);
            a[x][y+1]=0;
            Maxx=max(fuck,Maxx);
        }
    }
    if(A[x][y-1]==next)
    {
        if(a[x][y-1]==1)
        {
            Maxx=100000000;
        }
        else
        {
            a[x][y-1]=1;
            fuck=dfs(x,y-1);
            a[x][y-1]=0;
            Maxx=max(fuck,Maxx);
        }
    }
    if(A[x+1][y]==next)
    {
        if(a[x+1][y]==1)
        {
            Maxx=100000000;
        }
        else
        {
            a[x+1][y]=1;
            fuck=dfs(x+1,y);
            a[x+1][y]=0;
            Maxx=max(fuck,Maxx);
        }
    }
    if(A[x-1][y]==next)
    {
        if(a[x-1][y]==1)
        {
            Maxx=100000000;
        }
        else
        {
            a[x-1][y]=1;
            fuck=dfs(x-1,y);
            a[x-1][y]=0;
            Maxx=max(fuck,Maxx);
        }
    }
    dp[x][y]=Maxx+1;
    return dp[x][y];
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        maxx=ans=0;
        memset(A,0,sizeof(A));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
                scanf("%c",&A[i][j]);
            getchar();
        }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                if(A[i][j]=='D'&&dp[i][j]==0)
                {
                    a[i][j]=1;
                    ans=dfs(i,j);
                    maxx=max(maxx,ans/4);
                    a[i][j]=0;
                }
            }
        if(maxx==0)
            printf("Poor Dima!\n");
        else
        {
            if(maxx>n*m/4)
                printf("Poor Inna!\n");
            else
            {
                printf("%d\n",maxx);
            }
        }
    }
    return 0;
}


#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
int a[1005][1005],flag=0;
char A[1005][1005];
int n,m,ans,maxx;
int dfs(int x,int y)
{
    char next;
    if(A[x][y]=='D')
        next='I';
    else if(A[x][y]=='I')
        next='M';
    else if(A[x][y]=='M')
        next='A';
    else
        next='D';
    if(a[x][y+1]==1&&A[x][y+1]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x][y-1]==1&&A[x][y-1]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x+1][y]==1&&A[x+1][y]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x-1][y]==1&&A[x-1][y]==next)
    {
        flag=1;
        return 0;
    }
    if(a[x][y+1]==0&&A[x][y+1]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x,y+1);
        a[x][y]=0;
        ans--;
    }
    if(a[x][y-1]==0&&A[x][y-1]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x,y-1);
        a[x][y]=0;
        ans--;
    }
    if(a[x+1][y]==0&&A[x+1][y]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x+1,y);
        a[x][y]=0;
        ans--;
    }
    if(a[x-1][y]==0&&A[x-1][y]==next)
    {
        ans++;
        a[x][y]=1;
        maxx=max(ans,maxx);
        dfs(x-1,y);
        a[x][y]=0;
        ans--;
    }
    return 0;
}
int main()
{
    while(scanf("%d%d",&n,&m))
    {
        getchar();
        maxx=ans=flag=0;
        memset(a,0,sizeof(a));
        memset(A,0,sizeof(A));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
                scanf("%c",&A[i][j]);
                getchar();
        }

        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                if(A[i][j]=='D')
                {
                    ans=1;
                    dfs(i,j);
                }
            }
        if(flag==1)
            printf("Poor Inna!\n");
        else
        {
            if(maxx<4)
               printf("Poor Dima!\n");
            else
            {
                int sum;
                sum=maxx/4;
                printf("%d\n",sum);
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#define MAX 1005
#define R register
using namespace std ;
char Map[MAX][MAX]  ;
bool Can[30][30] , vis[MAX][MAX] ;
int n , m , tx , ty , Ans , Tim , f[MAX][MAX] ;
int l[4][2] = { {1,0} , {0,1} , {0,-1} , {-1,0} } ;
void dfs( int x ,int y , int step)
{
    vis[x][y] = 1 ;
    Ans = max ( Ans , step/4 ) ;
    for(int i=0; i<4; i++)
    {
        tx = l[i][0]+x ; ty = l[i][1]+y ;
        if( tx>n || ty>m || tx<1 || ty<1 ) continue ;
        if( !Can[Map[x][y]-'A'][Map[tx][ty]-'A'] ) continue ;
        if( vis[tx][ty] )
        {
            cout << "Poor Inna!" << endl;
            exit(0) ;
        }
        if( step+1 > f[tx][ty] )
          f[tx][ty] = step , dfs( tx , ty , step+1 ) ;
    }
    vis[x][y] = 0 ;
}
int main()
{
//  freopen( "inna.in" , "r" , stdin ) ;
//  freopen( "inna.out" , "w" , stdout ) ;
    cin >> n >> m ;
    for(R int i=1; i<=n; i++)
      for(R int j=1; j<=m; j++)
        cin >> Map[i][j] ;
    Can['D'-'A']['I'-'A']=1 ;
    Can['I'-'A']['M'-'A']=1 ;
    Can['M'-'A']['A'-'A']=1 ;
    Can['A'-'A']['D'-'A']=1 ;
    for(R int i=1; i<=n; i++)
      for(R int j=1; j<=m; j++)
       if( Map[i][j]=='D' )
        {
            memset( vis , 0 , sizeof(vis) ) ;
            dfs(i , j , 1) ;
            Ans = max( Ans , f[i][j]/4 ) ;
        }
    if(!Ans) cout << "Poor Dima!" << endl;
      else cout << Ans << endl;
    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f;
int index[4][2]={0,1,0,-1,-1,0,1,0};
char mp[1005][1005];
int f[1005][1005];
int v[1005][1005];
int n,m;
int qwq;
int dfs(int x,int y)
{
	int dx,dy;
	int ret=0;
	if(f[x][y]!=-1) return f[x][y];
	if(v[x][y]) return inf;
	v[x][y]=1;
	for(int i=0;i<4;i++)
	{
		dx=x+index[i][0];
		dy=y+index[i][1];
		if(x<0||x>n-1||y<0||y>m-1)
			continue ;
		if(mp[x][y]=='D'&&mp[dx][dy]=='I') ret=max(dfs(dx,dy),ret);
		else if(mp[x][y]=='I'&&mp[dx][dy]=='M') ret=max(dfs(dx,dy),ret);
		else if(mp[x][y]=='M'&&mp[dx][dy]=='A') ret=max(dfs(dx,dy),ret);
		else if(mp[x][y]=='A'&&mp[dx][dy]=='D') ret=max(dfs(dx,dy),ret);
	}
	v[x][y]=0;
	if(mp[x][y]=='A')
	{
		f[x][y]=ret+1;
		return ret+1;
	}
	else
		return ret;
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(f,-1,sizeof(f));
		memset(v,0,sizeof(v));
		qwq=0;
		for(int i=0;i<n;i++)
			cin>>mp[i];
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
		{
			if(mp[i][j]=='D')
			{
				qwq=max(dfs(i,j),qwq);
			}
		}
		if(qwq==0) puts("Poor Dima!");
		else if(qwq>=inf) puts("Poor Inna!");
		else cout<<qwq;
	}
   return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize(2)
const int N=1000+10;
using namespace std;

int n,m,ans,ma,haveAns,a[N][N];
int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
bool f[N][N],d[N][N],t[5];

int read()
{
	int a,f=1;
	char c;
	c=getchar();
	while(c<'0'||c>'9')
	{
	  if(c=='-') f=-1;
	  c=getchar();
	}
	a=c-'0';
	c=getchar();
	while(c>='0'&&c<='9')
	{
	  a=a*10+c-'0';
	  c=getchar();
	}
	return a*f;
}

void dfs(int x,int y,int now)
{
	int i,xx,yy,next;
//	cout<<x<<" "<<y<<" "<<now<<endl;
	if(haveAns==-1) return;

	if(a[x][y]==1) d[x][y]=1;
	next=a[x][y]+1;
	if(next==5) next=1;
	ma=max(ma,now);

	for(i=1;i<=4;i++)
	{
	  xx=x+dx[i];
	  yy=y+dy[i];
	  if(xx<1||xx>n||yy<1||yy>m) continue;
	  if(a[xx][yy]==next&&f[xx][yy]){haveAns=-1; return;}
	  if(a[xx][yy]!=next||f[xx][yy]) continue;
	  f[xx][yy]=1;
	  if(next==4) dfs(xx,yy,now+1);
	  else dfs(xx,yy,now);
	  f[xx][yy]=0;
	}
}

int main()
{
	int i,j;
	char c;
//	freopen("inna.in","r",stdin);
//	freopen("inna.out","w",stdout);
	n=read(); m=read();
	for(i=1;i<=n;i++)
	  for(j=1;j<=m;j++)
	  {
		cin>>c;
		switch(c)
		{
		  case 'D':{a[i][j]=1; t[1]=1; break;}
		  case 'I':{a[i][j]=2; t[2]=1; break;}
		  case 'M':{a[i][j]=3; t[3]=1; break;}
		  case 'A':{a[i][j]=4; t[4]=1; break;}
		}
	  }
	if(n*m<4){printf("Poor Dima!\n"); return 0;}
	if(!(t[1]&&t[2]&&t[3]&&t[4])){printf("Poor Dima!\n"); return 0;}
	for(i=1;i<=n;i++)
	  for(j=1;j<=m;j++)
	  {
	  	if(a[i][j]!=1||d[i][j]) continue;
	  	d[i][j]=1;

	  	memset(f,0,sizeof(f));
	    haveAns=0; ma=0;
	    f[i][j]=1;
	    dfs(i,j,0);
	    //cout<<endl;

	  	if(haveAns==-1){printf("Poor Inna!\n"); return 0;}
	  	ans=max(ans,ma);
	  }
	if(ans) printf("%d\n",ans);
	else printf("Poor Dima!\n");
//	fclose(stdin); fclose(stdout);
	return 0;
}


// kartikay26
#include <bits/stdc++.h>
using namespace std;
#define debug(...)
#ifdef LOCAL
#include "prettyprint.hpp"
#endif
typedef vector<int> vi; typedef long long int ll;

const int mod = 1e9+7;
const int N = 1e3+1;

int n, m;

char grid[N][N]; // input grid
int  dans[N][N]; // dfs answers
int  vis[N][N];  // visited status

int vis_token = 1;
// 1 = processing 1
// 2 = done processing 1

// down, up, right, left
int dx[] = {1, -1,  0,  0};
int dy[] = {0,  0,  1, -1};

map<char, char> next_ = {
	{'D', 'I'},
	{'I', 'M'},
	{'M', 'A'},
	{'A', 'D'},
};

bool check(int i, int j) {return 0<=i && i<n and 0<=j && j<m;}

int dfs(int i, int j) { // dfs(i,j): return longest path from i, j
	if (vis[i][j]) {
		if (vis[i][j] % 2 == 1) return dans[i][j]=1e9; // cycle found! [back edge]
		if (vis[i][j] % 2 == 0) return dans[i][j]; // no cycle, but return size of this component
	}
	vis[i][j] = vis_token; // odd number : processing
	int ans = 1;
	for (int d = 0; d < 4; ++d) {
		int ii = i + dx[d];
		int jj = j + dy[d];
		if (check(ii, jj) && grid[ii][jj] == next_[grid[i][j]]) {
			ans = max(ans, dfs(ii, jj) + 1);
		}
	}
	vis[i][j] = vis_token + 1; // even number : finished
	return dans[i][j]=ans;
}

void solve(){
	cin >> n >> m;
	for (int i = 0; i < n; ++i) {
		cin >> grid[i];
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (vis[i][j] == 0 and grid[i][j] == 'D') {
				dfs(i, j);
				ans = max(dans[i][j], ans);
				vis_token += 2;
				// print("--- vis:");
				// for (int i = 0; i < n; ++i) {
				// 	for (int j = 0; j < m; ++j) {
				// 		prints(vis[i][j], "\t");
				// 	} print();
				// }
				// print("--- dans:");
				// for (int i = 0; i < n; ++i) {
				// 	for (int j = 0; j < m; ++j) {
				// 		prints(dans[i][j], "\t");
				// 	} print();
				// }
			}
		}
	}

	ans /= 4;

	if (ans == 0) {
		cout << "Poor Dima!" << endl;
	} else if (ans >= 1e8) {
		cout << "Poor Inna!" << endl;
	} else {
		cout << ans << endl;
	}
}

int32_t main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int dx[4]= {0,0,1,-1};

int dy[4]= {1,-1,0,0};

int to[5]= {0,2,3,4,1};

int n,m;

int vis[1001][1001];

int a[1001][1001];

int mans;

int tnx,tny;

bool xexit;

int ans=0;

void dfs(int x,int y,int maxs)

{

  if(xexit) return;

  if(mans<maxs) mans=maxs;

  for(int i=0; i<4; i++)

  {

    int nx=x+dx[i];

    int ny=y+dy[i];

    if(nx<1||ny<1||nx>n||ny>m) continue;

    if(a[nx][ny]!=to[a[x][y]]) continue;

    if(vis[nx][ny]) continue;

    vis[nx][ny]=1;

    if(nx==tnx&&ny==tny)

    {

      ans=-1;

      xexit=1;

    }

    if(a[nx][ny]==4)

      dfs(nx,ny,maxs+1);

    else

      dfs(nx,ny,maxs);

    vis[nx][ny]=0;

  }

}

int main()

{

  //freopen("inna.in","r",stdin);

  //freopen("inna.out","w",stdout);

  scanf("%d%d",&n,&m);

  for(int i=1; i<=n; i++)

    for(int j=1; j<=m; j++)

    {

      char x;

      cin>>x;

      if(x=='D') a[i][j]=1;

      if(x=='I') a[i][j]=2;

      if(x=='M') a[i][j]=3;

      if(x=='A') a[i][j]=4;

    }

  for(int i=1; i<=n; i++)

    for(int j=1; j<=m; j++)

    {

      if(a[i][j]!=1) continue;

      memset(vis,0,sizeof(vis));

      mans=0;

      xexit=0;

      tnx=i,tny=j;

      dfs(i,j,0);

      if(ans==-1)

      {

        cout<<"Poor Inna!";

        return 0;

      }

      ans=max(mans,ans);

    }

  if(ans!=0)

    cout<<ans;

  else cout<<"Poor Dima!";

}


#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int n,m,ans,f[N][N],dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
char mp[N][N];
bool vis[N][N];
int dfs(int x,int y){
	int res=0;
	if(f[x][y]!=-1)return f[x][y];
	if(vis[x][y])return 1e9;
	vis[x][y]=1;
	for(int k=0;k<4;k++){
		int xx=x+dx[k],yy=y+dy[k];
		if(x<0||x>n-1||y<0||y>m-1)continue;
		if(mp[x][y]=='D'&&mp[xx][yy]=='I')res=max(dfs(xx,yy),res);
		else if(mp[x][y]=='I'&&mp[xx][yy]=='M')res=max(dfs(xx,yy),res);
		else if(mp[x][y]=='M'&&mp[xx][yy]=='A')res=max(dfs(xx,yy),res);
		else if(mp[x][y]=='A'&&mp[xx][yy]=='D')res=max(dfs(xx,yy),res);
	}
	vis[x][y]=0;
	if(mp[x][y]=='A'){
		f[x][y]=res+1;
		return res+1;
	}
	else return res;
}
int main(){
	scanf("%d%d",&n,&m);
	memset(f,-1,sizeof(f));
	for(int i=0;i<n;i++)
		scanf("%s",mp[i]);
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
	    	if(mp[i][j]=='D')
		    	ans=max(dfs(i,j),ans);
	if(ans==0)puts("Poor Dima!");
	else if(ans>=1e9)puts("Poor Inna!");
	else printf("%d",ans);
    return 0;
}


#include<bits/stdc++.h>
#define N 1000005
using namespace std;
bool flag=false;
int ans,dis[N],c,st[N],cnt[N],vis[N],que[N],num,n,m,first[N],nex[4*N],f[1005][1005],v[4*N];
int dt[4][4]={{1,0},{0,1},{-1,0},{0,-1}};
char a[1005][1005];
int S(char ch)
{
	if(ch=='D') return 1;
	if(ch=='I') return 2;
	if(ch=='M') return 3;
	if(ch=='A') return 4;
}
int F(int x,int y)
{
	return (x-1)*m+y;
}
void Add(int s,int e)
{
	nex[++num]=first[s];
	first[s]=num;
	v[num]=e;
}
int pd(int x)
{
	if(x==4) return 1;
	else return x+1;
}
void dfs(int s)
{
    if(dis[s]) return;
    vis[s]=1;
    dis[s]=1;
    for(int i=first[s];i!=-1;i=nex[i])
	{
        int j=v[i];
        if(vis[j])
		{
			flag=true;
			vis[s]=0;
			return;
		}
        dfs(j);
        if(flag)
		{
			vis[s]=0;
			return;
		}
        dis[s]=max(dis[s],dis[j]+1);
    }
    vis[s]=0;
}
int main()
{
	memset(first,-1,sizeof(first));
	cin>>n>>m;
	getchar();
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		for(int j=0;j<=strlen(a[i])-1;j++)
		{
			f[i][j+1]=S(a[i][j]);
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			for(int k=0;k<=3;k++)
			{
                int dx=i+dt[k][0];
                int dy=j+dt[k][1];
                if(dx<1||dx>n||dy<1||dy>m) continue;
                if(pd(f[i][j])==f[dx][dy]) Add(F(i,j),F(dx,dy));
            }
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(f[i][j]!=1) continue;
			dfs(F(i,j));
		    if(flag==true)
		    {
			   cout<<"Poor Inna!";
			   return 0;
		    }
		    if(dis[F(i,j)]>ans) ans=dis[F(i,j)];
		}
	}
	ans=ans/4;
    if(ans!=0) printf("%d",ans);
    else printf("Poor Dima!");
	return 0;
}


#include <bits/stdc++.h>
#define oo 1e9
using namespace std;

const int N = 1e3 + 5;
int dp[N][N], n, m, ans;
int zl[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
char a[N][N], s[N];
bool vis[N][N], tag;

bool check(char a, char b) {

	if(a == 'D') return b == 'I';
	if(a == 'I') return b == 'M';
	if(a == 'M') return b == 'A';
	if(a == 'A') return b == 'D';
}

void dfs(int x, int y) {

	if(tag) return ;
	int ans = 0;
	if(dp[x][y] != -1) return ;
	for(int i = 0; i < 4; i ++) {
		int xx = x + zl[i][0], yy = y + zl[i][1];
		if(xx < 1 || xx > n || yy < 1 || yy > m || ! check(a[x][y], a[xx][yy])) continue;
		if(vis[xx][yy]) tag = true, ans = oo;
		else {
			vis[xx][yy] = true;
			dfs(xx, yy);
			vis[xx][yy] = false;
			ans = max(ans, dp[xx][yy]);
		}
	}
	dp[x][y] = ans + 1;
	return ;
}

void Init( ) {

	memset(dp, -1, sizeof(dp)); tag = false;
	for(int i = 1; i <= n; i ++)
		for(int j = 1; j <= m; j ++) {
			if(dp[i][j] == -1 && a[i][j] == 'D') {
				vis[i][j] = true; dfs(i, j);
				vis[i][j] = false;
			}
			ans = max(ans, dp[i][j] / 4);
		}
	if(tag) {
		printf("Poor Inna!\n"); exit(0);
	}
	else {
		int cnt = ans / 4;
		if(! cnt) printf("Poor Dima!\n");
		else printf("%d\n", ans);
	}
}

int main( ) {

	scanf("%d%d", & n, & m);
	for(int i = 1; i <= n; i ++) {
		scanf("%s", s + 1);
		for(int j = 1; j <= m; j ++) a[i][j] = s[j];
	}
	Init( );
}


#include<bits/stdc++.h>
const int N=1000+10;
using namespace std;

int n,m,ans,ma,haveAns,a[N][N];
int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
bool f[N][N],d[N][N],t[5];

int read()
{
	int a,f=1;
	char c;
	c=getchar();
	while(c<'0'||c>'9')
	{
	  if(c=='-') f=-1;
	  c=getchar();
	}
	a=c-'0';
	c=getchar();
	while(c>='0'&&c<='9')
	{
	  a=a*10+c-'0';
	  c=getchar();
	}
	return a*f;
}

void dfs(int x,int y,int now)
{
	int i,xx,yy,next;
//	cout<<x<<" "<<y<<" "<<now<<endl;
	if(haveAns==-1) return;

	if(a[x][y]==1) d[x][y]=1;
	next=a[x][y]+1;
	if(next==5) next=1;
	ma=max(ma,now);

	for(i=1;i<=4;i++)
	{
	  xx=x+dx[i];
	  yy=y+dy[i];
	  if(xx<1||xx>n||yy<1||yy>m) continue;
	  if(a[xx][yy]==next&&f[xx][yy]){haveAns=-1; return;}
	  if(a[xx][yy]!=next||f[xx][yy]) continue;
	  f[xx][yy]=1;
	  if(next==4) dfs(xx,yy,now+1);
	  else dfs(xx,yy,now);
	  f[xx][yy]=0;
	}
}

int main()
{
	int i,j;
	char c;
//	freopen("inna.in","r",stdin);
//	freopen("inna.out","w",stdout);
	n=read(); m=read();
	for(i=1;i<=n;i++)
	  for(j=1;j<=m;j++)
	  {
		cin>>c;
		switch(c)
		{
		  case 'D':{a[i][j]=1; t[1]=1; break;}
		  case 'I':{a[i][j]=2; t[2]=1; break;}
		  case 'M':{a[i][j]=3; t[3]=1; break;}
		  case 'A':{a[i][j]=4; t[4]=1; break;}
		}
	  }
	if(n*m<4){printf("Poor Dima!\n"); return 0;}
	if(!(t[1]&&t[2]&&t[3]&&t[4])){printf("Poor Dima!\n"); return 0;}
	for(i=1;i<=n;i++)
	  for(j=1;j<=m;j++)
	  {
	  	if(a[i][j]!=1||d[i][j]) continue;
	  	d[i][j]=1;

	  	memset(f,0,sizeof(f));
	    haveAns=0; ma=0;
	    f[i][j]=1;
	    dfs(i,j,0);
	    //cout<<endl;

	  	if(haveAns==-1){printf("Poor Inna!\n"); return 0;}
	  	ans=max(ans,ma);
	  }
	if(ans) printf("%d\n",ans);
	else printf("Poor Dima!\n");
//	fclose(stdin); fclose(stdout);
	return 0;
}


#include<bits/stdc++.h>
#define N 1000005
using namespace std;
bool flag=false;
int ans,dis[N],c,st[N],cnt[N],vis[N],que[N],num,n,m,first[N],nex[N],f[1005][1005],v[N];
int dt[4][4]={{1,0},{0,1},{-1,0},{0,-1}};
char a[1005][1005];
int S(char ch)
{
	if(ch=='D') return 1;
	if(ch=='I') return 2;
	if(ch=='M') return 3;
	if(ch=='A') return 4;
}
int F(int x,int y)
{
	return (x-1)*m+y;
}
void Add(int s,int e)
{
	nex[++num]=first[s];
	first[s]=num;
	v[num]=e;
}
int pd(int x)
{
	if(x==4) return 1;
	else return x+1;
}
void dfs(int s)
{
    if(dis[s]) return;
    vis[s]=1;
    dis[s]=1;
    for(int i=first[s];i!=-1;i=nex[i])
	{
        int j=v[i];
        if(vis[j])
		{
			flag=true;
			vis[s]=0;
			return;
		}
        dfs(j);
        if(flag)
		{
			vis[s]=0;
			return;
		}
        dis[s]=max(dis[s],dis[j]+1);
    }
    vis[s]=0;
}
int main()
{
	memset(first,-1,sizeof(first));
	cin>>n>>m;
	getchar();
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		for(int j=0;j<=strlen(a[i])-1;j++)
		{
			f[i][j+1]=S(a[i][j]);
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			for(int k=0;k<=3;k++)
			{
                int dx=i+dt[k][0];
                int dy=j+dt[k][1];
                if(dx<1||dx>n||dy<1||dy>m) continue;
                if(pd(f[i][j])==f[dx][dy]) Add(F(i,j),F(dx,dy));
            }
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(f[i][j]!=1) continue;
			dfs(F(i,j));
		    if(flag==true)
		    {
			   cout<<"Poor Inna!";
			   return 0;
		    }
		    if(dis[F(i,j)]>ans) ans=dis[F(i,j)];
		}
	}
	ans=ans/4;
    if(ans!=0) printf("%d",ans);
    else printf("Poor Dima!");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
string s[4];
int main()
{
	int n,m;
	cin>>n>>m;
	s[0]="D";s[1]="I";s[2]="M";s[3]="A";
	if (n*m<4 || (n==2 && m==2)){
		cout<<"Poor Dima!"<<endl;
		return 0;
	}
	if ((n==1 && m==4) ||(m==1 && n==4)){
		string sss;
		for (int i=0;i<4;i++){
			cin>>sss;
			if (sss!=s[i]){
				cout<<"Poor Dima!"<<endl;
				exit(0);
			}
		}
		cout<<"1"<<endl;
	}
	if (n==1 || m==1){
		string aaaa;
		int sa=0;
		int ans=0;
		int aaaaaa=max(n,m);
		for (int i=0;i<aaaaaa;i++){
			cin>>aaaa;
			if (aaaa!=s[sa]) sa=0;
			if (sa==4) ans++;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int dx[4]= {0,0,1,-1};
int dy[4]= {1,-1,0,0};
int to[5]= {0,2,3,4,1};
int n,m;
int vis[1001][1001];
int a[1001][1001];
int mans;
int tnx,tny;
bool xexit;
int ans=0;
char tn[1001];
void dfs(int x,int y,int maxs)
{
  if(xexit) return;
  if(mans<maxs) mans=maxs;
  for(int i=0; i<4; i++)
  {
    int nx=x+dx[i];
    int ny=y+dy[i];
    if(nx<1||ny<1||nx>n||ny>m) continue;
    if(a[nx][ny]!=to[a[x][y]]) continue;
    if(vis[nx][ny]) continue;
    vis[nx][ny]=1;
    if(nx==tnx&&ny==tny)
    {
      ans=-1;
      xexit=1;
    }
    if(a[nx][ny]==4)
      dfs(nx,ny,maxs+1);
    else
      dfs(nx,ny,maxs);
    vis[nx][ny]=0;
  }
}
int main()
{
  //freopen("inna.in","r",stdin);
  //freopen("inna.out","w",stdout);
  scanf("%d%d",&n,&m);
  for(register int i=1; i<=n; i++)
  {
    scanf("%s",tn);
    for(register int j=0; j<m; j++)
    {
      if(tn[j]=='D') a[i][j+1]=1;
      if(tn[j]=='I') a[i][j+1]=2;
      if(tn[j]=='M') a[i][j+1]=3;
      if(tn[j]=='A') a[i][j+1]=4;
    }
  }
  for(register int i=1; i<=n; i++)
    for(register int j=1; j<=m; j++)
    {
      if(a[i][j]!=1) continue;
      memset(vis,0,sizeof(vis));
      mans=0;
      xexit=0;
      tnx=i,tny=j;
      dfs(i,j,0);
      if(ans==-1)
      {
        cout<<"Poor Inna!";
        return 0;
      }
      ans=max(mans,ans);
    }
  if(ans!=0)
    cout<<ans;
  else cout<<"Poor Dima!";
}


#include <bits/stdc++.h>
using namespace std;

int main(){
	//滑稽保命 (￣▽￣)~*
	int n,m;
	char a[1001][1001];
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>a[i][j];
		}
	}
	if (n*m<4){
		cout<<"Poor Dima!"<<endl;
	}else{
		bool f1=false,f2=false,f3=false,f4=false;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if (a[i][j]=='D')f1=true;
				if (a[i][j]=='I')f2=true;
				if (a[i][j]=='M')f3=true;
				if (a[i][j]=='A')f4=true;
			}
		}
		if(f1&&f2&&f3&&f4){
			cout<<"Poor Inna!"<<endl;
		}else{
			//cout<<f1<<f2<<f3<<f4<<endl;
			cout<<"Poor Dima!"<<endl;
		}
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
using namespace std;
const int maxn = 1005;
char s[maxn][maxn];
int f[maxn][maxn];
int n, m;
bool inq[maxn][maxn];
int onedfs[maxn][maxn];
char dima[5] = "DIMA";
inline int id(char c) {
    if (c == 'D') return 1;
    if (c == 'I') return 2;
    if (c == 'M') return 3;
    if (c == 'A') return 4;
}
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };
bool huan = false;
void dfs(int x, int y, int dep) {
    if (huan) return;
    inq[x][y] = true;
    f[x][y] = dep;
    onedfs[x][y] = 1;
    for (int i = 0;i < 4;i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx > 0 && nx <= n && ny > 0 && ny <= m) {
            if (id(s[nx][ny]) - id(s[x][y]) == 1 || id(s[nx][ny]) - id(s[x][y]) == -3) {
                if (onedfs[nx][ny] == true) {
                    huan = true;
                    return;
                }
                if (!inq[nx][ny]) {
                    dfs(nx, ny, dep + 1);
                    f[x][y] = max(f[x][y], f[nx][ny]);
                    onedfs[nx][ny] = 0;
                }
                else {
                    f[x][y] = max(f[x][y], f[nx][ny] + 1);
                }
            }
        }
    }
}
int main() {

    cin >> n >> m;
    for (int i = 1;i <= n;i++) {
        s[i][0] = ' ';
        scanf("%s", s[i] + 1);
    }
    // for (int i = 1;i <= n;i++) {
    //     for (int j = 1;j <= m;j++) {
    //         printf("%c", s[i][j]);
    //     }
    //     printf("\n");
    // }
    for (int i = 1;i <= n;i++) {
        for (int j = 1;j <= m;j++) {
            if (s[i][j] == 'D' && inq[i][j] == false) {
                dfs(i, j, 1);
                if (huan) {
                    cout << "Poor Inna!" << endl;
                    return 0;
                }
            }
        }
    }
    int ans = 0;
    for (int i = 1;i <= n;i++) {
        for (int j = 1;j <= m;j++) {
            ans = max(f[i][j] / 4, ans);
        }
    }
    if (ans == 0)
        cout << "Poor Dima!" << endl;
    else cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
map<char, char> dic;
int mv[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
int n, m, ans;
char a[1100][1100];
bool vis[1100][1100], flag;
void dfs(int x, int y, char now, int step) {
	if (flag) return ;
	ans = max(ans, step);
	for (int i = 0; i < 4; i++) {
		int sx = x + mv[i][0];
		int sy = y + mv[i][1];
		char next = a[sx][sy];
		if (sx >= 1 && sx <= n && sy >= 1 && sy <= m && next == dic[now]) {
			if (!vis[sx][sy]) {
				flag = 1;
				return ;
			}
			vis[sx][sy] = false;
			if (next == 'A') dfs(sx, sy, next, step + 1);
			else dfs(sx, sy, next, step);
			vis[sx][sy] = true;
		}
	}
}
int main() {
	dic['D'] = 'I', dic['I'] = 'M', dic['M'] = 'A', dic['A'] = 'D';
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> a[i][j];
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) {
			if (a[i][j] == 'D') {
				memset(vis, true, sizeof (vis));
				vis[i][j] = false;
				dfs(i, j, 'D', 0);
			}
		}
	if (flag) cout << "Poor Inna!\n";
	else if (ans == 0) cout << "Poor Dima!\n";
	else cout << ans << "\n";
	return 0;
}


#pragma optimize GCC ("O2")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int _max(int a, int b) {return a > b ? a : b;}
const int N = 1000 + 23;
const int zl[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
char mp[N][N];
int n, m, ans = 0;
bool vis[N][N], flag_oo = 0;

void dfs(int x, int y, int fromx, int fromy, int step) {
	if(vis[x][y]) {
		flag_oo = 1;
		return;
	}
	vis[x][y] = 1;
	for(int i = 0; i < 4; i++) {
		int xx = x + zl[i][0], yy = y + zl[i][1];
		if(xx > n || xx < 1 || yy > m || yy < 1) continue;
		else if(xx == fromx && yy == fromy) continue;
		else if(mp[x][y] == 'D' && mp[xx][yy] == 'I') dfs(xx, yy, x, y, step + 1);
		else if(mp[x][y] == 'I' && mp[xx][yy] == 'M') dfs(xx, yy, x, y, step + 1);
		else if(mp[x][y] == 'M' && mp[xx][yy] == 'A') dfs(xx, yy, x, y, step + 1);
		else if(mp[x][y] == 'A' && mp[xx][yy] == 'D') dfs(xx, yy, x, y, step + 1);
		if(flag_oo) return;
	}
	vis[x][y] = 0;
	ans = _max(ans, step / 4);
	return;
}

int main() {
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++)
		scanf("%s", mp[i] + 1);
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			memset(vis, 0, sizeof(vis));
			if(mp[i][j] == 'D') dfs(i, j, 0, 0, 1);
			if(flag_oo) break;
		}
		if(flag_oo) break;
	}
	if(flag_oo) {
		printf("Poor Inna!");
		return 0;
	}
	if(ans == 0) {
		printf("Poor Dima!");
		return 0;
	}
	printf("%d", ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<set>
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
using namespace std;

int n,m,ans,ans1,cnt;
int a[1001][1001];
int visit[1001][1001];
const int dx[5]={0,1,0,-1};
const int dy[5]={1,0,-1,0};
vector<pair<int,int> > start;

void dfs(int x,int y,int op,int cnt)
{
	ans1=max(ans1,cnt);
	if(visit[x][y])
	{
		cout<<"Poor Inna!"<<endl;
		exit(0);
	}
	visit[x][y]=1;
	REP(i,4)
	{
		int fx=x+dx[i],fy=y+dy[i];
		if(a[fx][fy]==op && fx>=0 && fx<n && fy>=0 && fy<m)
		{
			if(a[fx][fy]==3)
				dfs(fx,fy,(op+1)%4,cnt+1);
			else
				dfs(fx,fy,(op+1)%4,cnt);
		}
		visit[fx][fy]=0;
	}
}

int main()
{
	cin>>n>>m;
	REP(i,n)
		REP(j,m)
		{
			char c;
			cin>>c;
			if(c=='D')
			{
				a[i][j]=0;
				start.push_back(make_pair(i,j));
			}
			else if(c=='I')a[i][j]=1;
			else if(c=='M')a[i][j]=2;
			else a[i][j]=3;
		}
	REP(i,start.size())
	{
		int x=start[i].first;int y=start[i].second;
		dfs(x,y,1,0);
		if(ans1!=0)
			ans=max(ans,ans1);
		ans1=0;
		memset(visit,0,sizeof(visit));
	}
	if(ans==0)
		cout<<"Poor Dima!"<<endl;
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,s,ans,a[1010][1010];
bool b[1010][1010],v[1010][1010],flag;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
char ch;
void dfs(int x,int y,int s){
	for(int i=0;i<4;i++)
	{
		int nx=x+dx[i];
		int ny=y+dy[i];
		if(nx<1||nx>n||ny<1||ny>m) continue;
		if(a[nx][ny]==1&&a[x][y]==4)dfs(nx,ny,s+1);
		if(a[nx][ny]!=a[x][y]+1) continue;
		if(v[nx][ny]){
			cout<<"Poor Inna!";
			flag=1;
			return;
		}
		v[nx][ny]=1;
		if(flag)return;
		dfs(nx,ny,s+1);
		if(flag)return;
		v[nx][ny]=0;
	}
	ans=max(ans,s);
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
	{
		cin>>ch;
		if(ch=='D') a[i][j]=1,b[i][j]=1;
		if(ch=='I') a[i][j]=2;
		if(ch=='M') a[i][j]=3;
		if(ch=='A') a[i][j]=4;
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
	{
		if(b[i][j])
		{
			memset(v,0,sizeof(v));
			dfs(i,j,1);
			s=0;
			if(flag)break;
		}
	}
	if(flag)return 0;
	if(ans==0)cout<<"Poor Dima!";
	cout<<ans/4;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF 1e9
#define EPS 1e-9
#define SQ(c) ((c) * (c))
#define SZ(c) int(c.size())
#define ALL(c) c.begin(), c.end()
#define SET(c, v) memset(c, v, sizeof (c))

const int N = 1e3 + 9;

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };

bool vis[N][N];
int n, m, dp[N][N];
char s[N][N], t[] = "DIMA";

bool valid(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < m;
}

int dfs(int x, int y, int k) {
	int &ret = dp[x][y];
	if (ret) return ret;
	vis[x][y] = 1;
	for (int d = 0; d < 4; ++d) {
		int nx = x + dx[d], ny = y + dy[d];
		if (valid(nx, ny) && s[nx][ny] == t[k]) {
			if (vis[nx][ny]) {
				if (k == 0) return INF;
				continue;
			}
			ret = max(ret, dfs(nx, ny, (k + 1) % 4) + (k == 3));
		}
	}
	vis[x][y] = 0;
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("inp.txt", "r", stdin);
#endif
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (s[i][j] == 'D') {
				ans = max(ans, dfs(i, j, 1));
			}
		}
	}
	if (ans == 0) puts("Poor Dima!");
	else if (ans >= INF) puts("Poor Inna!");
	else printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF 1e9
#define EPS 1e-9
#define SQ(c) ((c) * (c))
#define SZ(c) int(c.size())
#define ALL(c) c.begin(), c.end()
#define SET(c, v) memset(c, v, sizeof (c))

const int N = 1e3 + 9;

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };

bool vis[N][N];
int n, m, dp[N][N];
char s[N][N], t[] = "DIMA";

bool valid(int x, int y) {
	return x >= 0 && x < n && y >= 0 && y < m;
}

int dfs(int x, int y, int k) {
	int &ret = dp[x][y];
	if (ret) return ret;
	vis[x][y] = 1;
	for (int d = 0; d < 4; ++d) {
		int nx = x + dx[d], ny = y + dy[d];
		if (valid(nx, ny) && s[nx][ny] == t[k]) {
			if (vis[nx][ny]) return INF;
			ret = max(ret, dfs(nx, ny, (k + 1) % 4) + (k == 3));
		}
	}
	vis[x][y] = 0;
	return ret;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("inp.txt", "r", stdin);
#endif
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (s[i][j] == 'D') {
				ans = max(ans, dfs(i, j, 1));
			}
		}
	}
	if (ans == 0) puts("Poor Dima!");
	else if (ans >= INF) puts("Poor Inna!");
	else printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF 1e9
#define EPS 1e-9
#define SQ(c) ((c) * (c))
#define SZ(c) int(c.size())
#define ALL(c) c.begin(), c.end()
#define SET(c, v) memset(c, v, sizeof (c))

const int N = 1e3 + 9;

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };

int n, m;
char s[N][N];
bool vis[N][N];
string t = "DIMA";

bool valid(int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < m;
}

int dfs(int x, int y, int k, int lx, int ly) {
    int ret = 0;
    for (int d = 0; d < 4; ++d) {
        int nx = x + dx[d], ny = y + dy[d];
        if (valid(nx, ny) && s[nx][ny] == t[k]) {
            if (vis[nx][ny]) return INF;
            else {
                if (k == 3) vis[lx][ly] = 1;
                char c = s[nx][ny];
                s[nx][ny] = '#';
                if (k == 0) ret = max(ret, dfs(nx, ny, (k + 1) % 4, nx, ny));
                else ret = max(ret, dfs(nx, ny, (k + 1) % 4, lx, ly) + (k == 3));
                s[nx][ny] = c;
            }
        }
    }
    return ret;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("inp.txt", "r", stdin);
#endif
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i)
        scanf("%s", s[i]);
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (!vis[i][j] && s[i][j] == 'D') {
                ans = max(ans, dfs(i, j, 1, i, j));
            }
        }
    }
    if (ans == 0) puts("Poor Dima!");
    else if (ans >= INF) puts("Poor Inna!");
    else printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF 1e9
#define EPS 1e-9
#define SQ(c) ((c) * (c))
#define SZ(c) int(c.size())
#define ALL(c) c.begin(), c.end()
#define SET(c, v) memset(c, v, sizeof (c))

const int N = 1e3 + 9;

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };

int n, m;
char s[N][N];
bool vis[N][N];
string t = "DIMA";

bool valid(int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < m;
}

int dfs(int x, int y, int k, int lx, int ly) {
    int ret = 0;
    for (int d = 0; d < 4; ++d) {
        int nx = x + dx[d], ny = y + dy[d];
        if (valid(nx, ny) && s[nx][ny] == t[k]) {
            if (vis[nx][ny]) return INF;
            else {
                if (k == 3) vis[lx][ly] = 1;
                if (k == 0) ret = max(ret, dfs(nx, ny, (k + 1) % 4, nx, ny) + (k == 3));
                else ret = max(ret, dfs(nx, ny, (k + 1) % 4, lx, ly) + (k == 3));
            }
        }
    }
    return ret;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("inp.txt", "r", stdin);
#endif
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i)
        scanf("%s", s[i]);
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (!vis[i][j] && s[i][j] == 'D') {
                ans = max(ans, dfs(i, j, 1, i, j));
            }
        }
    }
    if (ans == 0) puts("Poor Dima!");
    else if (ans >= INF) puts("Poor Inna!");
    else printf("%d\n", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll                          long long
#define in(a)                       ll a; cin>>a;
#define in2(a,b)                    ll a,b; cin>>a>>b;
#define in3(a,b,c)                  ll a,b,c; cin>>a>>b>>c;
#define in4(a,b,c,d)                ll a,b,c,d; cin>>a>>b>>c>>d;
#define in5(a,b,c,d,e)              ll a,b,c,d,e; cin>>a>>b>>c>>d>>e;
#define lelo(b)                     for(auto &i:b)cin>>i;
#define ff                          first
#define ss                          second
#define call(v)                     v.begin(),v.end()
#define rall(v)                     v.rbegin(),v.rend()
#define show(x)                     for(auto t:x)cout<<t<<" ";
#define pb                          push_back
#define bhar(s,v)                   memset(s,v,sizeof(s))
#define endl                        "\n"
#define ce(x)                       cout<<x<<"\n";
#define nl                          cout<<endl;
#define jaldi                       ios_base::sync_with_stdio(false);cin.tie(NULL);
#define dekh_lo                     ce("dekh_lo");
#define sz(x)                       (ll)x.size()
#define re                          return

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

typedef pair<ll,ll> pii;
typedef vector<pii> vii;
typedef vector<ll> vi;

const ll mod=1e9+7;
const ll N=4e5+5;
const ll INF=1e18;

/*

*/

bool thik(ll x,vi a,vi b,ll k){
    for(ll i=0;i<sz(a);i++){
        ll need=a[i]*x;
        ll aur=need-b[i];
        if(aur>0){
            k-=aur;
            if(k<0){
                re 0;
            }
        }
    }
    re 1;
}

void solve(){
    in2(n,k);
    vi a(n),b(n);
    lelo(a);lelo(b);

    ll l=0,r=1e7,ans;
    while(l<=r){
        ll mid=(l+r)>>1;
        if(thik(mid,a,b,k)){
            ans=mid;
            l=mid+1;
            debug(mid);
        }
        else{
            r=mid-1;
        }
    }

    ce(ans);

}

int32_t main(){
    // jaldi
    ll t;
    // cin>>t;
    t=1;
    while(t--){
        solve();
    }
}

/*

*/


/**
    In the name of Lord Hanuman
**/

#include<bits/stdc++.h>
using namespace std;
#define ll                          long long
#define in(a)                       ll a; cin>>a;
#define in2(a,b)                    ll a,b; cin>>a>>b;
#define in3(a,b,c)                  ll a,b,c; cin>>a>>b>>c;
#define in4(a,b,c,d)                ll a,b,c,d; cin>>a>>b>>c>>d;
#define in5(a,b,c,d,e)              ll a,b,c,d,e; cin>>a>>b>>c>>d>>e;
#define lelo(b)                     for(auto &i:b)cin>>i;
#define ff                          first
#define ss                          second
#define call(v)                     v.begin(),v.end()
#define rall(v)                     v.rbegin(),v.rend()
#define show(x)                     for(auto t:x)cout<<t<<" ";
#define deb(x)                      cout<<#x<<" "<<x<<endl;
#define pb                          push_back
#define fill(s,v)                   memset(s,v,sizeof(s))
#define endl                        "\n"
#define ce(x)                       cout<<x<<"\n";
#define nl                          cout<<endl;
#define jaldi                       ios_base::sync_with_stdio(false);cin.tie(NULL);
#define dekh_lo                     ce("dekh_lo");
#define sz(x)                       x.size()

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

typedef pair<ll,ll> pii;
typedef vector<pii> vii;
typedef vector<ll> vi;

const ll mod=1e9+7;
const ll N=4e5+5;
const ll INF=1e18;

bool thik(ll mid,ll k,vi a,vi b,ll n){
    for(ll i=0;i<n;i++){
        ll need=mid*a[i];
        ll phas_hai=b[i];
        if(phas_hai<need){
            int aur_chahiye=need-phas_hai;
            k-=aur_chahiye;
        }
        else{
            continue;
        }
    }
    return (k>=0);
}

void solve(){
    in2(n,k);

    vi a(n); lelo(a);
    vi b(n); lelo(b);

    if(0){
        ce("So my idea is if we can make k cookies we can also make k-1 cookies");
    }

   ll l=0,r=1e9,ans;
   while(l<=r){
       ll mid=(l+r)>>1;
       if(thik(mid,k,a,b,n)){
           ans=mid;
           l=mid+1;
       }
       else{
           r=mid-1;
       }
   }

   ce(ans);

}

int32_t main(){
    // jaldi
    ll t;
    // cin>>t;
    t=1;
    while(t--){
        solve();
    }
}
/*
 # Stuff you should care about
 * watch the constraints estimate the solution
 * estimate the time complexity of your program
 * check n==0/n==1/vectors_bounds when something is zero/equal
 * ask to yourself whhat exactly the setter wants
 * think deeply your solution and than code it
 * Can't think of it ? maybe there is a simple hidden bs/trick/pattern
 * Do something instead of nothing
*/

/*

*/


/**
    In the name of Lord Hanuman
**/

#include<bits/stdc++.h>
using namespace std;
#define ll                          long long
#define in(a)                       ll a; cin>>a;
#define in2(a,b)                    ll a,b; cin>>a>>b;
#define in3(a,b,c)                  ll a,b,c; cin>>a>>b>>c;
#define in4(a,b,c,d)                ll a,b,c,d; cin>>a>>b>>c>>d;
#define in5(a,b,c,d,e)              ll a,b,c,d,e; cin>>a>>b>>c>>d>>e;
#define lelo(b)                     for(auto &i:b)cin>>i;
#define ff                          first
#define ss                          second
#define call(v)                     v.begin(),v.end()
#define rall(v)                     v.rbegin(),v.rend()
#define show(x)                     for(auto t:x)cout<<t<<" ";
#define deb(x)                      cout<<#x<<" "<<x<<endl;
#define pb                          push_back
#define fill(s,v)                   memset(s,v,sizeof(s))
#define endl                        "\n"
#define ce(x)                       cout<<x<<"\n";
#define nl                          cout<<endl;
#define jaldi                       ios_base::sync_with_stdio(false);cin.tie(NULL);
#define dekh_lo                     ce("dekh_lo");
#define sz(x)                       x.size()

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

typedef pair<ll,ll> pii;
typedef vector<pii> vii;
typedef vector<ll> vi;

const ll mod=1e9+7;
const ll N=4e5+5;
const ll INF=1e18;

bool thik(ll mid,ll k,vi a,vi b,ll n){
    for(ll i=0;i<n;i++){
        ll need=mid*a[i];
        ll phas_hai=b[i];
        if(phas_hai<need){
            ll aur_chahiye=need-phas_hai;
            k-=aur_chahiye;
        }
        else{
            continue;
        }
    }
    return (k>=0);
}

void solve(){
    in2(n,k);

    vi a(n); lelo(a);
    vi b(n); lelo(b);

    if(0){
        ce("So my idea is if we can make k cookies we can also make k-1 cookies");
    }

    // ce(thik(5*100*1000*1000,k,a,b,n));

    // debug(k);
    ll l=0,r=1e9,ans;
    while(l<=r){
        ll mid=(l+r)>>1;
        debug(mid);
        if(thik(mid,k,a,b,n)){
            // cout<<"1"<<endl;
           ans=mid;
           l=mid+1;
        }
        else{
            // cout<<"2"<<endl;
           r=mid-1;
        }
    }

    ce(ans);

}

int32_t main(){
    // jaldi
    ll t;
    // cin>>t;
    t=1;
    while(t--){
        solve();
    }
}
/*
 # Stuff you should care about
 * watch the constraints estimate the solution
 * estimate the time complexity of your program
 * check n==0/n==1/vectors_bounds when something is zero/equal
 * ask to yourself whhat exactly the setter wants
 * think deeply your solution and than code it
 * Can't think of it ? maybe there is a simple hidden bs/trick/pattern
 * Do something instead of nothing
*/

/*

1 5
5
5

*/


#include<bits/stdc++.h>
#include<limits>
#define ll long long
#define F first
#define S second
#define pb push_back
#define oo 1e18
#define endl '\n'
#define si size()
#define all(v) v.begin(),v.end()
#define FASTIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define Matrixll vector< vector < ll > >
#define Matrixpll vector< vector< pair<ll,ll> > >
#define vi vector<int>
#define vll vector<ll>
#define vpll vector<pair<ll,ll> >
#define vd vector<double>
#define vs vector<string>
#define pll pair<ll,ll>

using namespace std;

int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};

const long double Pi=acos(-1),e=2.718,ebs=0.00000000001;
const int N=2e5+10,K=30,mod=1e9+7,SEED = 1331,B=40;
ll n , k;
vll a,b;
bool check(ll x){
    ll sum=0;
    for(int i=0;i<n;i++){
        sum += a[i]*x-b[i];
    }
    return (sum <= k);
}
int main()
{
    FASTIO
    cin >> n >> k;
    a.assign(n,0);
    b.assign(n,0);
    for(auto &x:a)cin >> x;
    for(auto &x:b)cin >> x;
    ll l=0,r=1e15,ans;
    while(l<=r){
        ll mid = l+r >> 1;
        if(check(mid)){
            ans = mid;
            l = mid+1;
        }
        else{
            r = mid-1;
        }
    }
    cout << ans << endl;
    return 0;
}
/**

**/


#include<bits/stdc++.h>
#include<limits>
#define ll long long
#define F first
#define S second
#define pb push_back
#define oo 1e18
#define endl '\n'
#define si size()
#define all(v) v.begin(),v.end()
#define FASTIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define Matrixll vector< vector < ll > >
#define Matrixpll vector< vector< pair<ll,ll> > >
#define vi vector<int>
#define vll vector<ll>
#define vpll vector<pair<ll,ll> >
#define vd vector<double>
#define vs vector<string>
#define pll pair<ll,ll>

using namespace std;

int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};

const long double Pi=acos(-1),e=2.718,ebs=0.00000000001;
const int N=2e5+10,K=30,mod=1e9+7,SEED = 1331,B=40;
ll n , k;
vll a,b;
bool check(ll x){
    ll sum=0;
    for(int i=0;i<n;i++){
        sum += max(0ll,a[i]*x-b[i]);
    }
    return (sum <= k);
}
int main()
{
    FASTIO
    cin >> n >> k;
    a.assign(n,0);
    b.assign(n,0);
    for(auto &x:a)cin >> x;
    for(auto &x:b)cin >> x;
    ll l=0,r=1e15,ans;
    while(l<=r){
        ll mid = l+r >> 1;
        if(check(mid)){
            ans = mid;
            l = mid+1;
        }
        else{
            r = mid-1;
        }
    }
    cout << ans << endl;
    return 0;
}
/**

**/


#include<bits/stdc++.h>
#include<limits>
#define ll long long
#define F first
#define S second
#define pb push_back
#define oo 1e18
#define endl '\n'
#define si size()
#define all(v) v.begin(),v.end()
#define FASTIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define Matrixll vector< vector < ll > >
#define Matrixpll vector< vector< pair<ll,ll> > >
#define vi vector<int>
#define vll vector<ll>
#define vpll vector<pair<ll,ll> >
#define vd vector<double>
#define vs vector<string>
#define pll pair<ll,ll>

using namespace std;

int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};

const long double Pi=acos(-1),e=2.718,ebs=0.00000000001;
const int N=2e5+10,K=30,mod=1e9+7,SEED = 1331,B=40;
ll n , k;
vll a,b;
bool check(ll x){
    ll sum=0;
    for(int i=0;i<n;i++){
        sum += a[i]*x-b[i];
    }
    return (sum <= k);
}
int main()
{
    FASTIO
    cin >> n >> k;
    a.assign(n,0);
    b.assign(n,0);
    for(auto &x:a)cin >> x;
    for(auto &x:b)cin >> x;
    ll l=1,r=1e18,ans;
    while(l<=r){
        ll mid = l+r >> 1;
        if(check(mid)){
            ans = mid;
            l = mid+1;
        }
        else{
            r = mid-1;
        }
    }
    cout << ans << endl;
    return 0;
}
/**

**/


#include<bits/stdc++.h>
#include<limits>
#define ll long long
#define F first
#define S second
#define pb push_back
#define oo 1e18
#define endl '\n'
#define si size()
#define all(v) v.begin(),v.end()
#define FASTIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define Matrixll vector< vector < ll > >
#define Matrixpll vector< vector< pair<ll,ll> > >
#define vi vector<int>
#define vll vector<ll>
#define vpll vector<pair<ll,ll> >
#define vd vector<double>
#define vs vector<string>
#define pll pair<ll,ll>

using namespace std;

int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};

const long double Pi=acos(-1),e=2.718,ebs=0.00000000001;
const int N=2e5+10,K=30,mod=1e9+7,SEED = 1331,B=40;
ll n , k;
vll a,b;
bool check(ll x){
    ll sum=0;
    for(int i=0;i<n;i++){
        sum += a[i]*x-b[i];
    }
    return (sum <= k);
}
int main()
{
    FASTIO
    cin >> n >> k;
    a.assign(n,0);
    b.assign(n,0);
    for(auto &x:a)cin >> x;
    for(auto &x:b)cin >> x;
    ll l=0,r=1e18,ans;
    while(l<=r){
        ll mid = l+r >> 1;
        if(check(mid)){
            ans = mid;
            l = mid+1;
        }
        else{
            r = mid-1;
        }
    }
    cout << ans << endl;
    return 0;
}
/**

**/


#include<iostream>
#include<vector>
#include<stack>
#include <cstdio>
typedef long long ll;
using namespace std;
int main()
{
    int ans=0, n,k,max=0;
    cin>>n>>k;
    int arr[n],ing[n];
    for (int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    for (int i=0;i<n;i++)
    {
        cin>>ing[i];
        if(ing[i]/arr[i]>max)
            max=ing[i]/arr[i];
    }
    while(true)
    {
        for(int i=0;i<n;i++)
        {
            if(arr[i]*max>ing[i])
                ans+=arr[i]*max-ing[i];
        }
        if(ans<=k)
        {
            for(int i=1;i<=1000;i++)
            {
            if(k-ans==i*n)
                max+=i;
            }
            cout<<max;
            return 0;
        }
        else
        {max--; ans=0;}
    }
}


#include<iostream>
#include<vector>
#include<stack>
#include <cstdio>
typedef long long ll;
using namespace std;
int main()
{
    int ans=0, n,k,max=0;
    cin>>n>>k;
    int arr[n],ing[n];
    for (int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    for (int i=0;i<n;i++)
    {
        cin>>ing[i];
        if(ing[i]/arr[i]>max)
            max=ing[i]/arr[i];
    }
    while(true)
    {
        for(int i=0;i<n;i++)
        {
            if(arr[i]*max>ing[i])
                ans+=arr[i]*max-ing[i];
        }
        if(ans<=k)
        {
            if(n==1)
                max+=k-ans;
            cout<<max;
            return 0;
        }
        else
        {max--; ans=0;}
    }
}


#include<iostream>
#include<vector>
#include<stack>
#include <cstdio>
typedef long long ll;
using namespace std;
int main()
{
    int ans=0, n,k,max=0;
    cin>>n>>k;
    int arr[n],ing[n];
    for (int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    for (int i=0;i<n;i++)
    {
        cin>>ing[i];
        if(ing[i]/arr[i]>max)
            max=ing[i]/arr[i];
    }
    while(true)
    {
        for(int i=0;i<n;i++)
        {
            if(arr[i]*max>ing[i])
                ans+=arr[i]*max-ing[i];
        }
        if(ans<=k)
        {
            cout<<max;
            return 0;
        }
        else
        {max--; ans=0;}
    }
}


#include<iostream>
#include<vector>
#include<stack>
#include <cstdio>
typedef long long ll;
using namespace std;
int main()
{
    int ans=0, n,k,max=0;
    cin>>n>>k;
    int arr[n],ing[n];
    for (int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    for (int i=0;i<n;i++)
    {
        cin>>ing[i];
        if(ing[i]/arr[i]>max)
            max=ing[i]/arr[i];
    }
    while(true)
    {
        for(int i=0;i<n;i++)
        {
            if(arr[i]*max>ing[i])
                ans+=arr[i]*max-ing[i];
        }
        if(ans<=k)
        {
            if(n==1)
                max+=(k-ans)/arr[0];
            cout<<max;
            return 0;
        }
        else
        {max--; ans=0;}
    }
}


#include <bits/stdc++.h>
#define endl "\n"
#define base ios_base::sync_with_stdio(false),cin.tie(0);
using namespace std;
typedef long long ll;
const int  mx = 1e5 + 5;
ll n, k;
ll a[mx];
ll b[mx];
bool Hey(ll c)
{
    ll t = k;
    for(int i = 0; i < n; ++i)
    {
        if((c*a[i]) > b[i])
            t -= ((c*a[i]) - b[i]);
        if(t < 0)
            return 0;
    }
    return 1;
}
int main()
{
    /*
    #ifndef ONLINE_JUDGE
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
    #endif*/
    base
    cin >> n >> k;
    for(int i = 0; i  <n; ++i)
        cin >> a[i];
    for(int i = 0; i < n; ++i)
        cin >> b[i];
    ll st = 1, en = 2e9 + 5LL, md;
    while(st < en)
    {
        md  = (st+en+1LL)>>1LL;
        if(Hey(md))
            st = md;
        else
            en = md - 1LL;
    }
    cout << st << endl;
}


#include <bits/stdc++.h>
#define endl "\n"
#define base ios_base::sync_with_stdio(false),cin.tie(0);
using namespace std;
typedef long long ll;
const int  mx = 1e5 + 5;
ll n, k;
ll a[mx];
ll b[mx];
bool Hey(ll c)
{
    ll t = k;
    for(int i = 0; i < n; ++i)
    {
        if((c*a[i]) > b[i])
            t -= (c*a[i]);
        if(t < 0)
            return 0;
    }
    return 1;
}
int main()
{
    /*
    #ifndef ONLINE_JUDGE
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
    #endif*/
    base
    cin >> n >> k;
    for(int i = 0; i  <n; ++i)
        cin >> a[i];
    for(int i = 0; i < n; ++i)
        cin >> b[i];
    ll st = 1, en = 2e9 + 5LL, md;
    while(st < en)
    {
        md  = (st+en+1LL)>>1LL;
        if(Hey(md))
            st = md;
        else
            en = md - 1LL;
    }
    cout << st + 1 << endl;
}


#include <bits/stdc++.h>
#define endl "\n"
#define base ios_base::sync_with_stdio(false),cin.tie(0);
using namespace std;
typedef long long ll;
const int  mx = 1e5 + 5;
ll n, k;
ll a[mx];
ll b[mx];
bool Hey(ll c)
{
    ll t = k;
    for(int i = 0; i < n; ++i)
    {
        if((c*a[i]) > b[i])
            t -= ((c*a[i]) - b[i]);
        if(t < 0)
            return 0;
    }
    return 1;
}
int main()
{
    /*
    #ifndef ONLINE_JUDGE
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
    #endif*/
    base
    cin >> n >> k;
    for(int i = 0; i  <n; ++i)
        cin >> a[i];
    for(int i = 0; i < n; ++i)
        cin >> b[i];
    ll st = 0, en = 2e9 + 5LL, md;
    while(st < en)
    {
        md  = (st+en+1LL)>>1LL;
        if(Hey(md))
            st = md;
        else
            en = md - 1LL;
    }
    cout << st << endl;
}


#include <iostream>
using namespace std;
int main()
{
    int n,k,counter=0,in=0;
        cin>>n>>k;
    bool test=true;
    int g[n];
    int need[n];
    for(int i=0;i<n;i++){cin>>g[i];}
    for(int i=0;i<n;i++){cin>>need[i];}
    for(int i=0;test==true;i++){
        if(need[i]-g[i]>=0){test=true;in++;need[i]-=g[i];}
        else{
            if((need[i]+k)-g[i]>=0){
               in++;
               test =true;
               k=k-(g[i]-need[i]);
               need[i]-=g[i];
            }
            else{test=false;}
        }
        if(i>=n){i=0;}
    }
    for(int i=in;i>=3;i-=3){
        counter++;
    }
  cout<<counter;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
bool flag(int a[],int b[],int n)
{
    for(int i=0;i<n;i++)
    {
        if(a[i]<b[i])
        {
            return false;
        }
    }
    return true;
}
int ext(int a[],int b[],int n)
{
    int e=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]<b[i])
        {
            e++;
        }
    }
    return e;
}
const int N=1005;
int need[N];
int have[N];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        cin>>need[i];
    }
    for(int i=0;i<n;i++)
    {
        cin>>have[i];
    }
    int ans=0;
    while(flag(have,need,n))
    {
        for(int i=0;i<n;i++)
        {
            have[i]-=need[i];
        }
        ans++;
    }
    while(k>0)
    {
        int extra=ext(have,need,n);
        if(extra>k)
        {
            break;
        }
        else
        {
            k-=extra;
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
bool flag(int a[],int b[],int n)
{
    for(int i=0;i<n;i++)
    {
        if(a[i]<b[i])
        {
            return false;
        }
    }
    return true;
}
int ext(int a[],int b[],int n)
{
    int e=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]<b[i])
        {
            e=e+(b[i]-a[i]);
            a[i]=0;
        }
        else
        {
            a[i]-=b[i];
        }
    }
    return e;
}
const int N=1005;
int need[N];
int have[N];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        cin>>need[i];
    }
    for(int i=0;i<n;i++)
    {
        cin>>have[i];
    }
    int ans=0;
    while(flag(have,need,n))
    {
        for(int i=0;i<n;i++)
        {
            have[i]-=need[i];
        }
        ans++;
    }
    while(k>0)
    {
        int extra=ext(have,need,n);
        if(extra>k)
        {
            break;
        }
        else
        {
            k-=extra;
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n,k,counter=0,in=0;
        cin>>n>>k;
    bool test=true;
    int g[n];
    int need[n];
    for(int i=0;i<n;i++){cin>>g[i];}
    for(int i=0;i<n;i++){cin>>need[i];}
    for(int i=0;test==true;i++){
        if(need[i]-g[i]>=0){test=true;in++;need[i]-=g[i];}
        else{
            if((need[i]+k)-g[i]>=0){
               in++;
               test =true;
               k=k-(g[i]-need[i]);
               need[i]-=g[i];
            }
            else{test=false;}
        }
        if(i>=n){i=0;}
    }
    for(int i=in;i>=3;i-=3){
        counter++;
    }

  cout<<counter;
    return 0;
}


#include <iostream>

using namespace std;

const int N=1e3+5;
int a[N],b[N];
int n,k;

int main()
{
    int ans=0;
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(int i=0;i<n;i++)
    {
        cin>>b[i];
    }
    bool flag=true;
    for(int j=1;flag==true;j++) //bake
    {
        for(int i=0;i<n;i++)
        {
            if(b[i]-a[i]>=0)
            {
                b[i]-=a[i];
            }
            else if(b[i]+k>=a[i])
            {
                k-=(a[i]-b[i]);
                b[i]=0;
            }
            else
            {
                flag=false;
            }
           //cout<<b[i]<<" ";
        }
        ans++;
        //cout<<endl<<k<<endl;
    }
    cout<<ans-1<<endl;
    return 0;
}


#include <iostream>
using namespace std;

long long n, k = 1000;
long long a[1000000], b[1000000];

bool lehet(long long x) {
  long long w = k;
  for (int i = 0; i < n; i++) {
    if (a[i] * x > b[i]+w) {
      return false;
    } else if ((a[i] * x > b[i]) && (a[i] * x <= b[i]+w)){
      w -= a[i]*x - b[i];
    }
  }
  return true;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];

  long long b = 0, j = 2000000000 + 1;
  while (j - b > 1) {
    if (lehet((b+j)/2)) {
      b = (b+j)/2;
    } else {
      j = (b+j)/2;
    }
  }

  cout << b << endl;
	return 0;
}


#include <iostream>
using namespace std;

int n, k = 1000;
int a[1000], b[1000];

bool lehet(int x) {
  int w = k;
  for (int i = 0; i < n; i++) {
    if (a[i] * x > b[i]+w) {
      return false;
    } else if ((a[i] * x > b[i]) && (a[i] * x <= b[i]+w)){
      w -= b[i]+w - a[i]*x;
    }
  }
  return true;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];

  int m = b[0]; for (int i = 1; i < n; i++) if (b[i] > m) m = b[i];
  for (int i = m; i >= 0; i--) if (lehet(i)) {cout << i << endl; break;};

	return 0;
}


#include <iostream>
using namespace std;

int n, k = 1000;
int a[1000], b[1000];

bool lehet(int x) {
  int k = k;
  for (int i = 0; i < n; i++) {
    if (a[i] * x > b[i]+k) {
      return false;
    } else if ((a[i] * x > b[i]) && (a[i] * x <= b[i]+k)){
      k -= b[i]+k - a[i]*x;
    }
  }
  return true;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];

  int m = b[0]; for (int i = 1; i < n; i++) if (b[i] > m) m = b[i];
  for (int i = m; i >= 0; i--) if (lehet(i)) {cout << i << endl; break;};

	return 0;
}


#include <iostream>
using namespace std;

int n, k = 1000;
int a[1000], b[1000];

bool lehet(int x) {
  int w = k;
  for (int i = 0; i < n; i++) {
    if (a[i] * x > b[i]+w) {
      return false;
    } else if ((a[i] * x > b[i]) && (a[i] * x <= b[i]+w)){
      w -= b[i]+w - a[i]*x;
    }
  }
  return true;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];

  for (int i = 1000; i >= 0; i--) if (lehet(i)) {cout << i << endl; break;};

	return 0;
}


#include <iostream>
using namespace std;

int n, k = 1000;
int a[1000], b[1000];

bool lehet(int x) {
  int w = k;
  for (int i = 0; i < n; i++) {
    if (a[i] * x > b[i]+w) {
      return false;
    } else if ((a[i] * x > b[i]) && (a[i] * x <= b[i]+w)){
      w -= a[i]*x - b[i];
    }
  }
  return true;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];

  for (int i = 2000; i >= 0; i--) if (lehet(i)) {cout << i << endl; break;};

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,k;
int a[1005],b[1005];
bool CanWeBake(int v)
{
	int x=k;
	for(int i=0;i<n;i++)
		if(a[i]*v>b[i])
			x-=((a[i]*v)-b[i]);
	if(x<0)
		return false;
	return true;
}

int BS()
{
	int l=0,r=2000,mid;
	while(l<r)
	{
		mid=(l+r+1)/2;
		if(CanWeBake(mid)==true)
			l=mid;
		else
			r=mid-1;
	}
	return l;
}

int main() {
	scanf("%d %d",&n,&k);
	for(int i=0;i<n;i++)
		scanf("%d",a+i);
	for(int i=0;i<n;i++)
		scanf("%d",b+i);
	printf("%d\n",BS());
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,k;
int a[1005],b[1005];
bool OK(int v)
{
	int x=k;
	for(int i=0;i<n;i++)
		if(a[i]*v>b[i])	x-=(a[i]*v-b[i]);
	return x>=0?true:false;
}
int BS()
{
	int l=0,r=2005;
	while(l<r)
	{
		int mid=(l+r+1)/2;
		if(OK(mid))
			l=mid;
		else
			r=mid-1;
	}
	return l;
}
int main() {
	cin>>n>>k;
	for(int i=0;i<n;i++)	scanf("%d",a+i);
	for(int i=0;i<n;i++)	scanf("%d",b+i);
	cout<<BS()<<endl;
	return 0;
}


/****** trying to be better *****/
#include<bits/stdc++.h>
#define mem(a,v) memset(a,v,sizeof a)
#define ll long long
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define max3(x,y,z) max(x,max(y,z))
#define min3(x,y,z) min(x,min(y,z))
#define f first
#define s second
#define fr(i,n) for(int j=i;j<n;j++)
#define rev(a) reverse(a.begin(),a.end())
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define lb(a,v) lower_bound(all(a),v)
#define ub(a,v) upper_bound(all(a),v)
#define ins(v) insert(v)
#define pb(v) push_back(v)
#define sz(x) (int)(x).size()
#define MOD 1000000007
#define sf1(a) scanf("%d",&a)
#define sfl1(a) scanf("%lld",&a)
#define sf2(a,b) scanf("%d %d",&a,&b)
#define sf3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define sf4(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d)
#define get_bit(x,b) x&(1<<b)
#define IO ios_base::sync_with_stdio(false);
using namespace std;
const int N=1e3+5,M=1e9+7,oo=0x3f3f3f3f;

int dx []={1,0,-1,0};
int dy []={0,-1,0,1};

int a[N],b[N],k,n;

bool CanWeMake(int v){
    int tmp = k;
    for(int i=0;i<n;i++){
        int totalgrams = v*a[i];
        if(totalgrams > b[i])
            tmp -= (totalgrams - b[i]);
        if(tmp<0)  return false;
    }
    return true;
}
int BS(){
    int l=0,r=2000,mid;
    while(l<r){
        mid = (l+r+1)/2;
        if(CanWeMake(mid))  l=mid;
        else                r=mid-1;
    }
    return l;
}
int main() {
	#ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	#endif
    cin>>n>>k;
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];
	cout<<BS()<<endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <unordered_map>
#include <stack>
#include <memory.h>
#include <list>
#include <numeric>
#include <functional>
#include <complex>
#include <cassert>
#include <clocale>
#include <fstream>
#include <sstream>
#include <iterator>
using namespace std;

int md = 1000000007;
//long long binpow(long long a, long long n) {
//	long long res = 1;
//	while (n) {
//		if (n & 1)
//		{
//			res = (res * a) % md;
//			//res %= md;
//		}
//		a = (a * a) % md;
//		//a %= md;
//		n >>= 1;
//	}
//	return res;// % md;
//}

int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}
int lcm(int a, int b) {
	return a / gcd(a, b) * b;
}
//int gcd(int a, int b, int& x, int& y) {
//	if (a == 0) {
//		x = 0; y = 1;
//		return b;
//	}
//	int x1, y1;
//	int d = gcd(b % a, a, x1, y1);
//	x = y1 - (b / a) * x1;
//	y = x1;
//	return d;
//}
//bool find_any_solution(int a, int b, int c, int& x0, int& y0, int& g) {
//	g = gcd(abs(a), abs(b), x0, y0);
//	if (c % g != 0)
//		return false;
//	x0 *= c / g;
//	y0 *= c / g;
//	if (a < 0)   x0 *= -1;
//	if (b < 0)   y0 *= -1;
//	return true;
//}

const int mod = 998244353;

// int md = 1000000007;
//long long binpow(long long a, long long n)
//{
//	if (n == 0)return 1;
//	if (n % 2 == 0)
//	{
//		long long b = (binpow(a, n / 2)) % mod;
//		return b * b % mod;
//	}
//	else
//		return (binpow(a, n - 1) * a) % mod;
//}
long long binpow(long long a, long long b) {
	long long res = 1;
	while (b > 0)
		if (b & 1) {
			res = (long long)(res * 1ll * a);//% mod;
			--b;
		}
		else {
			a = (long long)(a * a);//% mod;
			b >>= 1;
		}
	return (long long)res;
}

int main()
{
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("sum.in", "r", stdin);
	//freopen("sum.out", "w", stdout);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.precision(10); cout.setf(ios::fixed);
	int n,k;
	cin >> n >> k;
	vector<int>a(n);
	for (int i = 0; i < n; i++)
		cin >> a[i];
	vector<int>b(n);
	for (int i = 0; i < n; i++)
		cin >> b[i];
	int l = 0,r = 3000;

	int m;
	while (r - l > 1)
	{
		m = (l + r) / 2;
		bool flag = true;
		int cnt = k;
		for (int i = 0; i < n; i++)
		{
			if (a[i] * m > b[i])
			{
				if (a[i] * m <= b[i] + cnt)
				{
					cnt -= (a[i] * m - b[i]);
				}
				else
				{
					flag = false;
					break;
				}
			}
		}
		if (!flag)
		{
			r = m;
		}
		else
			l = m;
	}
	vector<int>p;
	p.push_back(l);
	p.push_back(m);
	p.push_back(r);
	vector<int>ans;
	for (int j = 0; j < 3; j++)
	{
		bool fl = true;
		int cnt = k;
		for (int i = 0; i < n; i++)
		{
			if (a[i] * p[j] > b[i])
			{
				if (a[i] * p[j] <= b[i] + cnt)
				{
					cnt -= (a[i] * p[j] - b[i]);
				}
				else
				{
					fl = false;
					break;
				}
			}
		}
		if (fl)
			ans.push_back(p[j]);
	}
	sort(ans.rbegin(), ans.rend());
	cout << ans[0];
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <utility>
#include <map>
#include <set>
#include <tuple>
#include <array>
#include <numeric>
#include <bitset>
#define _USE_MATH_DEFINES
#include <math.h>
#include <queue>
#include <cstring>
#include <stack>

#define pb push_back

//#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

int n, k;
ll a[100005], b[100005];

bool can(ll c)
{
	ll g = k;
	for (int i = 0; i < n && g >= 0; ++i)
	{
		ll req = a[i] * c;
		if (b[i] < req) g -= (req - b[i]);
	}
	return g >= 0;
}

int main()
{
	cin >> n >> k;

	for (int i = 0; i < n; ++i) cin >> a[i];
	for (int i = 0; i < n; ++i) cin >> b[i];

	ll lo = 0;
	ll hi = 2000000000;

	ll best = 0;
	while (lo <= hi)
	{
		ll m = lo + (hi - lo) / 2;
		if (can(m))
		{
			best = m;
			lo = m + 1;
		}
		else
		{
			hi = m - 1;
		}
	}

	cout << best << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
int main() {
	int n, k;
	cin >> n >> k;
	vector <int> a(n), b(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
	}
	int min1 = 1000000;
	for (int i = 0; i < n; i++) {
		min1 = min(min1, b[i] / a[i]);
	}
	int ans = min1;
	for (int i = 0; i < n; i++) {
		b[i]-= min1 * a[i];
	}
	while (k > 0) {
		for (int i = 0; i < n; i++) {
			if (b[i] < a[i]) {
				if (k > 0) { k -= a[i] - b[i]; b[i] = 0; }
				else { ans--; break; }
			}
			else b[i] -= a[i];
		}
		ans++;
	}
	cout << ans;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
int main() {
	int n, k;
	cin >> n >> k;
	vector <int> a(n), b(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
	}
	int min1 = 1000000;
	for (int i = 0; i < n; i++) {
		min1 = min(min1, b[i] / a[i]);
	}
	int ans = min1;
	for (int i = 0; i < n; i++) {
		b[i]-= min1 * a[i];
	}
	while (k > 0) {
		for (int i = 0; i < n; i++) {
			if (b[i] < a[i]) {
				 k -= a[i] - b[i]; b[i] = 0;
			}
			else b[i] -= a[i];
		}
		if (k>=0) ans++;
	}
	cout << ans;
}


#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;

int main()
{
	int n, k;
	vector<int>v1, v2;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		int l;
		cin >> l;
		v1.push_back(l);
	}
	for (int i = 0; i < n; i++)
	{
		int l;
		cin >> l;
		v2.push_back(l);
	}
	int lp=0, up=1000;
	int maxnum = 0;
	while (lp< up)
	{

		int mid = (lp + up) / 2;
		int temp = k;
		 bool yes = 1;
		for (int i = 0; i < v1.size()&&yes; i++)
		{
			if (v1[i] * mid > v2[i])
			{
				int c = v1[i] * mid - v2[i];
				temp -= c;
			}
			if (temp < 0)
				yes = 0;
		}
		if (yes)
		{
			if (maxnum == mid)
				break;
			maxnum = max(maxnum, mid);
			lp = mid;

		}
		else up = mid;
	}
	cout << maxnum << endl;

	return 0;
}


#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;

int main()
{
	int n, k;
	vector<int>v1, v2;
	cin >> n >> k;
	for (int i = 0; i < n; i++)
	{
		int l;
		cin >> l;
		v1.push_back(l);
	}
	for (int i = 0; i < n; i++)
	{
		int l;
		cin >> l;
		v2.push_back(l);
	}
	int lp=0, up=3000;
	int maxnum = 0;
	while (lp< up)
	{

		int mid = (lp + up) / 2;
		int temp = k;
		 bool yes = 1;
		for (int i = 0; i < v1.size()&&yes; i++)
		{
			if (v1[i] * mid > v2[i])
			{
				int c = v1[i] * mid - v2[i];
				temp -= c;
			}
			if (temp < 0)
				yes = 0;
		}
		if (yes)
		{
			if (maxnum == mid)
				break;
			maxnum = max(maxnum, mid);
			lp = mid;

		}
		else up = mid;
	}
	cout << maxnum << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstring>
#include <stack>
using namespace std;

int n,k;
int a[1005];
int b[1005];
struct point {
    int a;
    int b;
    int c;
} p[1005];

int main() {
    cin >> n >> k;
    int maxn = 1000000;
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for(int i = 0; i < n; i++) {
        cin >> b[i];
        p[i].a = b[i]/a[i];
        p[i].b = b[i]%a[i];
        p[i].c = a[i]-p[i].b;
        maxn = min(maxn,p[i].a);
    }
    //cout << 123 << '\n';
    while(true) {
        int i;
        for(i = 0; i < n; i++) {
            if(p[i].a == maxn) {
                if(k < p[i].c) {
                    break;
                } else {
                    k -= p[i].c;
                    p[i].c = a[i];
                    p[i].a = maxn+1;
                }
            }
        }
        if(i == n) {
            maxn++;
        } else {
            break;
        }
    }
    cout << maxn << endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
using ll = long long int;

int n, k;
vector <int> a, b;

bool f(ll m) {
	ll sum = 0;
	for (int i = 0; i < n; ++i)
		if (a[i] * m > b[i]) {
			sum += a[i] * m - b[i];
			if (sum > k) return false;
		}
	return true;
}

int b_search(ll r) {
	ll l = 0, mid;
	while (l <= r) {
		mid = (r + l) / 2;
		if (f(mid) && !f(mid+1)) {
			return mid;
		}
		if (f(mid)) l = mid + 1;
		else r = mid - 1;
	}
	return -1;
}

int main() {
#pragma region
	ios_base::sync_with_stdio(false);
	cin.tie(0);
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int q = 1;
#ifdef _DEBUG
	q = 1;
#endif
#pragma endregion
	cin >> n >> k;
	a.resize(n);
	b.resize(n);
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0; i < n; i++) cin >> b[i];

	int mx = 0;
	for (int i = 0; i < n; i++)
		mx = max(mx, (b[i]+k) / a[i]);

	while (q--) {
		cout << b_search(mx);
	}
}


#include <iostream>
using namespace std;

int main()
{
    int n,k;
    cin>>n>>k;
    int*a=new int[n];
    int*b=new int[n];
    for(int i=0;i<n;i++)cin>>a[i];
    for(int i=0;i<n;i++)cin>>b[i];

    int l=0,r=2000;
    int mid;
    int max=0;
    while(l<=r){
        mid=(l+r)/2;
        int sum=0;
        if(mid<=max){
            l=mid+1;
            continue;
        }
        for(int i=0;i<n;i++)
            if(mid*a[i]>b[i]) sum+=mid*a[i]-b[i];

        if(sum<=k){
            max=mid;
            l=mid+1;
        }else{
            r=mid-1;
        }
    }
    cout<<max;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <string>
#include <queue>
#include <iomanip>
#include <map>
#include <set>

#define loop(vec) for(auto &el:vec)
#define pb push_back
#define For(i,n) for(int i=0;i<n;i++)
#define all(vec) vec.begin(),vec.end()
#define rall(vec) vec.rbegin(),vec.rend()
#define fi first
#define se second
#define pii pair<int,int>
#define int long long
using namespace std;

const int inf = 1e9 + 47;

void solve() {
    int n,k;
    cin >> n >> k;
    vector<int>cnt(n);
    loop(cnt)cin >> el;
    vector<int>vec(n);
    loop(vec)cin >> el;
    int ans = 1e9;
    For(i,n){
        ans = min(vec[i] / cnt[i], ans);
    }
    For(i, n) {
        vec[i] -= ans*cnt[i];
    }
    while (k>0) {
        int sum = 0;
        For(i, n) {
            if (vec[i] > 0) {
                int t = min(0ll, vec[i] - cnt[i]);
                vec[i] -= cnt[i];
                if (t < 0)sum += abs(t);
            }
            else sum += cnt[i];
        }
        ans++;
        k -= sum;
    }
    if (k < 0)ans--;
    cout << ans;
}

signed main() {
    int t;
    //cin >> t;
    t = 1;
    while (t--)solve();
    return 0;
}


#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#pragma warning(disable : 4996)

#define all(v)             v.begin(),v.end()
#define sz(v)            ((int)((v).size()))
#define psh(x)                  push_back(x)
#define sc(x)                 scanf("%d",&x)
#define sc2(x,y)         scanf("%d%d",&x,&y)
#define scl(x)              scanf("%lld",&x)
#define lop(i,n)        for(register int i=0;i<n;i++)
#define loop(i,f,l)     for(register int i=f;i<l;i++)
#define R_(s)         freopen(s, "r", stdin)
#define W_(s)        freopen(s, "w", stdout)
#define R_W R_("input.txt"),W_("output.txt")
#define PI                        acos(-1.0)
#define INF                       1000000000
#define DFS_GRAY  -1
#define DFS_WHITE  0
#define DFS_BLACK  1

#define scc(x)           scanf("%c",&x)
#define mem(v, d)		memset(v, d, sizeof(v))
#define oo				2000000100
#define OO				4000000000000000100
typedef unsigned int uint;
typedef long long ll;

typedef string            ss;
typedef long long         ll;
typedef pair <int, int>   ii;
typedef pair<ss, int>     si;
typedef pair<int, ss>     is;
typedef pair<char, int>  chi;
typedef pair<ss, ss>     pss;
typedef vector<ii>       vii;
typedef vector<int>       vi;
typedef vector<vi>       vvi;
typedef vector<ss>        vs;
typedef vector<ll>       vll;
typedef vector<vll>     vvll;

//-----------------------------------------------------
const int N = 1e5+15;
const ll MOD = 1e9 + 7;

int dX[] = { 0,0,1,-1 };
int dY[] = { 1,-1,0,0 };

ll GCD(ll num, ll ones) { return !ones ? num : GCD(ones, num % ones); }
ll lcm(ll a, ll b) { return (a / GCD(a, b)) * b; }
double Len(int x, int y, int X, int Y) { return sqrt((X - x) * (X - x) + (Y - y) * (Y - y)); }
ll fastP(ll b, ll p)
{
	if (p == 0) return 1;
	else if (p == 1) return b;
	ll a = fastP(b, p / 2) % MOD;
	if (p % 2) return (((a * a) % MOD) * b) % MOD;
	else return (a * a) % MOD;
}
//----------------- ------   -------
ll n, k, req[N], has[N],temp;
int main()
{
	/*
	#ifndef ONLINE_JUDGE
		freopen("i.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	*/

	scl(n), scl(k);
	lop(i,n)
	{
		scl(temp);
		req[i] = temp;
	}
	lop(i, n)
	{
		scl(temp);
		has[i] = temp;
	}

	ll l = 0, r = 2e9 + 15;
	ll mid, ans = 0;
	unsigned long long counter = 0;
	while (r >= l)
	{
		mid = (r + l) / 2;
		lop(i, n)
		{
			counter += max(ll(0), req[i] * mid - has[i]);
			if (k < counter)break;
		}
		if (k >= counter)
			ans = mid, l = mid + 1;
		else
			r = mid - 1;

		counter = 0;
	}
	cout << ans;
	return 0;
}


#include <iostream>
using namespace std;
int n , k , need[1005] , has[1005];
bool check(int x)
{
int temp = k ;
for (int i = 0 ; i < n ; ++i)
{
	int a = need[i]*x;
	if (a > has[i])
	{
	temp -= (a-has[i]);
	}
}
if (temp<0) return false;
return true;
}

int main() {
cin >> n >> k;
	for (int i = 0 ; i < n ; ++i)
	{
		cin >> need[i];
	}
	for (int i = 0 ; i < n ; ++i)
	{
		cin >> has[i];
	}
	int cok = 0;
	for(int i = 1 ; i <= 2000 ; ++i)
	{
		if (check(i))
		cok = i ;
	}
	cout << cok;
	return 0;
}