#include using namespace std; #define ALL(a) (a).begin(),(a).end() #define ALLR(a) (a).rbegin(),(a).rend() #define rep(i,n,m) for(int i = (n); i < (int)(m); i++) #define rrep(i,n,m) for(int i = (m) - 1; i >= (int)(n); i--) using ll = long long; using ld = long double; const ll MOD = 1e9+7; //const ll MOD = 998244353; const ll INF = 1e9+1; template bool chmin(T &a,T b){if(a>b){a=b;return true;}else return false;} template bool chmax(T &a,T b){if(a vectordx={1,0,-1,0,1,1,-1,-1}; vectordy={0,1,0,-1,1,-1,1,-1}; template vector make_v(size_t a,T b){return vector(a,b);} template auto make_v(size_t a,Ts... ts){ return vector(a,make_v(ts...)); } int main(){ int n,m,a,b;cin>>n>>m>>a>>b; a--;b--; vectorl(m),r(m); rep(i,0,m){ cin>>l[i]>>r[i]; l[i]--;r[i]--; } int res=INF; ll m2=min(m,60); rep(i,0,1LL<x=i; vectork(n+1); rep(j,0,m){ if(x[j]){ rep(o,l[j],r[j]+1)k[o]^=1; } } int cnt=0; rep(j,0,n)if(k[j]==1&&k[j+1]==0)cnt++; if(cnt>=2)continue; if(k[a]&&k[b])chmin(res,__builtin_popcount(i)); } if(res==INF)cout<<-1<