#include <iostream>
#include <iomanip>
#include <complex>
#include <vector>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

const double EPS = 1e-10;
const double INF = 1e12;
#define LE(n,m) ((n) < (m) + EPS)
#define GE(n,m) ((n) + EPS > (m))
#define EQ(n,m) (abs((n)-(m)) < EPS)
#define X real()
#define Y imag()

typedef complex<double> P;
typedef vector<P> VP;
struct L : VP{
    L(const P& a, const P& b){ resize(2); at(0)=a; at(1)=b; }
    L(){ resize(2); }
};
struct C{
    P p;
    double r;
    C(const P& p, const double& r) : p(p), r(r) {}
};
namespace std{
    bool operator < (const P& a, const P& b){
        return (a.X!=b.X) ? a.X<b.X : a.Y<b.Y;
    }
}

double dot(P a, P b){
    return (conj(a)*b).X;
}
double cross(P a, P b){
    return (conj(a)*b).Y;
}

int ccw(P a, P b, P c){
    b -= a;
    c -= a;
    if(cross(b,c) > EPS) return +1; //counter clockwise
    if(cross(b,c) <-EPS) return -1; //clockwise
    if(dot(b,c) < EPS) return +2; //c-a-b
    if(norm(b) < norm(c)) return -2; //a-b-c
    return 0; //a-c-b
}

bool intersectSS(const L& s, const L& t){
    return ( ccw(s[0],s[1],t[0]) *ccw(s[0],s[1],t[1]) <= 0 ) &&
        ( ccw(t[0],t[1],s[0]) *ccw(t[0],t[1],s[1]) <= 0 );
}
bool strictItsSS(const L& s, const L& t){
    return ( ccw(s[0],s[1],t[0]) *ccw(s[0],s[1],t[1]) == -1 ) &&
        ( ccw(t[0],t[1],s[0]) *ccw(t[0],t[1],s[1]) == -1);
}
bool intersectSP(const L& s, const P& p){
    return abs(s[0]-p) +abs(s[1]-p) -abs(s[1]-s[0]) < EPS;
}
bool strictItsSP(const L& l, const P &p){
    return intersectSP(l,p) && !EQ(l[0],p) && !EQ(l[1],p);
}

P projection(const L& l, const P& p) {
    double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
    return l[0] + t*(l[0]-l[1]);
}

double distanceLP(const L &l, const P &p) {
    return abs(p - projection(l, p));
}

double distanceSP(const L &s, const P &p) {
    const P r = projection(s, p);
    if (intersectSP(s, r)) return abs(r - p);
    return min(abs(s[0]-p), abs(s[1]-p));
}
double distanceSS(const L &s, const L &t) {
    if (intersectSS(s, t)) return 0;
    return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])),
               min(distanceSP(t, s[0]), distanceSP(t, s[1])));
}

P crosspointLL(const L &l, const L &m) {
    double A = cross(l[1]-l[0], m[1]-m[0]);
    double B = cross(l[1]-l[0], l[1]-m[0]);
    return m[0] + B /A *(m[1]-m[0]);
}

VP crosspointLC(const L &l, const C &c){
    VP ret;
    P mid = projection(l, c.p);
    double dlp = distanceLP(l, c.p);
    if(EQ(dlp, c.r)){
        ret.push_back(mid);
        return ret;
    }
    if(dlp > c.r){
        return ret;
    }
    P d = sqrt(c.r*c.r -dlp*dlp)/abs(l[1]-l[0]) *(l[1]-l[0]);
    ret.push_back(mid+d);
    ret.push_back(mid-d);
    return ret;
}
VP crosspointSC(const L &s, const C &c){
    VP ret;
    VP cp = crosspointLC(s,c);
    for(int i=0; i<(int)cp.size(); i++){
        if(dot(s[1]-s[0], cp[i]-s[0]) < EPS) continue;
        if(norm(s[1]-s[0]) < norm(cp[i]-s[0]) +EPS) continue;
        ret.push_back(cp[i]);
    }
    return ret;
}
VP crosspointCC(C a, C b){
    VP ps;
    if(a.r < b.r) swap(a, b);
    P ab = b.p-a.p;
    double dist = abs(ab);
    if(GE(dist, a.r +b.r) || LE(dist, abs(a.r-b.r))){
        return ps;
    }
    if(EQ(dist, a.r+b.r) || EQ(dist, a.r-b.r)){
        ps.push_back( a.p +ab*(a.r/dist) );
        return ps;
    }
    double l = ((a.r*a.r -b.r*b.r)/dist +dist) /2;
    double n = sqrt(a.r*a.r -l*l);
    P mid = a.p + (ab)*(l/dist);
    P d = ab *(n/dist) *P(0,1);
    ps.push_back(mid+d);
    ps.push_back(mid-d);
    return ps;
}

double getangle(P p, P a, P b){
    a -= p;
    b -= p;
    return acos(dot(a,b)/(abs(a)*abs(b)));
}
double getangle(P a, P b){
    return acos(dot(a,b)/(abs(a)*abs(b)));
}

VP crosspointSDA(const L& l, const C& c, double as, double ae){
    VP cp = crosspointSC(l, c);
    VP ret;
    for(int i=0; i<(int)cp.size(); i++){
        P cpvec = cp[i] - c.p;
        if(cpvec.Y <0) cpvec = -cpvec;
        double angle = getangle(P(1,0), cpvec);
        if(as <= angle && angle <= ae){
            ret.push_back(cp[i]);
        }
    }
    return ret;
}
VP crosspointDADA(const C &a, const C& b, double as, double ae){
    VP cp = crosspointCC(a,b);
    VP ret;
    for(int i=0; i<(int)cp.size(); i++){
        P cen[2] = {a.p, b.p};
        double angle[2];
        for(int j=0; j<2; j++){
            P cpv = cp[i] -cen[j];
            if(cpv.Y <0) cpv = -cpv;
            angle[j] = getangle(P(1,0), cpv);
        }
        if(as <= angle[0] && angle[0] <= ae && as <= angle[1] && angle[1] <= ae){
            ret.push_back(cp[i]);
        }
    }
    return ret;
}
bool isParallel(const P &a, const P &b){
    return abs(cross(a,b)) < EPS;
}
bool isParallel(const L &a, const L &b){
    return isParallel(a[1]-a[0], b[1]-b[0]);
}

const double weps =1e-6;
const double exdir[4] = {1, -1, -1, 1};
const int inf = 1e9;
//for dijkstra
struct info{
    int c;
    int r;
    int id;
    info(int c, int r, int id):c(c), r(r), id(id){}
    info(){}
};
namespace std{
    bool operator < (const info &a, const info &b){
        return b.c < a.c;
    }
}

int main(){
    // input
    double l;
    int r,n;
    P s,g;
    double tmpx, tmpy;
    cin >> l >> r;
    cin >> tmpx >> tmpy;
    s = P(tmpx, tmpy);
    cin >> tmpx >> tmpy;
    g = P(tmpx, tmpy);
    cin >> n;
    vector<L> wall(n);
    for(int i=0; i<n; i++){
        for(int j=0; j<2; j++){
            cin >> tmpx >> tmpy;
            wall[i][j] = P(tmpx, tmpy);
        }
    }
    vector<P> rotate(r+1);
    vector<double> rotangle(r+1);
    for(int i=0; i<=r; i++){
        rotangle[i] = M_PI/r *i;
        rotate[i] = P(cos(rotangle[i]), sin(rotangle[i]));
       
    }

    //vlist[rot][id] = (coordinate of candidate to move)
    vector<vector<P> > vlist(r, vector<P>(2+n*4));
    for(int rot=0; rot<r; rot++){
        vlist[rot][0] = s;
        vlist[rot][1] = g;
    }
   
    //candidate of point to move (vertices of extended parallelogram)
    for(int rot=0; rot<r; rot++){
        for(int i=0; i<n; i++){
            P vwall = wall[i][1] -wall[i][0];
            if(isParallel(vwall, rotate[rot])){
                //thin rectangle
                P ext = weps*vwall/abs(vwall)*P(0,1);
                for(int j=0; j<4; j++){
                    P wext = l*(wall[i][j/2]-wall[i][1-j/2])/abs(vwall);
                    vlist[rot][2+i*4 +j] = wall[i][j/2] +wext +exdir[j]*ext;
                }
            }else{
                P ext = (l+weps)*rotate[rot];
                if(cross(vwall, ext) < 0) ext = -ext;
                for(int j=0; j<4; j++){
                    P wext = weps*(wall[i][j/2]-wall[i][1-j/2])/abs(vwall);
                    vlist[rot][2+i*4 +j] = wall[i][j/2] +wext +exdir[j]*ext;
                }
            }
        }
    }

    //list up candidate of point to rotate
    //and disable invalid point
    vector<int> numvrot(r, 0);
    vector<vector<bool> > disabled(r, vector<bool>(2+n*4, true));
    for(int rot=0; rot<r; rot++){
        vector<vector<L> > w(n);
        for(int i=0; i<n; i++){
            w.reserve(12);
            for(int j=0; j<4; j++){
                w[i].push_back( L(vlist[rot][2+4*i +j], vlist[rot][2+4*i +(j+1)%4]) );
            }
            for(int j=0; j<4; j++){
                w[i].push_back( L(vlist[(rot+1)%r][2+4*i +j], vlist[(rot+1)%r][2+4*i +(j+1)%4]) );
            }
            P vwall = wall[i][1] -wall[i][0];
            double angle[2] = {getangle(vwall,rotate[rot]) -0.5*M_PI,
                               getangle(vwall,rotate[rot+1]) -0.5*M_PI};
            if(angle[0]*angle[1] < 0){
                vector<P> vrect(4);
                P ext = vwall *P(0,1) *((l+weps)/abs(vwall));
                for(int j=0; j<4; j++){
                    vrect[j] = wall[i][j/2] +exdir[j]*ext;
                }
                for(int j=0; j<4; j++){
                    w[i].push_back(L(vrect[j], vrect[(j+1)%4]));
                }
            }
        }

        //candidate
        vector<P> vrot; //tmp
        for(int i=0; i<n; i++){
            for(int j=i+1; j<n; j++){
                //poly--poly
                for(int x=0; x<(int)w[i].size(); x++){
                    for(int y=0; y<(int)w[j].size(); y++){
                        if(!isParallel(w[i][x],w[j][y]) && intersectSS(w[i][x],w[j][y])){
                            vrot.push_back(crosspointLL(w[i][x], w[j][y]));
                        }
                    }
                }
                //poly--arc
                for(int z=0; z<2; z++){
                    swap(i,j);
                    for(int x=0; x<(int)w[i].size(); x++){
                        for(int y=0; y<2; y++){
                            VP cp = crosspointSDA(w[i][x], C(wall[j][y], l+weps),
                                                  rotangle[rot], rotangle[rot+1]);
                            for(int k=0; k<(int)cp.size(); k++){
                                vrot.push_back(cp[k]);
                            }
                        }
                    }
                }
                //arc-arc
                for(int x=0; x<2; x++){
                    for(int y=0; y<2; y++){
                        VP cp = crosspointDADA(C(wall[i][x], l+weps), C(wall[j][y], l+weps),
                                               rotangle[rot], rotangle[rot+1]);
                        for(int k=0; k<(int)cp.size(); k++){
                            vrot.push_back(cp[k]);
                        }
                    }
                }
            }
        }
        
        sort(vrot.begin(), vrot.end());
        vrot.erase(unique(vrot.begin(), vrot.end()), vrot.end());
        
        //disable invalid point
        for(int e=0; e <2+4*n +(int)vrot.size(); e++){
            P focus = (e<2+4*n)? vlist[rot][e] :vrot[e-(2+4*n)];
            bool enable = true;
            for(int i=0; i<n; i++){
                //in poly
                for(int j=0; j<(int)w[i].size()/4; j++){
                    if(e<2+4*n && j>0) break;
                    bool judge = true;
                    for(int k=0; k<4; k++){
                        if(ccw(w[i][j*4+k][0], w[i][j*4+k][1], focus) != +1){
                            judge = false;
                        }
                    }
                    if(judge){
                        enable = false;
                        break;
                    }
                }
                
                //in sector
                if(e>=2+4*n && enable){
                    for(int j=0; j<2; j++){
                        P vec = focus-wall[i][j];
                        if(abs(vec)+EPS <l+weps){
                            if(vec.Y < 0) vec = -vec;
                            if(ccw(wall[i][j], wall[i][j]+rotate[rot], focus)
                               *ccw(wall[i][j], wall[i][j]+rotate[rot+1], focus) == -1){
                                enable = false;
                                break;
                            }
                        }
                    }
                }
                if(!enable){
                    break;
                }
            }
            
            if(enable){
                if(e<2+4*n){
                    disabled[rot][e] = false;
                }else{
                    vlist[rot].push_back(focus);
                    numvrot[rot]++;
                }
            }
        }
    }

    for(int rot=0; rot<r; rot++){
        for(int i=0; i<numvrot[rot]; i++){
            vlist[(rot+1)%r].push_back(vlist[rot][2+n*4 +i]);
        }
    }

    //adj[rot][id][i] = next_i(info);
    vector<vector<vector<info> > > adj(r);
    for(int rot=0; rot<r; rot++){
        adj[rot].resize(vlist[rot].size());
    }

    //make visible graph
    for(int rot=0; rot<r; rot++){
        for(int i=0; i<(int)vlist[rot].size(); i++){
            if(i<2+4*n && disabled[rot][i]) continue;
            for(int j=i+1; j<(int)vlist[rot].size(); j++){
                if(j<2+4*n && disabled[rot][j]) continue;
                
                L move(vlist[rot][i], vlist[rot][j]);
                P mid = (vlist[rot][i]+vlist[rot][j])*0.5;
                bool judge = true;
                for(int k=0; k<n; k++){
                    judge = false;
                    for(int x=0; x<4; x++){
                        if(strictItsSS(move, L(vlist[rot][2+4*k+x], vlist[rot][2+4*k+(x+1)%4]))){
                            judge = false;
                            break;
                        }
                        if(ccw(vlist[rot][2+4*k+x], vlist[rot][2+4*k+(x+1)%4], mid) != +1){
                            judge = true;
                            continue;
                        }
                    }
                    if(!judge) break;
                }

                if(judge){
                    adj[rot][i].push_back(info(0, rot, j));
                    adj[rot][j].push_back(info(0, rot, i));
                }
            }
        }
        for(int i=0; i<numvrot[rot]; i++){
            adj[rot][2+4*n+i].push_back(info(1, (rot+1)%r, 2+4*n+numvrot[(rot+1)%r]+i));
        }
    }

    //Dijkstra
    int ans = -1;
    priority_queue<info> wait;
    int maxnode =0;
    for(int rot=0; rot<r; rot++) maxnode = max(maxnode, (int)adj[rot].size());
    vector<vector<int> > mincost(r, vector<int>(maxnode, inf));
    wait.push(info(0, 0, 0));
    mincost[0][0] = 0;
    while(!wait.empty()){
        int cost = wait.top().c;
        int rot = wait.top().r;
        int p = wait.top().id;
        wait.pop();
        if(cost > mincost[rot][p]) continue;
        if(p == 1){
            ans = cost;
            break;
        }
        for(int i=0; i<(int)adj[rot][p].size(); i++){
            info next = adj[rot][p][i];
            if(cost+next.c < mincost[next.r][next.id]){
                mincost[next.r][next.id] = cost+next.c;
                next.c += cost;
                wait.push(next);
            }
        }
    }
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 4176