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

using P = pair<int,int>;

bool solve(){
    int n, m, p, q, r;
    cin >> n;
    if(n==0) return false;
    cin >> m >> p >> q >> r;
    vector<P> v = {P(0,r)}, tmp;  // [a,b)
    for(int i=0;i<m;i++){
        int x, y;
        cin >> x >> y;
        tmp.clear();
        for(auto &e : v){
            int a = e.first, b = e.second;
            if(b <= x){
                int d = n-x;
                tmp.emplace_back(P(a+d,b+d));
            }else if(a >= y){
                tmp.emplace_back(P(a-y,b-y));
            }else{
                if(a < x) tmp.emplace_back(P(a+n-x,n));
                tmp.emplace_back(P(max(a,x)-x+n-y,min(b,y)-x+n-y));
                if(b > y) tmp.emplace_back(P(0,b-y));
            }
        }
        v = tmp;
    }
    int ans = 0;
    p--;
    for(auto &e : v){
        int a = e.first, b = e.second;
        ans += max(min(q,b)-max(p,a),0);
    }
    cout << ans << endl;
    return true;
}

int main(){
    while(solve());
    return 0;
}

