// Template
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <tuple>
#include <map>
#include <set>
#include <algorithm>
#include <utility>
#include <cmath>
#define rep(i, x) for (int i = 0; i < (x); ++i)
#define rng(i, x, y) for (int i = (x); i < (y); ++i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pb push_back
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003;
template <typename T> inline bool chmin(T &x, const T &y) {if (x > y) {x = y; return 1;} return 0;}
template <typename T> inline bool chmax(T &x, const T &y) {if (x < y) {x = y; return 1;} return 0;}

// Main Code
int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> a(n, vector<int>(m));
    rep(i, n) rep(j, m) {
        cin >> a[i][j];
        --a[i][j];
    }
    
    vector<bool> use(m, true);
    int ans = inf;
    while (true) {
        vector<int> cnt(m, 0);
        rep(i, n) rep(j, m) {
            if (use[a[i][j]]) {
                ++cnt[a[i][j]];
                break;
            }
        }
        int mx = 0;
        rep(i, m) chmax(mx, cnt[i]);
        if (mx == 0) break;
        chmin(ans, mx);
        rep(i, m) {
            if (cnt[i] == mx) use[i] = false;
        }
    }
    
    cout << ans << "\n";
    return 0;
}

#include<stdio.h>

int h,w;
int map[20][20];
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};

int ans;
int out(int y,int x){
	return y<0||h<=y||x<0||w<=x;
}
void search(int depth,int y,int x){
	int i,ny,nx;
	if(ans <= depth)return;
	for(i=0;i<4;i++){
		ny = y+dy[i];
		nx = x+dx[i];
		if(out(ny,nx) || map[ny][nx]==1)continue;
		do{
			if(map[ny][nx]==3){
				ans = depth;
				return;	
			}
			ny += dy[i];
			nx += dx[i];
		}while(!out(ny,nx) && map[ny][nx]!=1);
		if(!out(ny,nx) && map[ny][nx]==1){
			map[ny][nx]=0;
			search(depth+1,ny-dy[i],nx-dx[i]);
			map[ny][nx]=1;	
		}
	}
}
int main(){
	int i,j;
	int sy,sx;
	while(scanf("%d%d",&w,&h), w|h){
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				scanf("%d",&map[i][j]);
				if(map[i][j]==2){
					sy=i;sx=j;
				}
			}
		}
		ans=11;
		search(1,sy,sx);
		printf("%d\n",ans==11?-1:ans);
	}
}