#include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
using P = pair<int,int>;

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

const int INF = 1000100100;

int main()
{
    int n; cin >> n;
    int m; cin >> m;
    int s[m], c[m];
    rep(i,m) cin >> s[i] >> c[i];

    int rn, sn;
    if(n == 1) rn = 10, sn = 0;
    else if(n == 2) rn = 100, sn=10;
    else rn = 1000,sn=100;

    int res = INF;
    for(int i=sn; i<rn; i++){
        int cnt = 0;
        rep(j,m){
            int sj = n-s[j]+1;
            int num;
            if(sj == 1){
                num = i%10;
                if(c[j] == num) cnt++;
            }else if(sj ==2){
                num = i / 10;
                num %= 10;
                if(c[j] == num) cnt++;
            }else{
                num = i/100;
                if(c[j] == num) cnt++;
            }
        }
        if(cnt == m) res = min(i, res);
    }

    if(res == INF) cout << -1 << endl;
    else cout << res << endl;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;
using vl = vector<ll>;
using Map = map<ll,ll>;
using T = tuple<ll,ll,ll>;
using vvl = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define prt(v) cout<<v<<"\n";
#define fi(v) get<0>(v)
#define se(v) get<1>(v)
#define th(v) get<2>(v)
#define endl "\n"
template <typename T> bool chmax(T &a, const T &b){if (a<b){a=b;return 1;}return 0;}
template <typename T> bool chmin(T &a, const T &b){if (a>b){a=b;return 1;}return 0;}
const ll INF=1LL<<60;
const ll MOD=1000000007;

vector<vector<ll>> G(100007, vector<ll>(0));

void dfs(ll x,vl &d,ll dist){
    d[x]=dist;
    for(auto p: G[x]){
        if(d[p]==INF)dfs(p,d,dist+1);
    }
}

signed main(void){
    cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    ll u;
    cin >> u;u--;
    ll v;
    cin >> v;v--;
    vector<ll> A(N-1,0);
    vector<ll> B(N-1,0);
    for(ll i=0;i<N-1;++i){
        cin>>A[i]>>B[i];
        A[i]--;B[i]--;
        G[A[i]].emplace_back(B[i]);
        G[B[i]].emplace_back(A[i]);
    }
    vector<ll> du(N,INF);
    vector<ll> dv(N,INF);
    dfs(u,du,0);
    dfs(v,dv,0);
    ll mark;
    ll ou=-INF;
    for(ll i=0;i<N;++i){
        if(du[i]<dv[i]&&chmax(ou,dv[i]))mark=i;
    }
    ll nige=du[mark];
    ll ans=nige;
    ll S=ou-nige;//Sは1以上
    if(S==1)ans+=0;
    else if(S==2)ans++;
    else {
        ans+=((S-2)/2)*2;
        S-=((S-2)/2)*2;
        if(S==3)ans+=2;
        else ans++;
    }
    prt(ans)



    return 0;
}
