#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 intersectLL(const L& l, const L& m){
    return ( abs(cross(l[1]-l[0], m[1]-m[0])) > EPS ) ||
        ( abs(cross(l[1]-l[0], m[0]-l[0])) < EPS );
}
bool intersectLS(const L& l, const L& s){
    return cross(l[1]-l[0], s[0]-l[0])*
        cross(l[1]-l[0], s[1]-l[0]) < EPS;
}
bool intersectLP(const L& l, const P& p){
    return abs(cross(l[1]-p, l[0]-p)) < EPS;
}
*/
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 distanceLL(const L &l, const L &m) {
    return intersectLL(l, m) ? 0 : distanceLP(l, m[0]);
}
double distanceLS(const L &l, const L &s) {
    if (intersectLS(l, s)) return 0;
    return min( distanceLP(l, s[0]), distanceLP(l, s[1]) );
}
*/
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;
struct info{
    int c; //?????????
    int r; //????±?(?????¢????????????)
    int id; //id;
    P bef;
    info(int c, int r, int id):c(c), r(r), id(id){}
    info(){}
};
namespace std{
    bool operator < (const info &a, const info &b){ //priority_queue??§??????????°??????????
        return b.c < a.c;
    }
}

int main(){
    cerr << fixed << setprecision(10);
    // 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]));
       
    }
   
    //???????????????
    //v[????±?][?????????] = ??§?¨?
    //0???2+n*4 :?§??????????
    //2+n*4???2+n*4+numvrot[r] :?????????????±?????????¢??????
    //????????\??? :1?????????????±?????????¢??????
    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;
    }
   
    //?§??????????????????????????????¢??????
    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])){
                //?´°???????????¢
                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;
                }
            }
        }
    }

    //?????¢??????????????????????????¢??????
    //????????§??????????????????????????¨??¨????????§?¨????
    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};
            //???????????¢???90??????????????????????????????????????§????????°???????????¢??????????????????
            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]));
                }
            }
        }

        vector<P> vrot; //tmp
        //i,j???????????????????????????
        for(int i=0; i<n; i++){
            for(int j=i+1; j<n; j++){
                //?????¢???????????????????????????????????¢??????
                //????§???¢????£?
                for(int x=0; x<(int)w[i].size(); x++){
                    //if(abs(w[i][x][1]-w[i][x][0]) < 3*weps) continue;
                    for(int y=0; y<(int)w[j].size(); y++){
                        //if(abs(w[j][y][1]-w[j][y][0]) < 3*weps) continue;
                        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]));
                        }
                    }
                }
                //?????¢??¨????§???¢
                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]);
                            }
                        }
                    }
                }

                //?????¢????£?
                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]);
                        }
                    }
                }
            }
        }

        //????????????????????????????????????
        //vlist??¨vrot?????????????????£???????????§?????°????????????????¢????
        sort(vrot.begin(), vrot.end());
        vrot.erase(unique(vrot.begin(), vrot.end()), vrot.end());
        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++){
                //?????????????????????????????¢?????????????????????????????????
                //????§???¢
                for(int j=0; j<(int)w[i].size()/4; j++){
                    //if(e<2+4*n)?????¨??????rot???????§???¢(j=0)??????
                    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;
                        //cerr << i << " " << j << " poly  ";;
                        break;
                    }
                }
                
                //?????¢
                if(e>=2+4*n && enable){ //??\?????§ok?????????????????´???
                    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;
                            //double angle = getangle(P(1,0), vec);
                            //if(rotangle[rot]+0 < angle && angle < rotangle[rot+1]-0){
                            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;
                                //cerr << i << " " << j << " arc  ";
                                break;
                            }
                        }
                    }
                }
                if(!enable){
                    break;
                }
            }
            
            if(enable){
                if(e<2+4*n){
                    disabled[rot][e] = false;
                }else{
                    //enable == true????????¢??§???????????????vlist[rot]?????????
                    vlist[rot].push_back(focus);
                    numvrot[rot]++;
                    //cerr << "ACCEPTED!!!  ";
                    //cout << focus << endl;
                }
            }
            /*
            if(e>=2+4*n){
                cerr << focus << endl;
            }
            */
        }
    }

    //cerr << vlist[3][2] << " " << vlist[3][3] << " " << vlist[3][4] << " " << vlist[3][5] << endl;

    //vlist[rot+1] ??? vlist[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[????±?][??§?¨??????????????????\?????§???][???????????§???????????????] = ??????(info);
    vector<vector<vector<info> > > adj(r);
    for(int rot=0; rot<r; rot++){
        adj[rot].resize(vlist[rot].size());
    }

    //??°?????????????????????
    //???????±??????§????????\??§?????????
    //?????¢??§?¬????????±????????????????(???????????????????????¨?????¨?????????????????????)
    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;
                //???????????????????????????????????????(push_back??????)
                //????????????
                //????????§???????????????????????§??????????????§?????????
                L move(vlist[rot][i], vlist[rot][j]);
                P mid = (vlist[rot][i]+vlist[rot][j])*0.5;
                bool judge = true; //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));
        }
    }

    /*
    for(int i=0; i<(int)adj[4][1].size(); i++){
        info tmp = adj[4][1][i];
        cerr << vlist[tmp.r][tmp.id] << " " << tmp.id << " " << tmp.r << endl;
    }
    */

    /*
    for(int i=0; i<(int)vlist[0].size(); i++){
        if(i<2+4*n && disabled[0][i]){
            cerr << "disabled :";
        }
        cerr << vlist[0][i] << endl;
    }
    */
    

    
    int ans = -1;
    //????????????????????§?????????????????????
    priority_queue<info> wait;
    vector<vector<int> > mincost(r); //?????????????????????????????£??????max?????°??¨??¨inf??§?????????
    for(int rot=0; rot<r; rot++){
        int num = adj[rot].size();
        mincost[rot].resize(num);
        for(int i=0; i<num; i++){
            mincost[rot][i] = 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;
        //cerr << cost << " " << rot << " " << p << endl;
        //cerr << vlist[rot][p] << " before is :" << wait.top().bef << endl;
        wait.pop();
        if(cost > mincost[rot][p]) continue;
        if(p == 1){ //??´????????????
            ans = cost;
            break;
        }
        //if(cost > 2) break; // debug
        for(int i=0; i<(int)adj[rot][p].size(); i++){
            info next = adj[rot][p][i];
            //cerr << "next ";
            //cerr << next.c << " " << next.r << " " << next.id << endl;
            if(cost+next.c < mincost[next.r][next.id]){
                mincost[next.r][next.id] = cost+next.c;
                next.c += cost;
                next.bef = vlist[rot][p];
                wait.push(next);
            }
        }
    }
    cout << ans << endl;

    /*
    P focus = P(-175.9999990, -39.0000010);
    P wa = P(75, -39);
    P vec = focus-wa; //?£????????????????????????§
    if(abs(vec)+EPS <l+weps){
        if(vec.Y < 0) vec = -vec;
        double angle = getangle(P(1,0), vec);
        cerr << angle << " " << rotangle[0] << " " << rotangle[1] << endl;
        //if(rotangle[0]+0 < angle && angle < rotangle[1]-0){
        if(ccw(wa, wa+rotate[0], focus)*ccw(wa, wa+rotate[1], focus) == -1){
            cerr << "disabled" << endl;
        }
    }
    */
    
    return 0;
}

Measured memory_bytes: 4872