Problem: problem

There are n cards with numbers from 1 to n. First, make a pile of cards by stacking them in order so that the top is the number 1 card, the second from the top is the number 2 card, ..., and the bottom is the number n card.

<image>



Sort the cards by performing the following operation called "shuffle (x, y)" on the pile of cards (x, y is an integer that satisfies 1 ≤ x <y <n).

Shuffle (x, y)
n cards, pile A consisting of the xth cards from the top, pile B consisting of the x + 1th to yth cards, y + 1 consisting of the nth cards Divide into three mountains of mountain C. Then, mountain B is placed on top of mountain A, and mountain C is placed on top of it.

For example, if you perform "shuffle (3,5)" on 9 cards in order, the numbers written on the 9 cards will be 6, 7, 8, 9, 4 in order from the top. , 5, 1, 2, 3.

<image>



Shuffle m times from the state of the first pile "Shuffle (x1, y1)" "Shuffle (x2, y2)" ... Count from the top in the pile of cards after performing "shuffle (xm, ym)" in order Create a program to find out how many cards with numbers r or less are included in the pth to qth cards.



input

The input consists of multiple datasets. Each dataset is given in the following format.

The input consists of m + 3 lines. The number of cards n is written on the first line (3 ≤ n ≤ 1000000000 = 109). The second line contains the integer m, which represents the number of shuffles (1 ≤ m ≤ 5000). The integers p, q, r are written on the third line (1 ≤ p ≤ q ≤ n, 1 ≤ r ≤ n). On the third line of i + (1 ≤ i ≤ m), two integers xi and yi (1 ≤ xi <yi <n) are written separated by a blank.

When n is 0, it indicates the end of input. The number of data sets does not exceed 5.

output

For each data set, in the pile of cards after m shuffling, output the number of cards whose number is r or less in the pth to qth cards counted from the top in one line. ..

Examples

Input

9
1
3 7 4
3 5
12
3
3 8 5
3 8
2 5
6 10
0


Output

2
3


Input

None


Output

None

Solution (in CPP):

Solution:
#include<iostream>
#include<cassert>
#include<algorithm>
#include<vector>
#include<stack>
#include<list>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)


void simulate(int n,vector<pair<int,int> > & in,const int lim,int l,int r){
  vector<int> a(n);
  rep(i,n)a[i] = i+1;
  for(int i=in.size()-1;i>=0;i--){

  }
  rep(i,lim){

  }
}

/*
  l-rÌRÌª¾¯ðÛµÄAãë©çð¢Ä¢­B
  Rªªô·é±Æª Á½ç»ê¼êÊÉª¯ÄâÁÄµÜÁÄ¢¢BiÍ¸j
  [l,r]
  inÍ[first,second]ðÅèµÄ¼ðXbv·é
  1-origin
 */
int ans = 0;
void solve(const int n,vector<pair<int,int> > &in,int now,const int lim,int l,int r,const int p,const int q){
  //cout << l <<" " << r << endl;
  if (r < l)return;
  if (now == in.size()){
    if (q < l || r < p)return;
    ans += min(r,q)-max(l,p)+1;
    return;
  }
  int p1=in[now].first-1,p2=in[now].second-in[now].first+1,p3=n-p1-p2;//eRÌTCY
  swap(p1,p3);

  if (in[now].second < r){//[max(in[now].second+1,l)..r]ÜÅðãÉÁÄ¢­
    int nextl=in[now].second < l?l-in[now].second:1;
    int nextr=nextl + r-max(in[now].second+1,l);//
    //cout <<l <<" " << r <<" " << in[now].first <<" " << in[now].second << " type:1 " << nextl <<" " << nextr << endl;
    solve(n,in,now+1,lim,nextl,nextr,p,q);
  }
  
  //»ÌÜÜ
  if (!(r < in[now].first || in[now].second < l)){
    int newl = max(in[now].first,l),newr=min(in[now].second,r);
    int nextl = l < in[now].first ? 1+p1 : 1+p1 + l-in[now].first;
    int nextr = nextl + newr-newl;
    //cout <<l <<" " << r <<" " << in[now].first <<" "<< in[now].second <<" type:2 " << nextl <<" " << nextr << endl;
    solve(n,in,now+1,lim,nextl,nextr,p,q);
  }

  if (l < in[now].first){//[l..min(in[now].first-1,r)] ðºÜÅÁÄ¢­B
    int nextl=p1+p2+l;
    int nextr=nextl+min(in[now].first-1,r)-l;
    //cout <<l <<" " << r << " " << in[now].first <<" " << in[now].second << " type:3 " << nextl <<" "<< nextr << endl;
    solve(n,in,now+1,lim,nextl,nextr,p,q);
  }

}

int main(){
  int n,m;
  while(cin>>n && n){
    ans = 0;
    int m,l,r,lim;
    cin>>m;
    cin>>l>>r>>lim;
    vector<pair<int,int> > in(m);
    rep(i,m){
      cin>>in[i].first>>in[i].second;
      in[i].first++;
    }
    //reverse(in.begin(),in.end());
    solve(n,in,0,lim,1,lim,l,r);
    cout << ans << endl;
  }
}