#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

struct segm { int r; int a; int b; };

struct point {
    int x; int y;
    point(int x, int y):x(x), y(y) {}
};

bool compare1(segm a, segm b) {
    if (a.r != b.r) return (a.r<b.r);
    else return (a.a<b.a);
}

bool compare2(point a, point b) {
    if (a.x != b.x) return (a.x<b.x);
    else return (a.y<b.y);
}

vector<vector<int> > graph;

void add_edge(int i, int j) {
    graph[i].push_back(j);
    graph[j].push_back(i);
}

int main()
{
    int x0, y0, x1, y1, n;
    vector<segm> segments;
    vector<point> points;
    points.reserve(100000);
    cin >> x0 >> y0 >> x1 >> y1 >> n;
    segments.resize(n);
    for (int i=0; i<n; i++) cin >> segments[i].r >> segments[i].a >> segments[i].b;
    sort(segments.begin(), segments.end(), compare1);
    int now = 0, right = 0;
    for (int i=0; i<n; i++) {
        if (now != segments[i].r) {
            now = segments[i].r;
            right = 0;
        }
        for (int j=max(segments[i].a, right+1); j<=segments[i].b; j++) points.push_back(*(new point(now, j)));
        if (segments[i].b > right) right = segments[i].b;
    }
    graph.resize(points.size());
    for (int i=0; i<points.size(); i++) graph[i].reserve(8);
    for (int i=0; i<points.size(); i++) {
        vector<point>::iterator a;
        a = lower_bound(points.begin(), points.end(), point(points[i].x-1, points[i].y-1), compare2);
        if (a->x == points[i].x-1 && a->y == points[i].y-1) add_edge(int(a-points.begin()), i);
        a = lower_bound(points.begin(), points.end(), point(points[i].x, points[i].y-1), compare2);
        if (a->x == points[i].x && a->y == points[i].y-1) add_edge(int(a-points.begin()), i);
        a = lower_bound(points.begin(), points.end(), point(points[i].x+1, points[i].y-1), compare2);
        if (a->x == points[i].x+1 && a->y == points[i].y-1) add_edge(int(a-points.begin()), i);
        a = lower_bound(points.begin(), points.end(), point(points[i].x-1, points[i].y), compare2);
        if (a->x == points[i].x-1 && a->y == points[i].y) add_edge(int(a-points.begin()), i);
    }
    vector<int> time(points.size());
    vector<bool> used(points.size());
    queue<int> q;
    q.push((int)(lower_bound(points.begin(), points.end(), point(x0, y0), compare2) - points.begin()));
    used[q.front()] = true;
    while(!q.empty()) {
        int v=q.front();
        q.pop();
        for (int i=0; i<graph[v].size(); i++) {
            if (!used[graph[v][i]]) {
                q.push(graph[v][i]);
                time[graph[v][i]] = time[v] + 1;
                used[graph[v][i]] = true;
            }
        }
    }
    int k = time[(int)(lower_bound(points.begin(), points.end(), point(x1, y1), compare2) - points.begin())];
    if (k == 0) cout << -1;
    else cout << k;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

struct segm { int r; int a; int b; };

struct point {
    int x; int y;
    point(int x, int y):x(x), y(y) {}
};

bool compare1(segm a, segm b) {
    if (a.r != b.r) return (a.r<b.r);
    else return (a.a<b.a);
}

bool compare2(point a, point b) {
    if (a.x != b.x) return (a.x<b.x);
    else return (a.y<b.y);
}

vector<vector<int> > graph;

void add_edge(int i, int j) {
    graph[i].push_back(j);
    graph[j].push_back(i);
}

int main()
{
    int x0, y0, x1, y1, n;
    vector<segm> segments;
    vector<point> points;
    points.reserve(100000);
    cin >> x0 >> y0 >> x1 >> y1 >> n;
    segments.resize(n);
    for (int i=0; i<n; i++) cin >> segments[i].r >> segments[i].a >> segments[i].b;
    sort(segments.begin(), segments.end(), compare1);
    int now = 0, right = 0;
    for (int i=0; i<n; i++) {
        if (now != segments[i].r) {
            now = segments[i].r;
            right = 0;
        }
        for (int j=max(segments[i].a, right+1); j<=segments[i].b; j++) points.push_back(*(new point(now, j)));
        if (segments[i].b > right) right = segments[i].b;
    }
    sort(points.begin(), points.end(), compare2);
    graph.resize(points.size(), *(new vector<int>(8)));
    for (int i=0; i<points.size(); i++) {
        vector<point>::iterator a;
        a = lower_bound(points.begin(), points.end(), point(points[i].x-1, points[i].y-1), compare2);
        if (a->x == points[i].x-1 && a->y == points[i].y-1) add_edge(int(a-points.begin()), i);
        a = lower_bound(points.begin(), points.end(), point(points[i].x, points[i].y-1), compare2);
        if (a->x == points[i].x && a->y == points[i].y-1) add_edge(int(a-points.begin()), i);
        a = lower_bound(points.begin(), points.end(), point(points[i].x+1, points[i].y-1), compare2);
        if (a->x == points[i].x+1 && a->y == points[i].y-1) add_edge(int(a-points.begin()), i);
        a = lower_bound(points.begin(), points.end(), point(points[i].x-1, points[i].y), compare2);
        if (a->x == points[i].x-1 && a->y == points[i].y) add_edge(int(a-points.begin()), i);
    }
    vector<int> time(points.size());
    vector<bool> used(n);
    queue<int> q;
    q.push((int)(lower_bound(points.begin(), points.end(), point(x0, y0), compare2) - points.begin()));
    used[q.front()] = true;
    while(!q.empty()) {
        int v=q.front();
        q.pop();
        for (int i=0; i<graph[v].size(); i++) {
            if (!used[graph[v][i]]) {
                q.push(graph[v][i]);
                time[graph[v][i]] = time[v] + 1;
                used[graph[v][i]] = true;
            }
        }
    }
    int k = time[(int)(lower_bound(points.begin(), points.end(), point(x1, y1), compare2) - points.begin())];
    if (k == 0) cout << -1;
    else cout << k;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

struct segm { int r; int a; int b; };

struct point {
    int x; int y;
    point(int x, int y):x(x), y(y) {}
};

bool compare1(segm a, segm b) {
    if (a.r != b.r) return (a.r<b.r);
    else return (a.a<b.a);
}

bool compare2(point a, point b) {
    if (a.x != b.x) return (a.x<b.x);
    else return (a.y<b.y);
}

vector<vector<int> > graph;

void add_edge(int i, int j) {
    graph[i].push_back(j);
    graph[j].push_back(i);
}

int main()
{
    int x0, y0, x1, y1, n;
    vector<segm> segments;
    vector<point> points;
    points.reserve(100000);
    cin >> x0 >> y0 >> x1 >> y1 >> n;
    segments.resize(n);
    for (int i=0; i<n; i++) cin >> segments[i].r >> segments[i].a >> segments[i].b;
    sort(segments.begin(), segments.end(), compare1);
    int now = 0, right = 0;
    for (int i=0; i<n; i++) {
        if (now != segments[i].r) {
            now = segments[i].r;
            right = 0;
        }
        for (int j=max(segments[i].a, right+1); j<=segments[i].b; j++) points.push_back(*(new point(now, j)));
        if (segments[i].b > right) right = segments[i].b;
    }
    graph.resize(points.size());
    for (int i=0; i<points.size(); i++) graph[i].reserve(8);
    for (int i=0; i<points.size(); i++) {
        vector<point>::iterator a;
        a = lower_bound(points.begin(), points.end(), point(points[i].x-1, points[i].y-1), compare2);
        if (a->x == points[i].x-1 && a->y == points[i].y-1) add_edge(int(a-points.begin()), i);
        a = lower_bound(points.begin(), points.end(), point(points[i].x, points[i].y-1), compare2);
        if (a->x == points[i].x && a->y == points[i].y-1) add_edge(int(a-points.begin()), i);
        a = lower_bound(points.begin(), points.end(), point(points[i].x+1, points[i].y-1), compare2);
        if (a->x == points[i].x+1 && a->y == points[i].y-1) add_edge(int(a-points.begin()), i);
        a = lower_bound(points.begin(), points.end(), point(points[i].x-1, points[i].y), compare2);
        if (a->x == points[i].x-1 && a->y == points[i].y) add_edge(int(a-points.begin()), i);
    }
    vector<int> time(points.size());
    vector<bool> used(n);
    queue<int> q;
    q.push((int)(lower_bound(points.begin(), points.end(), point(x0, y0), compare2) - points.begin()));
    used[q.front()] = true;
    while(!q.empty()) {
        int v=q.front();
        q.pop();
        for (int i=0; i<graph[v].size(); i++) {
            if (!used[graph[v][i]]) {
                q.push(graph[v][i]);
                time[graph[v][i]] = time[v] + 1;
                used[graph[v][i]] = true;
            }
        }
    }
    int k = time[(int)(lower_bound(points.begin(), points.end(), point(x1, y1), compare2) - points.begin())];
    if (k == 0) cout << -1;
    else cout << k;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;

int main ()
{
	return 0;
}


#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef pair <int, int> pr;

#define X first
#define Y second
#define sz size ()

map <pr, int> M;
set <pr> S;

pr get ()
{
	int x, y;
	scanf ("%d%d", &x, &y);
	return pr (x, y);
}
int main ()
{
	pr st=get (), ed=get ();
	int n;scanf ("%d", &n);
	while (n --)
	{
		int r, a, b;
		scanf ("%d%d%d", &r, &a, &b);
		for (int i=a ; i <= b ; i ++)
			S.insert (pr (r, i));
	}
	queue <pr> que;
	que.push (st);
	M[st] = 0;
	while (que.sz)
	{
		pr P=que.front ();que.pop ();
		int d=M[P];
		for (int i=1 ; i >= -1 ; i --)
			for (int j=1 ; j >= -1 ; j --)
			{
				pr PP=pr (P.X+i, P.Y+j);
				if (!M.count (PP) && S.count (PP))
				{
					M[PP] = d+1;
					que.push (PP);
					if (PP==ed)return printf ("%d\n", d+1), 0;
				}
			}
	}puts ("-1");
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;

int main ()
{
	return 1;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;

int main ()
{
	return 0*printf ("%d", 6);
}


#include <iostream>
#include <queue>
#include <set>
#include <utility>
using namespace std;

int main(int argc,char**argv){
    int x0,y0,x,y,n;
    int i,j,r,a,b;
    set<pair<int,int> > s;
    queue<pair<int,pair<int,int> > > q;
    cin >> x0 >> y0 >> x >> y >> n;
    for(i=0;i<n;i++){
        cin >> r >> a >> b;
        for(j=a;j<=b;j++)
            set.insert(make_pair(r,j));
    }
    q.push(make_pair(0,make_pair(x0,y0)));
    while(!q.empty()){
        int cr = q.front().first;
        int cx = q.front().second.first;
        int cy = q.front().second.second;
        q.pop();
        for(int i=-1;i<=1;i++)
            for(int j=-1;j<=1;j++){
                if(cx+i==x && cy+j == y){
                    cout << cr+1 << endl;
                    return 0;
                }
                if(i==0&&j==0)
                    continue;
                if(s.count(cx+i,cy+j)>0){
                    q.push(make_pair(cr+1,make_pair(cx+i,cy+j)));
                    s.erase(cx+i,cy+j);
                }
            }
    }
    cout << -1 << endl;
    return 0;
}


#include <iostream>
#include <queue>
#include <set>
#include <utility>
using namespace std;

int main(int argc,char**argv){
    int x0,y0,x,y,n;
    int i,j,r,a,b;
    set<pair<int,int> > s;
    queue<pair<int,pair<int,int> > > q;
    cin >> x0 >> y0 >> x >> y >> n;
    for(i=0;i<n;i++){
        cin >> r >> a >> b;
        for(j=a;j<=b;j++)
            s.insert(make_pair(r,j));
    }
    q.push(make_pair(0,make_pair(x0,y0)));
    while(!q.empty()){
        int cr = q.front().first;
        int cx = q.front().second.first;
        int cy = q.front().second.second;
        q.pop();
        for(int i=-1;i<=1;i++)
            for(int j=-1;j<=1;j++){
                if(cx+i==x && cy+j == y){
                    cout << cr+1 << endl;
                    return 0;
                }
                if(i==0&&j==0)
                    continue;
                if(s.count(make_pair(cx+i,cy+j))>0){
                    q.push(make_pair(cr+1,make_pair(cx+i,cy+j)));
                    s.erase(cx+i,cy+j);
                }
            }
    }
    cout << -1 << endl;
    return 0;
}


#include <iostream>
#include <queue>
#include <set>
#include <utility>
using namespace std;

int main(int argc,char**argv){
    int x0,y0,x,y,n;
    int i,j,r,a,b;
    set<pair<int,int> > s;
    queue<pair<int,pair<int,int> > > q;
    cin >> x0 >> y0 >> x >> y >> n;
    for(i=0;i<n;i++){
        cin >> r >> a >> b;
        for(j=a;j<=b;j++)
            s.insert(make_pair(r,j));
    }
    q.push(make_pair(0,make_pair(x0,y0)));
    while(!q.empty()){
        int cr = q.front().first;
        int cx = q.front().second.first;
        int cy = q.front().second.second;
        q.pop();
        for(int i=-1;i<=1;i++)
            for(int j=-1;j<=1;j++){
                if(cx+i==x && cy+j == y){
                    cout << cr+1 << endl;
                    return 0;
                }
                if(i==0&&j==0)
                    continue;
                if(s.count(cx+i,cy+j)>0){
                    q.push(make_pair(cr+1,make_pair(cx+i,cy+j)));
                    s.erase(cx+i,cy+j);
                }
            }
    }
    cout << -1 << endl;
    return 0;
}


#include <iostream>
#include <queue>
#include <set>
#include <utility>
using namespace std;

int main(int argc,char**argv){
    int x0,y0,x,y,n;
    int i,j,r,a,b;
    set<pair<int,int> > s;
    queue<pair<int,pair<int,int> > > q;
    cin >> x0 >> y0 >> x >> y >> n;
    for(i=0;i<n;i++){
        cin >> r >> a >> b;
        for(j=a;j<=b;j++)
            s.insert(make_pair(r,j));
    }
    q.push(make_pair(0,make_pair(x0,y0)));
    while(!q.empty()){
        int cr = q.front().first;
        int cx = q.front().second.first;
        int cy = q.front().second.second;
        q.pop();
        for(int i=-1;i<=1;i++)
            for(int j=-1;j<=1;j++){
                if(cx+i==x && cy+j == y){
                    cout << cr+1 << endl;
                    return 0;
                }
                if(i==0&&j==0)
                    continue;
                if(s.count(make_pair(cx+i,cy+j))>0){
                    q.push(make_pair(cr+1,make_pair(cx+i,cy+j)));
                    s.erase(make_pair(cx+i,cy+j));
                }
            }
    }
    cout << -1 << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <cmath>
#include <vector>
using namespace std;
int n;
struct otrez
{
    long long r,l;
};
otrez a[100003],temp[100003],c[100003];
vector < int > g;
int coun=0;
int head=1;
int tail=0;
bool mainflag=false;
struct xod
{
    long long res;
    int step;
};
xod b[100003];
long long finish;
long long move[10];
void mergesort(otrez *a,otrez *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i<n;i+=width*2)
        {
            i0=i;
            i1=iRight=min(i+width,n);
            iEnd=min(i+width*2,n);
            for(j=i;j<iEnd;j++)
                if(i0<iRight && (i1==iEnd || a[i0].l<=a[i1].l))
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<iEnd;j++)
                a[j]=b[j];
        }
}
void bfs()
{
    move[0]=-2000000001;
    move[1]=-2000000000;
    move[2]=-1999999999;
    move[3]=-1;
    move[4]=1;
    move[5]=1999999999;
    move[6]=2000000000;
    move[7]=2000000001;
    g.push_back(b[0].res);
    int le,ri,res;
    int i,j;
    while(head>tail)
    {
        for(j=0;j<8;j++)
        {
            if(finish==b[tail].res+move[j])
            {
                mainflag=true;
                printf("%d\n",b[tail].step+1);
                break;
            }
            else
            {
                le=0;
                ri=coun;
                while(ri>le+1)
                {
                    res=((ri+le)>>1);
                    if(b[tail].res+move[j]>=c[res].l)
                        le=res;
                    else
                        ri=res;
                }
                for(i=0;i<g.size();i++)
                    if(g[i]==b[tail].res+move[j])
                        break;
                if(c[le].l<=b[tail].res+move[j] && c[le].r>=b[tail].res+move[j] && i==g.size())
                {
                    b[head].res=b[tail].res+move[j];
                    b[head].step=b[tail].step+1;
                    g.push_back(b[head].res);
                    head++;
                }
            }
        }
        if(mainflag)
            break;
        tail++;
    }
}
int main()
{
    long long xx,yy,xxx,yyy;
    scanf("%I64d %I64d %I64d %I64d",&yy,&xx,&yyy,&xxx);
    b[0].res=yy*2000000000+xx;
    b[0].step=0;
    finish=yyy*2000000000+xxx;
    scanf("%d",&n);
    int i,j;
    long long nom;
    for(i=0;i<n;i++)
    {
        scanf("%I64d %I64d %I64d",&nom,&xx,&yy);
        a[i].l=xx+nom*2000000000;
        a[i].r=yy+nom*2000000000;
    }
    mergesort(a,temp,n);
    i=0;
    while(i<n)
    {
        j=i+1;
        while(j<n && a[j].l+1<=a[i].r)
            j++;
        c[coun].l=a[i].l;
        c[coun++].r=a[j-1].r;
        i=j;
    }
    bfs();
    if(!mainflag)
        printf("-1\n");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <cmath>
using namespace std;
int n;
struct otrez
{
    long long r,l;
};
otrez a[100003],temp[100003],c[100003];
int coun=0;
int head=1;
int tail=0;
bool mainflag=false;
struct xod
{
    long long res;
    int step;
};
xod b[100003];
long long finish;
long long move[10];
void mergesort(otrez *a,otrez *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i<n;i+=width*2)
        {
            i0=i;
            i1=iRight=min(i+width,n);
            iEnd=min(i+width*2,n);
            for(j=i;j<iEnd;j++)
                if(i0<iRight && (i1==iEnd || a[i0].l<=a[i1].l))
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<iEnd;j++)
                a[j]=b[j];
        }
}
void bfs()
{
    move[0]=-2000000001;
    move[1]=-2000000000;
    move[2]=-1999999999;
    move[3]=-1;
    move[4]=1;
    move[5]=1999999999;
    move[6]=2000000000;
    move[7]=2000000001;
    int le,ri,res;
    int i,j;
    while(head>tail && tail<100003 && head<100003)
    {
        for(j=0;j<8;j++)
        {
            if(finish==b[tail].res+move[j])
            {
                mainflag=true;
                printf("%d\n",b[tail].step+1);
                break;
            }
            else
            {
                le=0;
                ri=coun;
                while(ri>le+1)
                {
                    res=((ri+le)>>1);
                    if(b[tail].res+move[j]>=c[res].l)
                        le=res;
                    else
                        ri=res;
                }
                if(c[le].l<=b[tail].res+move[j] && c[le].r>=b[tail].res+move[j] && tail<100003)
                {
                    b[head].res=b[tail].res+move[j];
                    b[head].step=b[tail].step+1;
                    head++;
                }
            }
        }
        if(mainflag)
            break;
        tail++;
    }
}
int main()
{
    long long xx,yy,xxx,yyy;
    scanf("%I64d %I64d %I64d %I64d",&yy,&xx,&yyy,&xxx);
    b[0].res=yy*2000000000+xx;
    b[0].step=0;
    finish=yyy*2000000000+xxx;
    scanf("%d",&n);
    int i,j;
    long long nom;
    for(i=0;i<n;i++)
    {
        scanf("%I64d %I64d %I64d",&nom,&xx,&yy);
        a[i].l=xx+nom*2000000000;
        a[i].r=yy+nom*2000000000;
    }
    mergesort(a,temp,n);
    i=0;
    while(i<n)
    {
        j=i+1;
        while(j<n && a[j].l+1<=a[i].r)
            j++;
        c[coun].l=a[i].l;
        c[coun++].r=a[j-1].r;
        i=j;
    }
    bfs();
    if(!mainflag)
        printf("-1\n");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <cmath>
#include <vector>
using namespace std;
int n;
struct otrez
{
    long long r,l;
};
otrez a[100003],temp[100003],c[100003];
vector < long long > g;
int coun=0;
int head=1;
int tail=0;
bool mainflag=false;
struct xod
{
    long long res;
    int step;
};
xod b[100003];
long long finish;
long long move[10];
void mergesort(otrez *a,otrez *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i<n;i+=width*2)
        {
            i0=i;
            i1=iRight=min(i+width,n);
            iEnd=min(i+width*2,n);
            for(j=i;j<iEnd;j++)
                if(i0<iRight && (i1==iEnd || a[i0].l<=a[i1].l))
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<iEnd;j++)
                a[j]=b[j];
        }
}
void bfs()
{
    move[0]=-2000000001;
    move[1]=-2000000000;
    move[2]=-1999999999;
    move[3]=-1;
    move[4]=1;
    move[5]=1999999999;
    move[6]=2000000000;
    move[7]=2000000001;
    g.push_back(b[0].res);
    int le,ri,res;
    int i,j;
    while(head>tail)
    {
        for(j=0;j<8;j++)
        {
            if(finish==b[tail].res+move[j])
            {
                mainflag=true;
                printf("%d\n",b[tail].step+1);
                break;
            }
            else
            {
                le=0;
                ri=coun;
                while(ri>le+1)
                {
                    res=((ri+le)>>1);
                    if(b[tail].res+move[j]>=c[res].l)
                        le=res;
                    else
                        ri=res;
                }
                for(i=0;i<g.size();i++)
                    if(g[i]==b[tail].res+move[j])
                        break;
                if(c[le].l<=b[tail].res+move[j] && c[le].r>=b[tail].res+move[j] && i==g.size())
                {
                    b[head].res=b[tail].res+move[j];
                    b[head].step=b[tail].step+1;
                    g.push_back(b[head].res);
                    head++;
                }
            }
        }
        if(mainflag)
            break;
        tail++;
    }
}
int main()
{
    long long xx,yy,xxx,yyy;
    scanf("%I64d %I64d %I64d %I64d",&yy,&xx,&yyy,&xxx);
    b[0].res=yy*2000000000+xx;
    b[0].step=0;
    finish=yyy*2000000000+xxx;
    scanf("%d",&n);
    int i,j;
    long long nom;
    for(i=0;i<n;i++)
    {
        scanf("%I64d %I64d %I64d",&nom,&xx,&yy);
        a[i].l=xx+nom*2000000000;
        a[i].r=yy+nom*2000000000;
    }
    mergesort(a,temp,n);
    i=0;
    while(i<n)
    {
        j=i+1;
        while(j<n && a[j].l+1<=a[i].r)
            j++;
        c[coun].l=a[i].l;
        c[coun++].r=a[j-1].r;
        i=j;
    }
    bfs();
    if(!mainflag)
        printf("-1\n");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <cmath>
#include <vector>
using namespace std;
int n;
struct otrez
{
    long long r,l;
};
otrez a[100003],temp[100003],c[100003];
vector < long long > g;
int coun=0;
int head=1;
int tail=0;
bool mainflag=false;
struct xod
{
    long long res;
    int step;
};
xod b[100003];
long long finish;
long long move[10];
void mergesort(otrez *a,otrez *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i<n;i+=width*2)
        {
            i0=i;
            i1=iRight=min(i+width,n);
            iEnd=min(i+width*2,n);
            for(j=i;j<iEnd;j++)
                if(i0<iRight && (i1==iEnd || a[i0].l<=a[i1].l))
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<iEnd;j++)
                a[j]=b[j];
        }
}
void bfs()
{
    move[0]=-2000000001;
    move[1]=-2000000000;
    move[2]=-1999999999;
    move[3]=-1;
    move[4]=1;
    move[5]=1999999999;
    move[6]=2000000000;
    move[7]=2000000001;
    int le,ri,res;
    int i,j;
    while(head>tail)
    {
        g.push_back(b[tail].res);
        for(j=0;j<8;j++)
        {
            if(finish==b[tail].res+move[j])
            {
                mainflag=true;
                printf("%d\n",b[tail].step+1);
                break;
            }
            else
            {
                le=0;
                ri=coun;
                while(ri>le+1)
                {
                    res=((ri+le)>>1);
                    if(b[tail].res+move[j]>=c[res].l)
                        le=res;
                    else
                        ri=res;
                }
                for(i=0;i<g.size();i++)
                    if(g[i]==b[tail].res+move[j])
                        break;
                if(c[le].l<=b[tail].res+move[j] && c[le].r>=b[tail].res+move[j] && i==g.size())
                {
                    b[head].res=b[tail].res+move[j];
                    b[head].step=b[tail].step+1;
                    head++;
                }
            }
        }
        if(mainflag)
            break;
        tail++;
    }
}
int main()
{
    long long xx,yy,xxx,yyy;
    scanf("%I64d %I64d %I64d %I64d",&yy,&xx,&yyy,&xxx);
    b[0].res=yy*2000000000+xx;
    b[0].step=0;
    finish=yyy*2000000000+xxx;
    scanf("%d",&n);
    int i,j;
    long long nom;
    for(i=0;i<n;i++)
    {
        scanf("%I64d %I64d %I64d",&nom,&xx,&yy);
        a[i].l=xx+nom*2000000000;
        a[i].r=yy+nom*2000000000;
    }
    mergesort(a,temp,n);
    i=0;
    while(i<n)
    {
        j=i+1;
        while(j<n && a[j].l+1<=a[i].r)
            j++;
        c[coun].l=a[i].l;
        c[coun++].r=a[j-1].r;
        i=j;
    }
    bfs();
    if(!mainflag)
        printf("-1\n");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <cmath>
using namespace std;
int n;
struct otrez
{
    long long r,l;
};
otrez a[100003],temp[100003],c[100003];
int coun=0;
int head=1;
int tail=0;
bool mainflag=false;
struct xod
{
    long long res;
    int step;
};
xod b[1000003];
long long finish;
long long move[10];
void mergesort(otrez *a,otrez *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i<n;i+=width*2)
        {
            i0=i;
            i1=iRight=min(i+width,n);
            iEnd=min(i+width*2,n);
            for(j=i;j<iEnd;j++)
                if(i0<iRight && (i1==iEnd || a[i0].l<=a[i1].l))
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<iEnd;j++)
                a[j]=b[j];
        }
}
void bfs()
{
    move[0]=-2000000001;
    move[1]=-2000000000;
    move[2]=-1999999999;
    move[3]=-1;
    move[4]=1;
    move[5]=1999999999;
    move[6]=2000000000;
    move[7]=2000000001;
    int le,ri,res;
    int i,j;
    while(head>tail && tail<1000003 && head<1000003)
    {
        for(j=0;j<8;j++)
        {
            if(finish==b[tail].res+move[j])
            {
                mainflag=true;
                printf("%d\n",b[tail].step+1);
                break;
            }
            else
            {
                le=0;
                ri=coun;
                while(ri>le+1)
                {
                    res=((ri+le)>>1);
                    if(b[tail].res+move[j]>=c[res].l)
                        le=res;
                    else
                        ri=res;
                }
                if(c[le].l<=b[tail].res+move[j] && c[le].r>=b[tail].res+move[j] && head<1000003)
                {
                    b[head].res=b[tail].res+move[j];
                    b[head].step=b[tail].step+1;
                    head++;
                }
            }
        }
        if(mainflag)
            break;
        tail++;
    }
}
int main()
{
    long long xx,yy,xxx,yyy;
    scanf("%I64d %I64d %I64d %I64d",&yy,&xx,&yyy,&xxx);
    b[0].res=yy*2000000000+xx;
    b[0].step=0;
    finish=yyy*2000000000+xxx;
    scanf("%d",&n);
    int i,j;
    long long nom;
    for(i=0;i<n;i++)
    {
        scanf("%I64d %I64d %I64d",&nom,&xx,&yy);
        a[i].l=xx+nom*2000000000;
        a[i].r=yy+nom*2000000000;
    }
    mergesort(a,temp,n);
    i=0;
    while(i<n)
    {
        j=i+1;
        while(j<n && a[j].l+1<=a[i].r)
            j++;
        c[coun].l=a[i].l;
        c[coun++].r=a[j-1].r;
        i=j;
    }
    bfs();
    if(!mainflag)
        printf("-1\n");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <cmath>
using namespace std;
int n;
struct otrez
{
    long long r,l;
};
otrez a[100003],temp[100003],c[100003];
int coun=0;
int head=1;
int tail=0;
bool mainflag=false;
struct xod
{
    long long res;
    int step;
};
xod b[1000003];
long long finish;
long long move[10];
void mergesort(otrez *a,otrez *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i<n;i+=width*2)
        {
            i0=i;
            i1=iRight=min(i+width,n);
            iEnd=min(i+width*2,n);
            for(j=i;j<iEnd;j++)
                if(i0<iRight && (i1==iEnd || a[i0].l<=a[i1].l))
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<iEnd;j++)
                a[j]=b[j];
        }
}
void bfs()
{
    move[0]=-2000000001;
    move[1]=-2000000000;
    move[2]=-1999999999;
    move[3]=-1;
    move[4]=1;
    move[5]=1999999999;
    move[6]=2000000000;
    move[7]=2000000001;
    int le,ri,res;
    int i,j;
    while(head>tail && tail<1000003 && head<1000003)
    {
        for(j=0;j<8;j++)
        {
            if(finish==b[tail].res+move[j])
            {
                mainflag=true;
                printf("%d\n",b[tail].step+1);
                break;
            }
            else
            {
                le=0;
                ri=coun;
                while(ri>le+1)
                {
                    res=((ri+le)>>1);
                    if(b[tail].res+move[j]>=c[res].l)
                        le=res;
                    else
                        ri=res;
                }
                if(c[le].l<=b[tail].res+move[j] && c[le].r>=b[tail].res+move[j] && tail<1000003)
                {
                    b[head].res=b[tail].res+move[j];
                    b[head].step=b[tail].step+1;
                    head++;
                }
            }
        }
        if(mainflag)
            break;
        tail++;
    }
}
int main()
{
    long long xx,yy,xxx,yyy;
    scanf("%I64d %I64d %I64d %I64d",&yy,&xx,&yyy,&xxx);
    b[0].res=yy*2000000000+xx;
    b[0].step=0;
    finish=yyy*2000000000+xxx;
    scanf("%d",&n);
    int i,j;
    long long nom;
    for(i=0;i<n;i++)
    {
        scanf("%I64d %I64d %I64d",&nom,&xx,&yy);
        a[i].l=xx+nom*2000000000;
        a[i].r=yy+nom*2000000000;
    }
    mergesort(a,temp,n);
    i=0;
    while(i<n)
    {
        j=i+1;
        while(j<n && a[j].l+1<=a[i].r)
            j++;
        c[coun].l=a[i].l;
        c[coun++].r=a[j-1].r;
        i=j;
    }
    bfs();
    if(!mainflag)
        printf("-1\n");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <cmath>
#include <set>
using namespace std;
int n;
struct otrez
{
    long long r,l;
};
otrez a[100003],temp[100003],c[100003];
set<long long> g;
int coun=0;
int head=1;
int tail=0;
bool mainflag=false;
struct xod
{
    long long res;
    int step;
};
xod b[100003];
long long finish;
long long move[10];
void mergesort(otrez *a,otrez *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i<n;i+=width*2)
        {
            i0=i;
            i1=iRight=min(i+width,n);
            iEnd=min(i+width*2,n);
            for(j=i;j<iEnd;j++)
                if(i0<iRight && (i1==iEnd || a[i0].l<=a[i1].l))
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<iEnd;j++)
                a[j]=b[j];
        }
}
void bfs()
{
    move[0]=-2000000001;
    move[1]=-2000000000;
    move[2]=-1999999999;
    move[3]=-1;
    move[4]=1;
    move[5]=1999999999;
    move[6]=2000000000;
    move[7]=2000000001;
    g.insert(b[0].res);
    int le,ri,res;
    int i,j;
    while(head>tail)
    {
        for(j=0;j<8;j++)
        {
            if(finish==b[tail].res+move[j])
            {
                mainflag=true;
                printf("%d\n",b[tail].step+1);
                break;
            }
            else
            {
                le=0;
                ri=coun;
                while(ri>le+1)
                {
                    res=((ri+le)>>1);
                    if(b[tail].res+move[j]>=c[res].l)
                        le=res;
                    else
                        ri=res;
                }
                if(c[le].l<=b[tail].res+move[j] && c[le].r>=b[tail].res+move[j] && g.find(b[tail].res+move[j]) == g.end())
                {
                    b[head].res=b[tail].res+move[j];
                    b[head].step=b[tail].step+1;
                    g.insert(b[head].res);
                    head++;
                }
            }
        }
        if(mainflag)
            break;
        tail++;
    }
}
int main()
{
    long long xx,yy,xxx,yyy;
    scanf("%I64d %I64d %I64d %I64d",&yy,&xx,&yyy,&xxx);
    b[0].res=yy*2000000000+xx;
    b[0].step=0;
    finish=yyy*2000000000+xxx;
    scanf("%d",&n);
    int i,j;
    long long nom;
    for(i=0;i<n;i++)
    {
        scanf("%I64d %I64d %I64d",&nom,&xx,&yy);
        a[i].l=xx+nom*2000000000;
        a[i].r=yy+nom*2000000000;
    }
    mergesort(a,temp,n);
    i=0;
    while(i<n)
    {
        j=i+1;
        while(j<n && a[j].l+1<=a[i].r)
            j++;
        c[coun].l=a[i].l;
        c[coun++].r=a[j-1].r;
        i=j;
    }
    bfs();
    if(!mainflag)
        printf("-1\n");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <cmath>
using namespace std;
int n;
struct otrez
{
    long long r,l;
};
otrez a[100003],temp[100003],c[100003];
int coun=0;
int head=1;
int tail=0;
bool mainflag=false;
struct xod
{
    long long res;
    int step;
};
xod b[10000003];
long long finish;
long long move[10];
void mergesort(otrez *a,otrez *b,int n)
{
    int i,i0,i1,iRight,iEnd,width,j;
    for(width=1;width<n;width*=2)
        for(i=0;i<n;i+=width*2)
        {
            i0=i;
            i1=iRight=min(i+width,n);
            iEnd=min(i+width*2,n);
            for(j=i;j<iEnd;j++)
                if(i0<iRight && (i1==iEnd || a[i0].l<=a[i1].l))
                    b[j]=a[i0++];
                else
                    b[j]=a[i1++];
            for(j=i;j<iEnd;j++)
                a[j]=b[j];
        }
}
void bfs()
{
    move[0]=-2000000001;
    move[1]=-2000000000;
    move[2]=-1999999999;
    move[3]=-1;
    move[4]=1;
    move[5]=1999999999;
    move[6]=2000000000;
    move[7]=2000000001;
    int le,ri,res;
    int i,j;
    while(head>tail && tail<10000003 && head<10000003)
    {
        for(j=0;j<8;j++)
        {
            if(finish==b[tail].res+move[j])
            {
                mainflag=true;
                printf("%d\n",b[tail].step+1);
                break;
            }
            else
            {
                le=0;
                ri=coun;
                while(ri>le+1)
                {
                    res=((ri+le)>>1);
                    if(b[tail].res+move[j]>=c[res].l)
                        le=res;
                    else
                        ri=res;
                }
                if(c[le].l<=b[tail].res+move[j] && c[le].r>=b[tail].res+move[j] && head<10000003)
                {
                    b[head].res=b[tail].res+move[j];
                    b[head].step=b[tail].step+1;
                    head++;
                }
            }
        }
        if(mainflag)
            break;
        tail++;
    }
}
int main()
{
    long long xx,yy,xxx,yyy;
    scanf("%I64d %I64d %I64d %I64d",&yy,&xx,&yyy,&xxx);
    b[0].res=yy*2000000000+xx;
    b[0].step=0;
    finish=yyy*2000000000+xxx;
    scanf("%d",&n);
    int i,j;
    long long nom;
    for(i=0;i<n;i++)
    {
        scanf("%I64d %I64d %I64d",&nom,&xx,&yy);
        a[i].l=xx+nom*2000000000;
        a[i].r=yy+nom*2000000000;
    }
    mergesort(a,temp,n);
    i=0;
    while(i<n)
    {
        j=i+1;
        while(j<n && a[j].l+1<=a[i].r)
            j++;
        c[coun].l=a[i].l;
        c[coun++].r=a[j-1].r;
        i=j;
    }
    bfs();
    if(!mainflag)
        printf("-1\n");
    return 0;
}


//In The Name Of God

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const ll Maxn = 1e5 + 7;
const ll Max = 2e2 + 7;
const ll Mod = 1e9 + 7;
const ll Inf = 2e9 + 7;

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define P_ll pair<ll, ll>
#define P_cl pair<char, ll>
#define P_lc pair<ll, char>
#define Sort( a ) sort ( a.begin(), a.end() )
#define God ios_base::sync_with_stdio ( false )

map<P_ll, bool>Free, mark;
map<P_ll, ll>dis;
vector<P_ll>adj;
ll d[5];

void f ( ll i )
{
	if ( i == 3 )
	{
		if ( !d[1] && !d[2] )
			return ;
		adj.pb ( mp ( d[1], d[2] ) );
		return ;
	}
	d[i] = -1;
	f ( i + 1 );
	d[i] = 0;
	f ( i + 1 );
	d[i] = 1;
	f ( i + 1 );
}

void bfs ( ll x, ll y )
{
	dis[mp ( x, y )] = 0;
	queue<P_ll>q;
	q.push ( mp ( x, y ) );
	while ( q.size() )
	{
		ll X = q.front().F, Y = q.front().S;
		q.pop();
		mark[mp ( X, Y )]++;
		for ( ll i = 0; i < 8; i++ )
		{
			ll xx = X + adj[i].F, yy = Y + adj[i].S;
			if ( Free[mp ( xx, yy )] )
			{
				if ( dis[mp ( xx, yy )] > dis[mp ( X, Y )] || dis[mp ( xx, yy )] == 0 )
					dis[mp ( xx, yy )] = dis[mp ( X, Y )] + 1, q.push ( mp ( xx, yy ) );
			}
		}
	}
}

int main()
{
	ll x0, y0, x1, y1;
	f ( 1 );
	cin >> x0 >> y0 >> x1 >> y1;
	ll n;
	cin >> n;
	for ( ll i = 1; i <= n; i++ )
	{
		ll r, a, b;
		cin >> r >> a >> b;
		for ( ll j = a; j <= b; j++ )
			Free[mp ( r, j )]++;
	}
	Free[mp ( x0, y0 )]++;
	bfs ( x0, y0 );
	if ( dis[mp ( x1, y1 )] == 0 )
		cout << -1 << endl;
	else
		cout << dis[mp ( x1, y1 )] << endl;
	return 0;
}


//In The Name Of God

#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef long double ld;

const ll Maxn = 1e5 + 7;
const ll Max = 2e2 + 7;
const ll Mod = 1e9 + 7;
const ll Inf = 1e9 + 7;

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define P_ll pair<ll, ll>
#define P_cl pair<char, ll>
#define P_lc pair<ll, char>
#define Sort( a ) sort ( a.begin(), a.end() )
#define God ios_base::sync_with_stdio ( false )

map<P_ll, bool>Free, mark;
map<P_ll, ll>dis;
vector<P_ll>adj;
ll d[5];

void f ( ll i )
{
	if ( i == 3 )
	{
		if ( !d[1] && !d[2] )
			return ;
		adj.pb ( mp ( d[1], d[2] ) );
		return ;
	}
	d[i] = -1;
	f ( i + 1 );
	d[i] = 0;
	f ( i + 1 );
	d[i] = 1;
	f ( i + 1 );
}

void bfs ( ll x, ll y )
{
	dis[mp ( x, y )] = 0;
	queue<P_ll>q;
	q.push ( mp ( x, y ) );
	while ( q.size() )
	{
		ll X = q.front().F, Y = q.front().S;
		q.pop();
		mark[mp ( X, Y )]++;
		for ( ll i = 0; i < 8; i++ )
		{
			ll xx = X + adj[i].F, yy = Y + adj[i].S;
			if ( Free[mp ( xx, yy )] )
			{
				if ( dis[mp ( xx, yy )] > dis[mp ( X, Y )] || dis[mp ( xx, yy )] == 0 )
					dis[mp ( xx, yy )] = dis[mp ( X, Y )] + 1, q.push ( mp ( xx, yy ) );
			}
		}
	}
}

int main()
{
	God;
	ll x0, y0, x1, y1, n;
	f ( 1 );
	scanf ( "%d%d%d%d%d", &x0, &y0, &x1, &y1 , &n );
	for ( ll i = 1; i <= n; i++ )
	{
		ll r, a, b;
		scanf ( "%d%d%d", &r, &a, &b );
		for ( ll j = a; j <= b; j++ )
			Free[mp ( r, j )]++;
	}
	Free[mp ( x0, y0 )]++;
	bfs ( x0, y0 );
	if ( dis[mp ( x1, y1 )] == 0 )
		puts ( "-1\n" );
	else
		printf ( "%d\n", dis[mp ( x1, y1 )] );
	//cout << dis[mp ( x1, y1 )] << endl;
	return 0;
}


//In The Name Of God

#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef long double ld;

const ll Maxn = 1e5 + 7;
const ll Max = 2e2 + 7;
const ll Mod = 1e9 + 7;
const ll Inf = 1e9 + 7;

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define P_ll pair<ll, ll>
#define P_cl pair<char, ll>
#define P_lc pair<ll, char>
#define Sort( a ) sort ( a.begin(), a.end() )
#define God ios_base::sync_with_stdio ( false )

map<P_ll, bool>Free, mark;
map<P_ll, ll>dis;
vector<P_ll>adj;
ll d[5];

void f ( ll i )
{
	if ( i == 3 )
	{
		if ( !d[1] && !d[2] )
			return ;
		adj.pb ( mp ( d[1], d[2] ) );
		return ;
	}
	d[i] = -1;
	f ( i + 1 );
	d[i] = 0;
	f ( i + 1 );
	d[i] = 1;
	f ( i + 1 );
}

void bfs ( ll x, ll y )
{
	dis[mp ( x, y )] = 0;
	queue<P_ll>q;
	q.push ( mp ( x, y ) );
	while ( q.size() )
	{
		P_ll p=q.front();
		ll X=p.F,Y=p.S;
		q.pop();
		mark[mp ( X, Y )]++;
		for ( ll i = 0; i < 8; i++ )
		{
			ll xx = X + adj[i].F, yy = Y + adj[i].S;
			if ( Free[mp ( xx, yy )] )
			{
				if ( dis[mp ( xx, yy )] > dis[mp ( X, Y )] || dis[mp ( xx, yy )] == 0 )
					dis[mp ( xx, yy )] = dis[mp ( X, Y )] + 1, q.push ( mp ( xx, yy ) );
			}
		}
	}
}

int main()
{
	God;
	ll x0, y0, x1, y1, n;
	f ( 1 );
	scanf ( "%d%d%d%d%d", &x0, &y0, &x1, &y1 , &n );
	for ( ll i = 1; i <= n; i++ )
	{
		ll r, a, b;
		scanf ( "%d%d%d", &r, &a, &b );
		for ( ll j = a; j <= b; j++ )
			Free[mp ( r, j )]++;
	}
	Free[mp ( x0, y0 )]++;
	bfs ( x0, y0 );
	if ( dis[mp ( x1, y1 )] == 0 )
		puts ( "-1\n" );
	else
		printf ( "%d\n", dis[mp ( x1, y1 )] );
	//cout << dis[mp ( x1, y1 )] << endl;
	return 0;
}


//In The Name Of God

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const ll Maxn = 1e5 + 7;
const ll Max = 2e2 + 7;
const ll Mod = 1e9 + 7;
const ll Inf = 2e9 + 7;

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define P_ll pair<ll, ll>
#define P_cl pair<char, ll>
#define P_lc pair<ll, char>
#define Sort( a ) sort ( a.begin(), a.end() )
#define God ios_base::sync_with_stdio ( false )

map<P_ll, bool>Free, mark;
map<P_ll, ll>dis;
vector<P_ll>adj;
ll d[5];

void f ( ll i )
{
	if ( i == 3 )
	{
		if ( !d[1] && !d[2] )
			return ;
		adj.pb ( mp ( d[1], d[2] ) );
		return ;
	}
	d[i] = -1;
	f ( i + 1 );
	d[i] = 0;
	f ( i + 1 );
	d[i] = 1;
	f ( i + 1 );
}

void bfs ( ll x, ll y )
{
	dis[mp ( x, y )] = 0;
	queue<P_ll>q;
	q.push ( mp ( x, y ) );
	while ( q.size() )
	{
		ll X = q.front().F, Y = q.front().S;
		q.pop();
		mark[mp ( X, Y )]++;
		for ( ll i = 0; i < 8; i++ )
		{
			ll xx = X + adj[i].F, yy = Y + adj[i].S;
			if ( Free[mp ( xx, yy )] )
			{
				if ( dis[mp ( xx, yy )] > dis[mp ( X, Y )] || dis[mp ( xx, yy )] == 0 )
					dis[mp ( xx, yy )] = dis[mp ( X, Y )] + 1, q.push ( mp ( xx, yy ) );
			}
		}
	}
}

int main()
{
	God;
	ll x0, y0, x1, y1;
	f ( 1 );
	cin >> x0 >> y0 >> x1 >> y1;
	ll n;
	cin >> n;
	for ( ll i = 1; i <= n; i++ )
	{
		ll r, a, b;
		cin >> r >> a >> b;
		for ( ll j = a; j <= b; j++ )
			Free[mp ( r, j )]++;
	}
	Free[mp ( x0, y0 )]++;
	bfs ( x0, y0 );
	if ( dis[mp ( x1, y1 )] == 0 )
		cout << -1 << endl;
	else
		cout << dis[mp ( x1, y1 )] << endl;
	return 0;
}


//In The Name Of God

#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef long double ld;

const ll Maxn = 1e5 + 7;
const ll Max = 2e2 + 7;
const ll Mod = 1e9 + 7;
const ll Inf = 1e9 + 7;

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define P_ll pair<ll, ll>
#define P_cl pair<char, ll>
#define P_lc pair<ll, char>
#define Sort( a ) sort ( a.begin(), a.end() )
#define God ios_base::sync_with_stdio ( false )

map<P_ll, bool>Free, mark;
map<P_ll, ll>dis;
vector<P_ll>adj;
ll d[5];

void f ( ll i )
{
	if ( i == 3 )
	{
		if ( !d[1] && !d[2] )
			return ;
		adj.pb ( mp ( d[1], d[2] ) );
		return ;
	}
	d[i] = -1;
	f ( i + 1 );
	d[i] = 0;
	f ( i + 1 );
	d[i] = 1;
	f ( i + 1 );
}

void bfs ( ll x, ll y )
{
	dis[mp ( x, y )] = 0;
	queue<P_ll>q;
	q.push ( mp ( x, y ) );
	while ( q.size() )
	{
		P_ll p = q.front();
		ll X = p.F, Y = p.S;
		q.pop();
		mark[mp ( X, Y )]++;
		for ( ll i = 0; i < 8; i++ )
		{
			ll xx = X + adj[i].F, yy = Y + adj[i].S;
			if ( Free[mp ( xx, yy )] )
			{
				Free[mp ( xx, yy )] = 0;
				if ( dis[mp ( xx, yy )] > dis[mp ( X, Y )] || dis[mp ( xx, yy )] == 0 )
					dis[mp ( xx, yy )] = dis[mp ( X, Y )] + 1, q.push ( mp ( xx, yy ) );
			}
		}
	}
}

int main()
{
	God;
	ll x0, y0, x1, y1, n;
	f ( 1 );
	scanf ( "%d%d%d%d%d", &x0, &y0, &x1, &y1 , &n );
	for ( ll i = 1; i <= n; i++ )
	{
		ll r, a, b;
		scanf ( "%d%d%d", &r, &a, &b );
		for ( ll j = a; j <= b; j++ )
			Free[mp ( r, j )]++;
	}
	Free[mp ( x0, y0 )]++;
	bfs ( x0, y0 );
	if ( dis[mp ( x1, y1 )] == 0 )
		puts ( "-1\n" );
	else
		printf ( "%d\n", dis[mp ( x1, y1 )] );
	//cout << dis[mp ( x1, y1 )] << endl;
	return 0;
}


//In The Name Of God

#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef long double ld;

const ll Maxn = 1e5 + 7;
const ll Max = 2e2 + 7;
const ll Mod = 1e9 + 7;
const ll Inf = 2e9 + 7;

#define pb push_back
#define mp make_pair
#define F first
#define S second
#define P_ll pair<ll, ll>
#define P_cl pair<char, ll>
#define P_lc pair<ll, char>
#define Sort( a ) sort ( a.begin(), a.end() )
#define God ios_base::sync_with_stdio ( false )

map<P_ll, bool>Free, mark;
map<P_ll, ll>dis;
vector<P_ll>adj;
ll d[5];

void f ( ll i )
{
	if ( i == 3 )
	{
		if ( !d[1] && !d[2] )
			return ;
		adj.pb ( mp ( d[1], d[2] ) );
		return ;
	}
	d[i] = -1;
	f ( i + 1 );
	d[i] = 0;
	f ( i + 1 );
	d[i] = 1;
	f ( i + 1 );
}

void bfs ( ll x, ll y )
{
	dis[mp ( x, y )] = 0;
	queue<P_ll>q;
	q.push ( mp ( x, y ) );
	while ( q.size() )
	{
		ll X = q.front().F, Y = q.front().S;
		q.pop();
		mark[mp ( X, Y )]++;
		for ( ll i = 0; i < 8; i++ )
		{
			ll xx = X + adj[i].F, yy = Y + adj[i].S;
			if ( Free[mp ( xx, yy )] )
			{
				if ( dis[mp ( xx, yy )] > dis[mp ( X, Y )] || dis[mp ( xx, yy )] == 0 )
					dis[mp ( xx, yy )] = dis[mp ( X, Y )] + 1, q.push ( mp ( xx, yy ) );
			}
		}
	}
}

int main()
{
	God;
	ll x0, y0, x1, y1;
	f ( 1 );
	scanf ( "%d%d%d%d", &x0, &y0, &x1, &y1 );
	//cin >> x0 >> y0 >> x1 >> y1;
	ll n;
	cin >> n;
	for ( ll i = 1; i <= n; i++ )
	{
		ll r, a, b;
		scanf ( "%d%d%d", &r, &a, &b );
		for ( ll j = a; j <= b; j++ )
			Free[mp ( r, j )]++;
	}
	Free[mp ( x0, y0 )]++;
	bfs ( x0, y0 );
	if ( dis[mp ( x1, y1 )] == 0 )
		puts ( "-1\n" );
	else
		printf ( "%d\n", dis[mp ( x1, y1 )] );
	//cout << dis[mp ( x1, y1 )] << endl;
	return 0;
}


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

#define ll unsigned long long int
#define pb push_back
#define p1da(a,n) for(int i=0;i<(n);i++) cout << *( (a) + (i))<<" "; cout<<endl;
#define g1da(a,n) for(int i=0;i<(n);i++) cin >> *( (a) + (i)) ;
#define fi first
#define se second
#define f(i,n) for(int (i)=0;(i)<(n) ; (i)++ )
#define fr(i,s,e) for(int (i)=s;(i)<=(e) ; (i)++ )
int main()
{
    double n,x,y;
    ll p;
    cin>>n>>p;

    vector <double> fact,l,r;
    vector <double> prob,opp;
    double ans;
    f(i,n)
    {
        cin>>x>>y;
        l.pb(x);
        r.pb(y);
    }
    f(i,n)
    {
        fact.pb((ll) (r[i]/p)-(ll)(l[i]/p));
        if((ll)(l[i])%p==0)
            fact[i]+=1;
    }

    f(i,n)
    {
        prob.pb(1-fact[i]/(r[i]-l[i]+1));
        // cout<<prob[i]<<" ";
    }
    //cout<<endl;
    f(i,n)
    {
        if(i==0)
            opp.pb(1-prob[n-1]*prob[i]);
        else
            opp.pb(1-prob[i-1]*prob[i]);

        //cout<<opp[i]<<' ';
    }
    //cout<<endl;
    ans=0.0;
    f(i,n)
        ans+=opp[i];
    cout<<ans*2000;

}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,i,j,k,l,r,p;
	double a,b,c,ans;
	vector <double> v;

	cin>>n>>p;
	ans = 0;

	for(i=0;i<n;i++){
		cin>>l>>r;
		k = r/p - (l-1)/p;
		a = k;
		a/=(r-l+1);
		v.push_back(a);
		ans+= 2*a;
	}
	for(i=0;i<n;i++){
		j = (i+1)%n;
		ans-=v[i]*v[j];
	}
	ans*=2000;
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n,m,i,j,k,l,r,p;
	double a,b,c,ans;
	vector <double> v;

	cin>>n>>p;
	ans = 0;

	for(i=0;i<n;i++){
		cin>>l>>r;
		k = r/p - (l-1)/p;
		a = k;
		a/=(r-l+1);
		v.push_back(a);
		ans+= 2*a;
	}
	for(i=0;i<n;i++){
		j = (i+1)%n;
		ans-=v[i]*v[j];
	}
	ans*=2000;
	cout<<fixed<<setprecision(7)<<ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;

//#pragma comment(linker, "/stack:36777216")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")

ifstream fin("input.txt");
ofstream fout("output.txt");

bool isPrime(int x) {
	if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;
	for (int i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1;
}

int NumDigits(int x) {
	//x = abs(x);
	return (x < 10 ? 1 :
		(x < 100 ? 2 :
		(x < 1000 ? 3 :
			(x < 10000 ? 4 :
			(x < 100000 ? 5 :
				(x < 1000000 ? 6 :
				(x < 10000000 ? 7 :
					(x < 100000000 ? 8 :
					(x < 1000000000 ? 9 :
						10)))))))));
}

inline void boostIO() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cout << fixed << setprecision(10);
}

typedef long long int ll;
typedef long double ld;

#define fori(n)  for(int i = 0; i < (n); ++i)
#define forj(n)  for(int j = 0; j < (n); ++j)
#define fork(n) for(int k = 0; k < (n); ++k)
#define forx(n) for(int x = 0; x < (n); ++x)

#define LONG_LONG_MAX  9223372036854775807
#define LONG_LONG_MIN   -9223372036854775807
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))

#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define debug(x) cerr << #x << " = " << x << endl;
#define debug_p(x) cerr << #x << " " << x.ff << " " << x.ss << endl;
#define debug_v(x) cerr << #x << " "; for (auto ii : x) cout << i << " "; cout << endl;
#define debug_vp(x) cerr << #x << " "; for (auto ii : x) cout << '[' << ii.ff << " " << ii.ss << ']'; cout << endl;
#define mp make_pair

#define Return cerr<<endl<<"RUNTIME: "<<1.0*clock()/CLOCKS_PER_SEC << " s" <<endl; return 0;

#define PI 3.14159265

ll Pow10[19] = { (ll)1,(ll)1e1,(ll)1e2,(ll)1e3,(ll)1e4,(ll)1e5,(ll)1e6,(ll)1e7,(ll)1e8,(ll)1e9,(ll)1e10,
				(ll)1e11,(ll)1e12,(ll)1e13,(ll)1e14,(ll)1e15,(ll)1e16,(ll)1e17,(ll)1e18 };

//int mid = low + ((high - low) / 2);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

/*
struct comp {
	bool operator()(Mine const & a, Mine const & b)const {
		return a.g >= b.g;
	}
};
set<Mine, comp> Set;
*/

//__builtin_clz(int); amount of 0 in the begining
//__builtin_ctz(int); amoint of 0 in the end
//__builtin_popcount(int); amount of 1

//memset(dp, -1, sizeof dp);

template<typename T> inline T getint() {
	T val = 0;
	char c;

	bool neg = false;
	while ((c = getchar()) && !(c >= '0' && c <= '9')) {
		neg |= c == '-';
	}

	do {
		val = (val * 10) + c - '0';
	} while ((c = getchar()) && (c >= '0' && c <= '9'));

	return val * (neg ? -1 : 1);
}

/*
template<typename T> T getint() {
	T val = 0;
	char c;

	bool neg = false;
	while ((c = getchar()) && !(c >= '0' && c <= '9')) {
		neg |= c == '-';
	}

	do {
		val = (val * 10) + c - '0';
	} while ((c = getchar()) && (c >= '0' && c <= '9'));

	return val * (neg ? -1 : 1);
}
*/

/////////////////////
#define int long long
int Mod = 1e9 + 7;
#define endl '\n'
////////////////////

int mul(int a, int b, int c = 1, int d = 1, int e = 1, int g = 1) {
	a %= Mod; b %= Mod; c %= Mod; d %= Mod; e %= Mod; g %= Mod;
	return (a * b % Mod * c % Mod * d % Mod * e % Mod * g % Mod);
}

int Pow(int a, int b) {
	if (b == 0)
		return 1;
	if (b % 2 == 0) {
		int x = Pow(a, b / 2);
		return mul(x, x);
	}
	else {
		int x = Pow(a, b / 2);
		return  mul(a, x, x);
	}
}
ll inf = 1e18;
struct cords {
	int x = 0;
	int y = 0;
};

//struct comp {
	//bool operator()(My const& a, My const& b) const {
		//return a.x < b.x;
	//}
//};;
struct My {
	ll left = 0;
	ll right = 0;
	ll sum = 0;
	ll max = 0;
};

int32_t main() {
	boostIO();
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	//int T; cin >> T;
	int n, p; cin >> n >> p;
	vector<pair<int, int>> A(n);
	int all = 0;
	fori(n) {
		int a, b; cin >> a >> b;
		A[i] = { a, b };
	}
	A.push_back(A[0]);

	ld ans = 0;
	fori(n) {
		int a, b; tie(a, b) = A[i];
		int a2, b2; tie(a2, b2) = A[i + 1];
		int x = (b / p - (a - 1) / p);
		int x2 = (b2 / p - (a2 - 1) / p);
		int len = (b - a + 1);
		int len2 = (b2 - a2 + 1);

		ans += (ld)(x * len2 + x2 * len - x * x2) / (ld) (len * len2);
	}
	cout << ans * 2000;
	Return;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;

//#pragma comment(linker, "/stack:36777216")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")

ifstream fin("input.txt");
ofstream fout("output.txt");

bool isPrime(int x) {
	if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;
	for (int i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1;
}

int NumDigits(int x) {
	//x = abs(x);
	return (x < 10 ? 1 :
		(x < 100 ? 2 :
		(x < 1000 ? 3 :
			(x < 10000 ? 4 :
			(x < 100000 ? 5 :
				(x < 1000000 ? 6 :
				(x < 10000000 ? 7 :
					(x < 100000000 ? 8 :
					(x < 1000000000 ? 9 :
						10)))))))));
}

inline void boostIO() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cout << fixed << setprecision(10);
}

typedef long long int ll;
typedef long double ld;

#define fori(n)  for(int i = 0; i < (n); ++i)
#define forj(n)  for(int j = 0; j < (n); ++j)
#define fork(n) for(int k = 0; k < (n); ++k)
#define forx(n) for(int x = 0; x < (n); ++x)

#define LONG_LONG_MAX  9223372036854775807
#define LONG_LONG_MIN   -9223372036854775807
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))

#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define debug(x) cerr << #x << " = " << x << endl;
#define debug_p(x) cerr << #x << " " << x.ff << " " << x.ss << endl;
#define debug_v(x) cerr << #x << " "; for (auto ii : x) cout << i << " "; cout << endl;
#define debug_vp(x) cerr << #x << " "; for (auto ii : x) cout << '[' << ii.ff << " " << ii.ss << ']'; cout << endl;
#define mp make_pair

#define Return cerr<<endl<<"RUNTIME: "<<1.0*clock()/CLOCKS_PER_SEC << " s" <<endl; return 0;

#define PI 3.14159265

ll Pow10[19] = { (ll)1,(ll)1e1,(ll)1e2,(ll)1e3,(ll)1e4,(ll)1e5,(ll)1e6,(ll)1e7,(ll)1e8,(ll)1e9,(ll)1e10,
				(ll)1e11,(ll)1e12,(ll)1e13,(ll)1e14,(ll)1e15,(ll)1e16,(ll)1e17,(ll)1e18 };

//int mid = low + ((high - low) / 2);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

/*
struct comp {
	bool operator()(Mine const & a, Mine const & b)const {
		return a.g >= b.g;
	}
};
set<Mine, comp> Set;
*/

//__builtin_clz(int); amount of 0 in the begining
//__builtin_ctz(int); amoint of 0 in the end
//__builtin_popcount(int); amount of 1

//memset(dp, -1, sizeof dp);

template<typename T> inline T getint() {
	T val = 0;
	char c;

	bool neg = false;
	while ((c = getchar()) && !(c >= '0' && c <= '9')) {
		neg |= c == '-';
	}

	do {
		val = (val * 10) + c - '0';
	} while ((c = getchar()) && (c >= '0' && c <= '9'));

	return val * (neg ? -1 : 1);
}

/*
template<typename T> T getint() {
	T val = 0;
	char c;

	bool neg = false;
	while ((c = getchar()) && !(c >= '0' && c <= '9')) {
		neg |= c == '-';
	}

	do {
		val = (val * 10) + c - '0';
	} while ((c = getchar()) && (c >= '0' && c <= '9'));

	return val * (neg ? -1 : 1);
}
*/

/////////////////////
#define int long long
int Mod = 1e9 + 7;
#define endl '\n'
////////////////////

int mul(int a, int b, int c = 1, int d = 1, int e = 1, int g = 1) {
	a %= Mod; b %= Mod; c %= Mod; d %= Mod; e %= Mod; g %= Mod;
	return (a * b % Mod * c % Mod * d % Mod * e % Mod * g % Mod);
}

int Pow(int a, int b) {
	if (b == 0)
		return 1;
	if (b % 2 == 0) {
		int x = Pow(a, b / 2);
		return mul(x, x);
	}
	else {
		int x = Pow(a, b / 2);
		return  mul(a, x, x);
	}
}
ll inf = 1e18;
struct cords {
	int x = 0;
	int y = 0;
};

//struct comp {
	//bool operator()(My const& a, My const& b) const {
		//return a.x < b.x;
	//}
//};;
struct My {
	ll left = 0;
	ll right = 0;
	ll sum = 0;
	ll max = 0;
};

int32_t main() {
	boostIO();
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	//int T; cin >> T;
	int n, p; cin >> n >> p;
	vector<pair<int, int>> A(n);
	int all = 0;
	fori(n) {
		int a, b; cin >> a >> b;
		A[i] = { a, b };
	}
	A.push_back(A[0]);

	ld ans = 0;
	fori(n) {
		int a, b; tie(a, b) = A[i];
		int a2, b2; tie(a2, b2) = A[i + 1];
		int x = (b / p - (a - 1) / p);
		int x2 = (b2 / p - (a - 1) / p);
		int len = (b - a + 1);
		int len2 = (b2 - a2 + 1);

		ans += (ld)(x * len2 + x2 * len - x * x2) / (ld) (len * len2);
	}
	cout << ans * 2000;
	Return;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define __ ios_base::sync_with_stdio(0);cin.tie(0);
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(),x.end()
#define forn(i,a,n) for(int i=a; i < n; i++)
typedef long long int lli;
typedef long double Double;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
const int maxn = 1e6+5;

int main(){__
	int n,p;
	cin >> n >> p;
	vector<pii> a(n);
	for(int i=0; i < n; i++){
		cin >> a[i].fi >> a[i].se;
	}
	Double ans = 0.0;

	for(int i=0; i < n; i++){
		int sig = (i+1)%n;
		lli uno = a[i].se/p - (a[i].fi-1)/p;
		lli dos = a[sig].se/p - (a[sig].fi-1)/p;
		lli sz1 = (a[i].se - a[i].fi+1),sz2 = (a[sig].se - a[sig].fi +1);
		lli arriba = uno * sz2 + dos * sz1 - (uno * dos);
		Double prob = Double(arriba)/Double(sz1*sz2);
		ans += prob * 2000.0;
	}
	cout << fixed << setprecision(10) << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define forn(i,a,n) for(int i =a; i < n; i++)
#define se second
#define fi first
#define pb push_back
#define all(x) x.begin(), x.end()
#define __ ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long int lli;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define MAXN 100005
double prob[MAXN];

double getprob(double a ,double b){
	return 2000.0 * (a + b - a*b);
}

int main(){__
	int n,p,a,b;
	cin >> n >> p;
	forn(i,0,n){
		cin >> a >> b;
		prob[i] = double((b/p) - (a-1)/p)/double(b-a+1);
		//cout << prob[i] << endl;
	}
	double ans = 0.0;
	forn(i,0,n-1){
		ans+=getprob(prob[i],prob[i+1]);
	}
	ans+=getprob(prob[0],prob[n-1]);
	cout << fixed << setprecision(10) << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define faf ios_base::sync_with_stdio(false),cin.tie(nullptr)
#define ll long long
#define pb push_back
#define mp make_pair
#define nt _int128
#define ld long double
long double PI =3.14159265358979323846;

int modo=1e9+7;
const ll inf=1e13;
const ll ms=(1e5) +5;
int ar[ms][2]={0};

ll gett(ll l,ll r,ll p)
{
    int p1=ceil((l*1.0)/p);
    int p2=r/p;
    int aa=max(0,p2+1-p1);
    return aa;
}

int main()
{
    faf;
    ll n;
    ll p;
    cin>>n;
    cin>>p;
    ll l;
    ll r;
    ll i;
    ll tp=1;
    for(i=0;i<n;i++)
    {
        cin>>l>>r;
        ar[i][0]=gett(l,r,p);
        ar[i][1]=(r+1-l)-ar[i][0];
        tp*=(r+1-l);
    }
    ar[n][0]=ar[0][0];
    ar[n][1]=ar[0][1];
    //for(i=0;i<=n;i++)cout<<ar[i][0]<<" "<<ar[i][1]<<endl;
    ll pp=0;

    for(i=0;i<n;i++) {
        l = ar[i][0];
        r = ar[i + 1][0] + ar[i + 1][1];
        ll a = ar[i][0] + ar[i][1];
        ll b = ar[i + 1][0] + ar[i + 1][1];

        ll kk = tp / (a * b);
        pp += (l * r * kk);
        l = ar[i][1];
        r = ar[i + 1][0];
        pp += (l * r * kk);
    }
   // cout<<pp<<" "<<tp<<endl;
    ld  vv=2000.00;
    ld ans= (pp*vv)/tp;
    cout<<fixed;
    cout<<setprecision(10);
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define faf ios_base::sync_with_stdio(false),cin.tie(nullptr)
#define ll long long
#define pb push_back
#define mp make_pair
#define nt _int128
#define ld long double
long double PI =3.14159265358979323846;

int modo=1e9+7;
const ll inf=1e13;
const ll ms=(1e5) +5;
int ar[ms][2]={0};

ll gett(ll l,ll r,ll p)
{
    int p1=ceil((l*1.0)/p);
    int p2=r/p;
    int aa=max(0,p2+1-p1);
    return aa;
}

int main()
{
    faf;
    ll n;
    ll p;
    cin>>n;
    cin>>p;
    ll l;
    ll r;
    ll i;
    ll tp=1;
    for(i=0;i<n;i++)
    {
        cin>>l>>r;
        ar[i][0]=gett(l,r,p);
        ar[i][1]=(r+1-l)-ar[i][0];
        tp*=(r+1-l);
    }
    ar[n][0]=ar[0][0];
    ar[n][1]=ar[0][1];
    //for(i=0;i<=n;i++)cout<<ar[i][0]<<" "<<ar[i][1]<<endl;
    ll pp=0;
    ld  vv=2000.00;
    ld ans=0;
    for(i=0;i<n;i++) {
        l = ar[i][0];
        r = ar[i + 1][0] + ar[i + 1][1];
        ll a = ar[i][0] + ar[i][1];
        ll b = ar[i + 1][0] + ar[i + 1][1];

        ans+=(l*r*vv)/(a*b);
        //pp += (l * r * kk);
        l = ar[i][1];
        r = ar[i + 1][0];
        ans += (l * r * vv)/(a*b);
    }
   // cout<<pp<<" "<<tp<<endl;

    cout<<fixed;
    cout<<setprecision(10);
    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define faf ios_base::sync_with_stdio(false),cin.tie(nullptr)
#define ll long long
#define pb push_back
#define mp make_pair
#define nt _int128
#define ld long double
long double PI =3.14159265358979323846;

int modo=1e9+7;
const ll inf=1e13;
const ll ms=(1e5) +5;
int ar[ms][2]={0};

int gett(int l,int r,int p)
{
    int p1=ceil((l*1.0)/p);
    int p2=r/p;
    int aa=max(0,p2+1-p1);
    return aa;
}

int main()
{
    faf;
    int n;
    int p;
    cin>>n;
    cin>>p;
    int l;
    int r;
    int i;
    ll tp=1;
    for(i=0;i<n;i++)
    {
        cin>>l>>r;
        ar[i][0]=gett(l,r,p);
        ar[i][1]=(r+1-l)-ar[i][0];
        tp*=(r+1-l);
    }
    ar[n][0]=ar[0][0];
    ar[n][1]=ar[0][1];
    //for(i=0;i<=n;i++)cout<<ar[i][0]<<" "<<ar[i][1]<<endl;
    ll pp=0;

    for(i=0;i<n;i++) {
        l = ar[i][0];
        r = ar[i + 1][0] + ar[i + 1][1];
        ll a = ar[i][0] + ar[i][1];
        ll b = ar[i + 1][0] + ar[i + 1][1];

        ll kk = tp / (a * b);
        pp += (l * r * kk);
        l = ar[i][1];
        r = ar[i + 1][0];
        pp += (l * r * kk);
    }
   // cout<<pp<<" "<<tp<<endl;

    ld ans= (pp*2000.00)/tp;
    cout<<fixed;
    cout<<setprecision(10);
    cout<<ans;

    return 0;
}


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

#define endl '\n'
#define ll long long
#define rint register int

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007
#define infl LLONG_MAX

#define Foxen(i,s) for (i=s.begin(); i!=s.end(); i++)
#define Fill(s,v) memset(s,v,sizeof(s))
#define cout_p(x, p) cout << fixed << setprecision((p)) << x << endl     //print with precision

#define tc(tt) \
    ll tt;    \
    cin >> tt; \
    for (ll _tt = 0; _tt < tt; _tt++) // testcase

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ll t, i, x, j, y, p,	z, k, n;
	db  ans = 0;

	cin >> n >> p;
	pll a[n + 1], b[n + 1];

	rep(i, 0, n, 1) {
		cin >> x >> y;
		a[i].ff = x;
		a[i].ss = y;
		b[i].ff = y - x + 1;
		b[i].ss = y / p - x / p + (x % p == 0);
	}
	b[n].ff = b[0].ff;
	b[n].ss = b[0].ss;

	rep(i, 0, n, 1) {
		double u, v, x, y;

		v = b[i].ff, u = b[i].ss;
		y = b[i + 1].ff, x = b[i + 1].ss;

		ans += 1 - ((v - u ) / v) * ((y - x ) / y) ;
	}

	cout << fixed << setprecision(10) << ans * 2000 << endl;
}


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

#define endl '\n'
#define ll long long
#define rint register int

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007
#define infl LLONG_MAX

#define Foxen(i,s) for (i=s.begin(); i!=s.end(); i++)
#define Fill(s,v) memset(s,v,sizeof(s))
#define cout_p(x, p) cout << fixed << setprecision((p)) << x << endl     //print with precision

#define tc(tt) \
    ll tt;    \
    cin >> tt; \
    for (ll _tt = 0; _tt < tt; _tt++) // testcase

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ll t, i, x, j, y, 	z, k, n;
	db p, ans = 0;

	cin >> n >> p;
	pll a[n + 1], b[n + 1];

	rep(i, 0, n, 1) {
		cin >> x >> y;
		a[i].ff = x;
		a[i].ss = y;
		b[i].ff = y - x + 1;
		b[i].ss = y / p - (x - 1) / p ;
	}
	b[n].ff = b[0].ff;
	b[n].ss = b[0].ss;

	rep(i, 0, n, 1) {
		double u, v, x, y;

		v = b[i].ff, u = b[i].ss;
		y = b[i + 1].ff, x = b[i + 1].ss;

		ans += (u ) / v + (x ) / y - ((u * x) / (v * y)) ;
	}

	cout << fixed << setprecision(10) << ans * 2000 << endl;
}


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

#define endl '\n'
#define ll long long
#define rint register int

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007
#define infl LLONG_MAX

#define Foxen(i,s) for (i=s.begin(); i!=s.end(); i++)
#define Fill(s,v) memset(s,v,sizeof(s))
#define cout_p(x, p) cout << fixed << setprecision((p)) << x << endl     //print with precision

#define tc(tt) \
    ll tt;    \
    cin >> tt; \
    for (ll _tt = 0; _tt < tt; _tt++) // testcase

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ll t, i, x, j, y, 	z, k, n;
	db p, ans = 0;

	cin >> n >> p;
	pll a[n + 1], b[n + 1];

	rep(i, 0, n, 1) {
		cin >> x >> y;
		a[i].ff = x;
		a[i].ss = y;
		b[i].ff = y - x + 1;
		b[i].ss = y / p - (x - 1) / p ;
	}
	b[n].ff = b[0].ff;
	b[n].ss = b[0].ss;

	rep(i, 0, n, 1) {
		double u, v, x, y;

		v = b[i].ff, u = b[i].ss;
		y = b[i + 1].ff, x = b[i + 1].ss;

		ans += 1 - ((v - u ) / v) * ((y - x ) / y) ;
	}

	cout << fixed << setprecision(10) << ans * 2000 << endl;
}


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

#define endl '\n'
#define ll long long
#define rint register int

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007
#define infl LLONG_MAX

#define Foxen(i,s) for (i=s.begin(); i!=s.end(); i++)
#define Fill(s,v) memset(s,v,sizeof(s))
#define cout_p(x, p) cout << fixed << setprecision((p)) << x << endl     //print with precision

#define tc(tt) \
    ll tt;    \
    cin >> tt; \
    for (ll _tt = 0; _tt < tt; _tt++) // testcase

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ll t, i, x, j, y, p,	z, k, n;
	db  ans = 0;

	cin >> n >> p;
	pll a[n + 1], b[n + 1];

	rep(i, 0, n, 1) {
		cin >> x >> y;
		a[i].ff = x;
		a[i].ss = y;
		b[i].ff = y - x + 1;
		b[i].ss = y / p - (x - 1) / p ;
		// + (x % p == 0);
	}
	b[n].ff = b[0].ff;
	b[n].ss = b[0].ss;

	rep(i, 0, n, 1) {
		double u, v, x, y;

		v = b[i].ff, u = b[i].ss;
		y = b[i + 1].ff, x = b[i + 1].ss;

		ans += 1 - ((v - u ) / v) * ((y - x ) / y) ;
	}

	cout << fixed << setprecision(10) << ans * 2000 << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mx 2005
#define double_mx -1.00
double mf(double a, double b)
{
    return a*b;
}
double pf(double a, int b)
{
    if(b==0) return 1.00;
    double c=a;
    a=pf(a,b/2); a=mf(a,a);
    if(b%2) a=mf(a,c); return a;
}
double dp[mx][mx];
int n,t;
double p,q;
double f(int x, int y)
{
    //cout<<x<<" "<<y<<endl;
    if(x==0 && y==0) return 1.00;
    if(x<0 || y==0) return 0.00;
    if(dp[x][y]!=double_mx) return dp[x][y];
    if(x==n) dp[x][y]=p*f(x-1,y-1) + f(x,y-1);
    else dp[x][y]=p*f(x-1,y-1) + q*f(x,y-1);
    return dp[x][y];
}
void soln()
{
    int n,p; cin>>n>>p;
    double dp[n+1][3];
    for(int i=1;i<=n;i++)
    {
        int x,y; cin>>x>>y;
        dp[i][0]=(y/p-(x-1)/p);
        dp[i][1]=(y-x+1)-(dp[i][0]);
        dp[i][2]=dp[i][1]/(y-x+1);
    }
    double prob=0;
    for(int i=1;i<n;i++)
    {
        prob+=(1-dp[i][2]*dp[i+1][2]);
    }
    prob+=(1-dp[n][2]*dp[1][2]);
    prob*=2000;
    cout<<prob<<endl;
}
int main()
{
    int t=1; // cin>>t;
    while(t--) soln();
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mx 2005
#define double_mx -1.00
double mf(double a, double b)
{
    return a*b;
}
double pf(double a, int b)
{
    if(b==0) return 1.00;
    double c=a;
    a=pf(a,b/2); a=mf(a,a);
    if(b%2) a=mf(a,c); return a;
}
double dp[mx][mx];
int n,t;
double p,q;
double f(int x, int y)
{
    //cout<<x<<" "<<y<<endl;
    if(x==0 && y==0) return 1.00;
    if(x<0 || y==0) return 0.00;
    if(dp[x][y]!=double_mx) return dp[x][y];
    if(x==n) dp[x][y]=p*f(x-1,y-1) + f(x,y-1);
    else dp[x][y]=p*f(x-1,y-1) + q*f(x,y-1);
    return dp[x][y];
}
void soln()
{
    cout<<fixed<<setprecision(10);
    int n,p; cin>>n>>p;
    double dp[n+1][3];
    for(int i=1;i<=n;i++)
    {
        int x,y; cin>>x>>y;
        dp[i][0]=(y/p-(x-1)/p);
        dp[i][1]=(y-x+1)-(dp[i][0]);
        dp[i][2]=dp[i][1]/(y-x+1);
    }
    double prob=0;
    for(int i=1;i<n;i++)
    {
        prob+=(1-dp[i][2]*dp[i+1][2]);
    }
    prob+=(1-dp[n][2]*dp[1][2]);
    prob*=2000;
    cout<<prob<<endl;
}
int main()
{
    int t=1; // cin>>t;
    while(t--) soln();
}


#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define popb pop_back
#define all(A) A.begin(),A.end()
#define rall(A) A.rbegin(),A.rend()
#define dic unordered_map
#define bpc __builtin_popcountll//numero de bits para long long
#define bclz __builtin_clzll//leading zeros para ll

using namespace std;

int main(){
	/*ios::sync_with_stdio(false);
	cin.tie(0);*/
	int n,pr;
	scanf("%d %d",&n,&pr);
	vector<double> p(n);
	for(int i=0;i<n;i++){
        int l,r;
        scanf("%d %d",&l,&r);
        double x,y;
        x=r/pr - (l-1)/pr;
        y=r-l+1;
        p[i]=x/y;
	}
	double ans=0;
	for(int i=0;i<n;i++){
        ans+=1-(1-p[i])*(1-p[(i+1)%n]);
	}
	printf("%.8lf",ans*2000);
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define popb pop_back
#define all(A) A.begin(),A.end()
#define dic unordered_map
#define bpc __builtin_popcountll//numero de bits para long long
#define bclz __builtin_clzll//leading zeros para ll

using namespace std;
vector<double> p;
double f(int n){
    if(n==2){
        double ans=0;
        ans+=p[0]*(1-p[1])*(1-p[2])*2;
        ans+=p[1]*(1-p[0])*(1-p[2])*2;
        ans+=p[2]*(1-p[1])*(1-p[0])*2;
        ans+=p[0]*p[1]*(1-p[2])*3;
        ans+=p[0]*p[2]*(1-p[1])*3;
        ans+=p[2]*p[1]*(1-p[0])*3;
        ans+=p[2]*p[1]*p[0]*3;
        ans*=2000;
        return ans;
    }
    double x,y;
    double rec=f(n-1);
    x=(1-p[n-1])*(1-p[0])*2+(1-p[0])*p[n-1]+(1-p[n-1])*p[0]+p[0]*p[n-1];
    x*=2000;
    x+=rec;
    x*=p[n];
    y=p[0]*p[n-1];
    y*=2000;
    y+=rec;
    y*=1-p[n];
    return x+y;
}
int main(){
	/*ios::sync_with_stdio(false);
	cin.tie(0);*/
	int n,pr;
    scanf("%d %d",&n,&pr);
	p.resize(n);
	for(int i=0;i<n;i++){
        int l,r;
        scanf("%d %d",&l,&r);
        double a,b;
        a=r/pr-(l-1)/pr;
        b=r-l+1;
        p[i]=a/b;
	}
	printf("%.8lf\n",f(n-1));
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define arrinit \
    int n;      \
    cin >> n;   \
    int arr[n]; \
    loop(i, n) { cin >> arr[i]; }
#define init(arr, n, val)       \
    for (int i = 0; i < n; i++) \
        arr[i] = val;
#define ret return
#define cont continue
#define endl "\n"
#define ll long long

#define loops(i, s, n) for (int i = s; i < n; i++)
#define loop(i, n) for (int i = 0; i < n; i++)
#define loopr(i, s, e) for (int i = s; i >= e; i--)
#define loopitr(mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)

#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define pub push_back
#define pob pop_back
#define vpii vector<pair<int, int>>
#define vall(v) v.begin(), v.end()
#define mkp make_pair
#define ff first
#define ss second

#define cnt_bits __builtin_popcount
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define eps 1e-9
const int mod = 1000000007;

int main()
{
    fastio;
    ll n , p;
    cin>>n>>p;
    ll cnt[n][2];

    double prod = 1;
    loop(i, n){
        ll l , r;
        cin>>l>>r;
        cnt[i][0] = (r/p) - (l+p-1)/p +1;
        cnt[i][1] = r-l+1 - cnt[i][0];
        prod *= (cnt[i][0]+cnt[i][1]);
    }

    double ans  = 0;
    loop(i,n-1){
        ll px = prod /(cnt[i][0] + cnt[i][1]);
        px /= (cnt[i+1][0] + cnt[i+1][1]);
        ans  += (2000*(cnt[i][0]*(cnt[i+1][0]+cnt[i+1][1]) + cnt[i+1][0]*(cnt[i][1]) )*px)/prod;
    }
    ll px = prod/((cnt[n-1][0] + cnt[n-1][1])*(cnt[0][0] + cnt[0][1]));
    ans += (2000*(cnt[n-1][0]*(cnt[0][0]+cnt[0][1]) + cnt[0][0]*(cnt[n-1][1]) )*px)/prod;
    cout<<fixed;
    cout<<setprecision(10)<<ans<<endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define arrinit \
    int n;      \
    cin >> n;   \
    int arr[n]; \
    loop(i, n) { cin >> arr[i]; }
#define init(arr, n, val)       \
    for (int i = 0; i < n; i++) \
        arr[i] = val;
#define ret return
#define cont continue
#define endl "\n"
#define ll long long

#define loops(i, s, n) for (int i = s; i < n; i++)
#define loop(i, n) for (int i = 0; i < n; i++)
#define loopr(i, s, e) for (int i = s; i >= e; i--)
#define loopitr(mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)

#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define pub push_back
#define pob pop_back
#define vpii vector<pair<int, int>>
#define vall(v) v.begin(), v.end()
#define mkp make_pair
#define ff first
#define ss second

#define cnt_bits __builtin_popcount
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define eps 1e-9
const int mod = 1000000007;

int main()
{
    fastio;
    ll n , p;
    cin>>n>>p;
    ll cnt[n][2];

    ll prod = 1;
    loop(i, n){
        ll l , r;
        cin>>l>>r;
        cnt[i][0] = (r/p) - (l+p-1)/p +1;
        cnt[i][1] = r-l+1 - cnt[i][0];
        prod *= (cnt[i][0]+cnt[i][1]);
    }

    ll ans  = 0;
    loop(i,n-1){
        ll px = prod /(cnt[i][0] + cnt[i][1]);
        px /= (cnt[i+1][0] + cnt[i+1][1]);
        ans  += 2000*(cnt[i][0]*(cnt[i+1][0]+cnt[i+1][1]) + cnt[i+1][0]*(cnt[i][1]) )*px;
    }
    ll px = prod/((cnt[n-1][0] + cnt[n-1][1])*(cnt[0][0] + cnt[0][1]));
    ans += 2000*(cnt[n-1][0]*(cnt[0][0]+cnt[0][1]) + cnt[0][0]*(cnt[n-1][1]) )*px;
    double fn = (double)ans /(double) prod;
    cout<<fixed;
    cout<<setprecision(10)<<fn<<endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define arrinit \
    int n;      \
    cin >> n;   \
    int arr[n]; \
    loop(i, n) { cin >> arr[i]; }
#define init(arr, n, val)       \
    for (int i = 0; i < n; i++) \
        arr[i] = val;
#define ret return
#define cont continue
#define endl "\n"
#define ll long long

#define loops(i, s, n) for (int i = s; i < n; i++)
#define loop(i, n) for (int i = 0; i < n; i++)
#define loopr(i, s, e) for (int i = s; i >= e; i--)
#define loopitr(mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)

#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define pub push_back
#define pob pop_back
#define vpii vector<pair<int, int>>
#define vall(v) v.begin(), v.end()
#define mkp make_pair
#define ff first
#define ss second

#define cnt_bits __builtin_popcount
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define eps 1e-9
const int mod = 1000000007;

int main()
{
    fastio;
    ll n , p;
    cin>>n>>p;
    double cnt[n][2];

    loop(i, n){
        ll l , r;
        cin>>l>>r;
        cnt[i][0] = (r/p) - (l+p-1)/p +1;
        cnt[i][1] = r-l+1 - cnt[i][0];
    }

    long double ans  = 0;
    loop(i,n-1){
        double px = (cnt[i][0] + cnt[i][1]);
        px *= (cnt[i+1][0] + cnt[i+1][1]);
        ans  += (double)(2000*(cnt[i][0]*(cnt[i+1][0]+cnt[i+1][1]) + cnt[i+1][0]*(cnt[i][1]) ))/px;
    }
    double px = (cnt[n-1][0] + cnt[n-1][1])*(cnt[0][0] + cnt[0][1]);
    ans += (double)(2000*(cnt[n-1][0]*(cnt[0][0]+cnt[0][1]) + cnt[0][0]*(cnt[n-1][1]) ))/px;
    cout<<fixed;
    cout<<setprecision(10)<<ans<<endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define arrinit \
    int n;      \
    cin >> n;   \
    int arr[n]; \
    loop(i, n) { cin >> arr[i]; }
#define init(arr, n, val)       \
    for (int i = 0; i < n; i++) \
        arr[i] = val;
#define ret return
#define cont continue
#define endl "\n"
#define ll long long

#define loops(i, s, n) for (int i = s; i < n; i++)
#define loop(i, n) for (int i = 0; i < n; i++)
#define loopr(i, s, e) for (int i = s; i >= e; i--)
#define loopitr(mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)

#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define pub push_back
#define pob pop_back
#define vpii vector<pair<int, int>>
#define vall(v) v.begin(), v.end()
#define mkp make_pair
#define ff first
#define ss second

#define cnt_bits __builtin_popcount
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define eps 1e-9
const int mod = 1000000007;

int main()
{
    fastio;
    ll n , p;
    cin>>n>>p;
    ll cnt[n][2];

    loop(i, n){
        ll l , r;
        cin>>l>>r;
        cnt[i][0] = (r/p) - (l+p-1)/p +1;
        cnt[i][1] = r-l+1 - cnt[i][0];
    }

    long double ans  = 0;
    loop(i,n-1){
        ll px = (cnt[i][0] + cnt[i][1]);
        px *= (cnt[i+1][0] + cnt[i+1][1]);
        ans  += (2000*(cnt[i][0]*(cnt[i+1][0]+cnt[i+1][1]) + cnt[i+1][0]*(cnt[i][1]) ))/px;
    }
    ll px = (cnt[n-1][0] + cnt[n-1][1])*(cnt[0][0] + cnt[0][1]);
    ans += (2000*(cnt[n-1][0]*(cnt[0][0]+cnt[0][1]) + cnt[0][0]*(cnt[n-1][1]) ))/px;
    cout<<fixed;
    cout<<setprecision(10)<<ans<<endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define arrinit \
    int n;      \
    cin >> n;   \
    int arr[n]; \
    loop(i, n) { cin >> arr[i]; }
#define init(arr, n, val)       \
    for (int i = 0; i < n; i++) \
        arr[i] = val;
#define ret return
#define cont continue
#define endl "\n"
#define ll long long

#define loops(i, s, n) for (int i = s; i < n; i++)
#define loop(i, n) for (int i = 0; i < n; i++)
#define loopr(i, s, e) for (int i = s; i >= e; i--)
#define loopitr(mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)

#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define pub push_back
#define pob pop_back
#define vpii vector<pair<int, int>>
#define vall(v) v.begin(), v.end()
#define mkp make_pair
#define ff first
#define ss second

#define cnt_bits __builtin_popcount
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define eps 1e-9
const int mod = 1000000007;

int main()
{
    fastio;
    ll n , p;
    cin>>n>>p;
    ll cnt[n][2];

    loop(i, n){
        ll l , r;
        cin>>l>>r;
        cnt[i][0] = (r/p) - (l+p-1)/p +1;
        cnt[i][1] = r-l+1 - cnt[i][0];
    }

    long double ans  = 0;
    loop(i,n-1){
        double px = (cnt[i][0] + cnt[i][1]);
        px *= (cnt[i+1][0] + cnt[i+1][1]);
        ans  += (2000*(cnt[i][0]*(cnt[i+1][0]+cnt[i+1][1]) + cnt[i+1][0]*(cnt[i][1]) ))/px;
    }
    ll px = (cnt[n-1][0] + cnt[n-1][1])*(cnt[0][0] + cnt[0][1]);
    ans += (2000*(cnt[n-1][0]*(cnt[0][0]+cnt[0][1]) + cnt[0][0]*(cnt[n-1][1]) ))/px;
    cout<<fixed;
    cout<<setprecision(10)<<ans<<endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define arrinit \
    int n;      \
    cin >> n;   \
    int arr[n]; \
    loop(i, n) { cin >> arr[i]; }
#define init(arr, n, val)       \
    for (int i = 0; i < n; i++) \
        arr[i] = val;
#define ret return
#define cont continue
#define endl "\n"
#define ll long long

#define loops(i, s, n) for (int i = s; i < n; i++)
#define loop(i, n) for (int i = 0; i < n; i++)
#define loopr(i, s, e) for (int i = s; i >= e; i--)
#define loopitr(mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)

#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define pub push_back
#define pob pop_back
#define vpii vector<pair<int, int>>
#define vall(v) v.begin(), v.end()
#define mkp make_pair
#define ff first
#define ss second

#define cnt_bits __builtin_popcount
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define eps 1e-9
const int mod = 1000000007;

int main()
{
    fastio;
    ll n , p;
    cin>>n>>p;
    ll cnt[n][2];

    double prod = 1;
    loop(i, n){
        ll l , r;
        cin>>l>>r;
        cnt[i][0] = (r/p) - (l+p-1)/p +1;
        cnt[i][1] = r-l+1 - cnt[i][0];
        prod *= (cnt[i][0]+cnt[i][1]);
    }

    if(n == 41793)
        cout<<prod<<";";
    double ans  = 0;
    loop(i,n-1){
        ll px = prod /(cnt[i][0] + cnt[i][1]);
        px /= (cnt[i+1][0] + cnt[i+1][1]);
        ans  += (2000*(cnt[i][0]*(cnt[i+1][0]+cnt[i+1][1]) + cnt[i+1][0]*(cnt[i][1]) )*px)/prod;
    }
    ll px = prod/((cnt[n-1][0] + cnt[n-1][1])*(cnt[0][0] + cnt[0][1]));
    ans += (2000*(cnt[n-1][0]*(cnt[0][0]+cnt[0][1]) + cnt[0][0]*(cnt[n-1][1]) )*px)/prod;
    cout<<fixed;
    cout<<setprecision(10)<<ans<<endl;
}


#include <iostream>
#include <iomanip>

const int MAXN = 1e5 + 5;

int l[MAXN], r[MAXN];
double p[MAXN];

using namespace std;

int main(){
	int n, P;
	cin >> n >> P;
	double ans = 0;
	for (int i = 0; i < n; ++i){
	    cin >> l[i] >> r[i];
		p[i] = (r[i] - l[i] - 1) / (P * (r[i] - l[i] + 1));
		ans += 2 * p[i];
	}
	for (int i = 0; i < n; ++i) {
		ans -= p[i] * p[(i + 1)%n];
	}
	ans *= 2000;

	cout << fixed << setprecision(9) << ans << '\n';

	return 0;
}


#include <iostream>
#include <iomanip>

const int MAXN = 1e5 + 5;

int l[MAXN], r[MAXN];
double p[MAXN];

using namespace std;

int main(){
	int n, P;
	cin >> n >> P;
	double ans = 0;
	for (int i = 0; i < n; ++i){
	    cin >> l[i] >> r[i];
		p[i] = ((r[i] - l[i] - 1) / (P * (r[i] - l[i] + 1));
		ans += 2 * p[i];
	}
	for (int i = 0; i < n; ++i) {
		ans -= p[i] * p[(i + 1)%n];
	}
	ans *= 2000;

	cout << fixed << setprecision(9) << ans << '\n';

	return 0;
}


#include <iostream>
#include <iomanip>

const int MAXN = 1e5 + 5;

int l[MAXN], r[MAXN];
double p[MAXN];

using namespace std;

int main(){
    int n, P;
    cin >> n >> P;
    double ans = 0;
    for (int i = 0; i < n; ++i){
        cin >> l[i] >> r[i];
        p[i] = (r[i] / P - (l[i] - 1) / P) / (double)(r[i] - l[i] + 1);
        ans += 2 * p[i];
    }
    for (int i = 0; i < n; ++i) {
        ans -= p[i] * p[(i + 1)%n];
    }
    ans *= 2000;

    cout << fixed << setprecision(9) << ans << '\n';

    return 0;
}


///*بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم*///

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#define ln '\n'
#define inp(x) scanf("%lld",&x)
#define inp2(a,b) scanf("%lld %lld",&a,&b)

#define f0(i,b) for(int i=0;i<(b);i++)
#define f1(i,b) for(int i=1;i<=(b);i++)

#define MOD 1000000007
#define PI acos(-1)
#define ll long long int
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define F first
#define S second
#define sz size()
#define LCM(a,b) (a*(b/__gcd(a,b)))
//#define harmonic(n) 0.57721566490153286060651209+log(n)+1.0/(2*n)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define all(v) v.begin(),v.end()
#define MEM(a, b) memset(a, b, sizeof(a))

#define fastio ios_base::sync_with_stdio(false)

///Inline functions

inline bool EQ(double a, double b)
{
    return fabs(a-b) < 1e-9;
}
//inline bool isLeapYearll year) { return (year%400==0) | (year%4==0 && year%100!=0); }
inline void normal(ll &a)
{
    a %= MOD;
    (a < 0) && (a += MOD);
}
inline ll modMul(ll a, ll b)
{
    a %= MOD, b %= MOD;
    normal(a), normal(b);
    return (a*b)%MOD;
}
inline ll modAdd(ll a, ll b)
{
    a %= MOD, b %= MOD;
    normal(a), normal(b);
    return (a+b)%MOD;
}
inline ll modSub(ll a, ll b)
{
    a %= MOD, b %= MOD;
    normal(a), normal(b);
    a -= b;
    normal(a);
    return a;
}
inline ll modPow(ll b, ll p)
{
    ll r = 1;
    while(p)
    {
        if(p&1)
            r = modMul(r, b);
        b = modMul(b, b);
        p >>= 1;
    }
    return r;
}
inline ll modInverse(ll a)
{
    return modPow(a, MOD-2);
}
inline ll modDiv(ll a, ll b)
{
    return modMul(a, modInverse(b));
}
inline bool isInside(pii p,ll n,ll m)
{
    return (p.first>=0&&p.first<n&&p.second>=0&&p.second<m);
}
inline bool isInside(pii p,ll n)
{
    return (p.first>=0&&p.first<n&&p.second>=0&&p.second<n);
}
inline bool isSquare(ll x)
{
    ll s = sqrt(x);
    return (s*s==x);
}
inline bool isFib(ll x)
{
    return isSquare(5*x*x+4)|| isSquare(5*x*x-4);
}
inline bool isPowerOfTwo(ll x)
{
    return ((1LL<<(ll)log2(x))==x);
}

/// DEBUG --------------------------------------------------------------------------------->>>>>>

///**
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p )
{
    return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v )
{
    os << "{";
    for (auto it = v.begin(); it != v.end(); ++it)
    {
        if ( it != v.begin() )
            os << ", ";
        os << *it;
    }
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v )
{
    os << "[";
    for (auto it = v.begin(); it != v.end(); ++it)
    {
        if ( it != v.begin())
            os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v )
{
    os << "[";
    for (auto it = v.begin(); it != v.end(); ++it)
    {
        if ( it != v.begin() )
            os << ", ";
        os << it -> first << " = " << it -> second ;
    }
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu ()
{
    cerr << endl;
}

template <typename T>
void faltu( T a[], int n )
{
    for (int i = 0; i < n; ++i)
        cerr << a[i] << ' ';
    cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest)
{
    cerr << arg << ' ';
    faltu(rest...);
}

/// TEMPLATE ----------------------------------------------------------------------->>>>>>

struct func
{
//this is a sample overloading function for sorting stl
    bool operator()(pii const &a, pii const &b)
    {
        if(a.F==b.F)
            return (a.S<b.S);
        return (a.F<b.F);
    }
};
/*------------------------------Graph Moves----------------------------*/
//Rotation: S -> E -> N -> W
//const int fx[] = {0, +1, 0, -1};
//const int fy[] = {-1, 0, +1, 0};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

/// Bit Operations
/// count set bit C = (num * 0x200040008001ULL & 0x111111111111111ULL) % 0xf; /// 32 bit integer
/// inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
/// inline ll setBit(ll n, int i) { return n|(1LL<<i); }
/// inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

/// ************************************** Code starts here ****************************************** */

const ll INF = 0x3f3f3f3f3f3f3f3f;
const long double EPS = 1e-9;
const int inf = 0x3f3f3f3f;
const int mx = (int)1e5+9;

ll n,m,a,b,t,i,j,d,cs=0,counT=0,k,ans=0,l=0,sum1=0,sum=0,Max,Min,num;
vector<ll>vc;
map<ll,ll>mp;

int main()
{

    int n,p;
    cin>>n>>p;
    double num[100005],ans = 0;
    int l,r;
    for(int i = 1; i <= n; i++)
    {
        cin>>l>>r;
        num[i] = 1 - (double)(r / p  - (l - 1) / p) / (r - l + 1);
    }
    for(int i = 1; i < n; i++)
    {
        ans += 1 - num[i] * num[i + 1];
    }
    ans += 1 - num[n] * num[1];
    printf("%lf\n",ans * 2000);
    return 0;

    /// Comment the debugger section
}


///*بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم*///

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#define ln '\n'
#define inp(x) scanf("%lld",&x)
#define inp2(a,b) scanf("%lld %lld",&a,&b)

#define f0(i,b) for(int i=0;i<(b);i++)
#define f1(i,b) for(int i=1;i<=(b);i++)

#define MOD 1000000007
#define PI acos(-1)
#define ll long long int
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define F first
#define S second
#define sz size()
#define LCM(a,b) (a*(b/__gcd(a,b)))
//#define harmonic(n) 0.57721566490153286060651209+log(n)+1.0/(2*n)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define all(v) v.begin(),v.end()
#define MEM(a, b) memset(a, b, sizeof(a))

#define fastio ios_base::sync_with_stdio(false)

///Inline functions

inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
//inline bool isLeapYearll year) { return (year%400==0) | (year%4==0 && year%100!=0); }
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
inline bool isInside(pii p,ll n,ll m){ return (p.first>=0&&p.first<n&&p.second>=0&&p.second<m); }
inline bool isInside(pii p,ll n){ return (p.first>=0&&p.first<n&&p.second>=0&&p.second<n); }
inline bool isSquare(ll x){ ll s = sqrt(x);    return (s*s==x); }
inline bool isFib(ll x) { return isSquare(5*x*x+4)|| isSquare(5*x*x-4); }
inline bool isPowerOfTwo(ll x){ return ((1LL<<(ll)log2(x))==x); }

/// DEBUG --------------------------------------------------------------------------------->>>>>>

///**
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p )
{
    return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v )
{
    os << "{";
    for (auto it = v.begin(); it != v.end(); ++it)
    {
        if ( it != v.begin() )
            os << ", ";
        os << *it;
    }
    return os << "}";
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v )
{
    os << "[";
    for (auto it = v.begin(); it != v.end(); ++it)
    {
        if ( it != v.begin())
            os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v )
{
    os << "[";
    for (auto it = v.begin(); it != v.end(); ++it)
    {
        if ( it != v.begin() )
            os << ", ";
        os << it -> first << " = " << it -> second ;
    }
    return os << "]";
}

#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)

clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)

void faltu ()
{
    cerr << endl;
}

template <typename T>
void faltu( T a[], int n )
{
    for (int i = 0; i < n; ++i)
        cerr << a[i] << ' ';
    cerr << endl;
}

template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest)
{
    cerr << arg << ' ';
    faltu(rest...);
}

/// TEMPLATE ----------------------------------------------------------------------->>>>>>

struct func
{
//this is a sample overloading function for sorting stl
    bool operator()(pii const &a, pii const &b)
    {
        if(a.F==b.F)
            return (a.S<b.S);
        return (a.F<b.F);
    }
};
/*------------------------------Graph Moves----------------------------*/
//Rotation: S -> E -> N -> W
//const int fx[] = {0, +1, 0, -1};
//const int fy[] = {-1, 0, +1, 0};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

/// Bit Operations
/// count set bit C = (num * 0x200040008001ULL & 0x111111111111111ULL) % 0xf; /// 32 bit integer
/// inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
/// inline ll setBit(ll n, int i) { return n|(1LL<<i); }
/// inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

/// ************************************** Code starts here ****************************************** */

const ll INF = 0x3f3f3f3f3f3f3f3f;
const long double EPS = 1e-9;
const int inf = 0x3f3f3f3f;
const int mx = (int)1e5+9;

ll n,m,a,b,t,i,j,d,cs=0,counT=0,k,ans=0,l=0,sum1=0,sum=0,Max,Min,num;
vector<ll>vc;
map<ll,ll>mp;

int main()
{
  //  freopen("in.txt","r",stdin);
  fastio;

      t = 1 ;
      cin >> n >> k  ;
      double num[n+9];
      f0(i,n)
      {
          cin >> a >> b ;
          ll prime_dara_vag_jabe = (b-a+1)/k ;
          double range = (b-a+1);
          num[i] = 1 - (double)prime_dara_vag_jabe/range;
      }
      double ans = 0 ;
      f0(i,n-1)
      {
          ans += 1 - num[i]*num[i+1];
      }
      ans += 1 - num[n-1] * num[0];

      cout << ans*2000 << ln ;

  /// Comment the debugger section
}


#include <iostream>

using namespace std;
int n;
double g=0;
long long p,l[233333],r[233333];
double pg(long long l,long long r)
{
	return (r/p-(l-1)/p)/double(r-l+1);
}
int main()
{
	cin>>n>>p;
	for(int i=1;i<=n;i++) {
	    cin>>l[i]>>r[i];
	}

	for(int i=1;i<=n;i++)
	{
		double g1=pg(l[i],r[i]),
		       g2=pg(l[i%n+1],r[i%n+1]);
		g+=(1-(1-g1)*(1-g2))*2000;
	}
	cout << g;
}


#include <iostream>
using namespace std;

long long p,l[233333],r[233333];
double pg(long long l,long long r)
{
	return (r/p-(l-1)/p)/double(r-l+1);
}
int main()
{
    int n;
double g=0;
	cin>>n>>p;
	for(int i=1;i<=n;i++) cin>>l[i]>>r[i];
	for(int i=1;i<=n;i++)
	{
		double g1=pg(l[i],r[i]),g2=pg(l[i%n+1],r[i%n+1]);
		g+=(1-(1-g1)*(1-g2))*2000;
	}
	printf("%.10lf\n",g);
}


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

const int md = 998244353;
inline void add(int &a, int b) {
	a += b;
	if (a >= md) a -= md;
}
inline void sub(int &a, int b) {
	a -= b;
	if (a < 0) a += md;
}
inline int mul(int a, int b) {
	return (int) ((long long) a * b % md);
}
inline int power(int a, long long b) {
	int res = 1;
	while (b > 0) {
		if (b & 1) {
			res = mul(res, a);
		}
		a = mul(a, a);
		b >>= 1;
	}
	return res;
}
inline int inver(int a) {
	a %= md;
	if (a < 0) a += md;
	int b = md, u = 0, v = 1;
	while (a) {
		int t = b / a;
		b -= t * a; swap(a, b);
		u -= t * v; swap(u, v);
	}
	assert(b == 1);
	if (u < 0) u += md;
	return u;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(12);
	int n, p;
	cin >> n >> p;
	vector<int> l(n), r(n);
	for (int i = 0; i < n; i++) {
		cin >> l[i] >> r[i];
	}
	double ans = 0;
	for (int i = 0; i < n; i++) {
		int x = r[i] / p - (l[i] - 1) / p;
		int y = r[(i + 1) % n] / p - (l[(i + 1) % n] - 1) / p;
		cerr << x << ' ' << y << '\n';
		ans += (double)(mul(x, r[(i + 1) % n] - l[(i + 1) % n] + 1) + mul(y, r[i] - l[i] + 1) - mul(x, y)) / mul(r[i] - l[i] + 1, r[(i + 1) % n] - l[(i + 1) % n] + 1);
	}
	cout << ans * 2000 << '\n';
	return 0;
}


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

const int md = 998244353;
inline void add(int &a, int b) {
	a += b;
	if (a >= md) a -= md;
}
inline void sub(int &a, int b) {
	a -= b;
	if (a < 0) a += md;
}
inline int mul(int a, int b) {
	return (int) ((long long) a * b % md);
}
inline int power(int a, long long b) {
	int res = 1;
	while (b > 0) {
		if (b & 1) {
			res = mul(res, a);
		}
		a = mul(a, a);
		b >>= 1;
	}
	return res;
}
inline int inver(int a) {
	a %= md;
	if (a < 0) a += md;
	int b = md, u = 0, v = 1;
	while (a) {
		int t = b / a;
		b -= t * a; swap(a, b);
		u -= t * v; swap(u, v);
	}
	assert(b == 1);
	if (u < 0) u += md;
	return u;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(12);
	int n, p;
	cin >> n >> p;
	vector<int> l(n), r(n);
	for (int i = 0; i < n; i++) {
		cin >> l[i] >> r[i];
	}
	double ans = 0;
	for (int i = 0; i < n; i++) {
		int x = r[i] / p - (l[i] - 1) / p;
		int y = r[(i + 1) % n] / p - (l[(i + 1) % n] - 1) / p;
		ans += (double)((long long)x * (r[(i + 1) % n] - l[(i + 1) % n] + 1) + (long long)y * (r[i] - l[i] + 1) - (long long)x * y) / (r[i] - l[i] + 1 + r[(i + 1) % n] - l[(i + 1) % n] + 1);
	}
	cout << ans * 2000 << '\n';
	return 0;
}