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<vector>

#define AMNT 4096

using namespace std;

typedef pair<int,int> range;
typedef vector< range > deck;

inline int quant(const range &r){
  return r.second-r.first+1;
}

int shuffle(int x, int y, deck &D, bool count = false, int r = 0){
  int i = 0;
  deck d1,d2,d3;
  int cts[D.size()];
  
  for(int i = 0; i < D.size(); ++i){
    cts[i] = (i==0?0:cts[i-1]) + (D[i].second-D[i].first)+1;
  }

  d1.reserve(AMNT);
  d2.reserve(AMNT);
  d3.reserve(AMNT);

  // part I
  int precnt=0, cnt=0;
  for(i; i < D.size(); ++i){
    range R=D[i];
    cnt += quant( R );
    if( cnt >= x ){
      d1.push_back( range( R.first, R.first + (x-precnt) - 1 ) );
      break;
    }else{
      d1.push_back( R );
    }
    precnt = cnt;
  }
  i=max(i-1,0);
  precnt=i<=1?0:cts[i-2];
  cnt=i<=0?0:cts[i-1];
  
  // part II
  bool first=true;
  for(i; i < D.size(); ++i){
    range R = D[i];
    cnt += quant( R );
    if( precnt <= x && precnt <= y && cnt > x && cnt > y ){
      d2.push_back( range( R.first + (x-precnt), R.first + (y-precnt) - 1 )  );
      break;
    }else if( cnt >= x && first ){
      d2.push_back( range( R.first + (x-precnt), R.second ) );
      first = false;
    }else if( cnt >= x && cnt < y  ){
      d2.push_back( R );
    }else if( cnt >= y ){
      d2.push_back( range( R.first, R.first + (y-precnt) - 1 ) );
      break;
    }
    precnt = cnt;
  }
  i=max(i-1,0);
  precnt=i<=1?0:cts[i-2];
  cnt=i<=0?0:cts[i-1];
  
  // part III
  first=true;
  for(i;i<D.size();++i){
    range R = D[i];
    cnt += quant( R );
    if( cnt > y && first ){
      d3.push_back( range( R.first + (y-precnt), R.second ) );
      first=false;
    }else if( !first ){
      d3.push_back( R );
    }
    precnt = cnt;
  }

/*
  cout << "D1" << endl;
  for(deck::iterator itd=d1.begin();itd!=d1.end();++itd){
    cout << '(' << itd->first << " - " << itd->second << ')' << endl;
  }cout << "D2" << endl;
  for(deck::iterator itd=d2.begin();itd!=d2.end();++itd){
    cout << '(' << itd->first << " - " << itd->second << ')' << endl;
  }cout << "D3" << endl;
  for(deck::iterator itd=d3.begin();itd!=d3.end();++itd){
    cout << '(' << itd->first << " - " << itd->second << ')' << endl;
  }
  cout << endl;
*/

  if( count ){
    int ret = 0;
    if( d1.back().second <= r ) ++ret;
    for(i = 0; i < d2.size(); ++i){
      range R = d2[i];
      //cout << R.first << ' ' << R.second << endl;
      if( R.first <= r ){
	ret += ( min(R.second,r) - R.first + 1);
      }
    }
    return ret;
  }


  int ni = 0;
  //  D.clear();
  D.resize( d1.size() + d2.size() + d3.size() );
  for(i = 0; i < d3.size(); ++i){ D[i] = d3[i]; } ni = d3.size();
  for(i = ni; i < ni+d2.size(); ++i){ D[i] = d2[i - ni]; } ni = d3.size() + d2.size();
  for(i = ni; i < ni+d1.size(); ++i){ D[i] = d1[i - ni]; }

  return 0;
}

int main()
{
  while(true){
    int n,m,p,q,r;
    deck D;
    scanf("%d",&n);
    if( n == 0 ) break;
    scanf("%d%d%d%d", &m, &p, &q, &r);
    D.push_back( range(1,n) );
    for(int i = 0; i < m; ++i){
      int x,y;
      scanf("%d%d", &x, &y);
      shuffle( x, y, D );
      // compress
      //CompressDeck( D );
      /*
      for(deck::iterator itd=D.begin();itd!=D.end();++itd){
	cout << '(' << itd->first << " - " << itd->second << ')' << endl;
      }
      cout << endl;
      */
    }
    printf("%d\n", shuffle( p, q, D, true, r ));
  }
  return 0;
}