#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
#include <locale>
#include <codecvt>
#include <memory>
#include <iostream>
#include <fstream>
#include <string>
#include <locale>
#include <iomanip>
#include <utility>
#include <algorithm>
 
 
int32_t N;
int32_t p, q, r;
int32_t M;
int32_t X[5000];
int32_t Y[5000];
 
int32_t K[2][5000 * 3][2];//0: s 1: ??????°
auto K_end = K[0];
inline void insert(decltype(K_end) pos, int32_t ns, int32_t nn)
{
    for (auto iter = K_end; iter != pos; --iter) {
        (*iter)[0] = (*(iter - 1))[0];
        (*iter)[1] = (*(iter - 1))[1];
    }
    (*pos)[0] = ns;
    (*pos)[1] = nn;
    ++K_end;
}
int main()
{
    std::cin >> N;
    while (N != 0)
    {
        std::cin >> M >> p >> q >> r;
        for (int32_t i = 0; i < M; ++i) {
            std::cin >> X[i] >> Y[i];
        }
        K_end = K[0];
        (*K_end)[0] = 1;
        (*K_end)[1] = N;
        ++K_end;
 
        for (int32_t i = 0; i < M; ++i)
        {
            //??????
            int num_buf = 0;
            decltype(K_end) x_end_pos = nullptr;
            for (auto iter = K[i & 1]; iter != K_end; ++iter) {
                num_buf += (*iter)[1];
                if (X[i] <= num_buf) {
                    auto nn = (num_buf - X[i]);
                    (*iter)[1] -= nn;
                    insert(iter + 1, (*iter)[0] + (*iter)[1], nn);
                    x_end_pos = iter + 1;
                    break;
                }
            }
            num_buf = X[i];
            decltype(K_end) y_end_pos = nullptr;
            for (auto iter = x_end_pos; iter != K_end; ++iter) {
                num_buf += (*iter)[1];
                if (Y[i] <= num_buf) {
                    auto nn = (num_buf - Y[i]);
                    (*iter)[1] -= nn;
                    insert(iter + 1, (*iter)[0] + (*iter)[1], nn);
                    y_end_pos = iter + 1;
                    break;
                }
            }
            //????????????
            auto iter_out = K[(i + 1) & 1];
            for (auto iter = y_end_pos; iter != K_end; ++iter) {
                (*iter_out)[0] = (*iter)[0];
                (*iter_out)[1] = (*iter)[1];
                ++iter_out;
            }
            for (auto iter = x_end_pos; iter != y_end_pos; ++iter) {
                (*iter_out)[0] = (*iter)[0];
                (*iter_out)[1] = (*iter)[1];
                ++iter_out;
            }
            for (auto iter = K[i & 1]; iter != x_end_pos; ++iter) {
                (*iter_out)[0] = (*iter)[0];
                (*iter_out)[1] = (*iter)[1];
                ++iter_out;
            }
            K_end = iter_out;
        }
 
        //??????
        int32_t num_buf = 0;
        int32_t count = 0;
        for (auto iter = K[M & 1]; iter != K_end; ++iter) {
            auto n_numbuf = num_buf + (*iter)[1];
            auto add = std::min(r - ((*iter)[0] - 1), (*iter)[1]);
            if (num_buf < p) {
                add -= (p - 1 - num_buf);
            }
            if (q < n_numbuf) {
                add = std::min(add, q - num_buf);
            }
            count += std::max(0, add);
            num_buf = n_numbuf;
        }
        std::cout << count << '\n';
 
        std::cin >> N;
    }
}

Measured memory_bytes: 3144