/*

weak        weak  we      ak   we  akwea        weak  we
  weak    weak    we      ak   weak    weak    we  ak we
    weakweak      we      ak   wea       ak   we    akwe
      wea         we      ak   we        ak   we    akwe
      wea         we      ak   we        ak   we    akwe
      wea          eak  weak   we        ak    we  ak we
      wea            wea  ak   we        ak     weak  we
                                                      we
we      ak     wea  ak       weak                     we
 we    ak    wea  weak     wea  eak                   we
  we  ak    we      ak   wea      wea         we      we
   weak     we      ak   we        we         we      we
    we      we      ak   we        we         we      we
   we        wea  weak    wea    wea          weak  weak
weak           wea  akw      weak                weak


*/
using namespace std;
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <utility>
#include <bitset>
#include <set>
#include <string>
#include <stack>
#include <iomanip>
#include <map>
#include <memory.h>
#include <deque>

#define pb push_back
#define pii pair<int,int>
#define F first
#define S second
#define LL long long
#define mid (LB+RB)/2
#define vvl vector <vector<LL>>
#define vl vector <LL>
#define mkp make_pair

//iterators
#define iter(x) x.begin(),x.end()
#define aiter(a,n) a,a+n

//loops
#define REP(n) for (int ___=n;___--;)
#define REP0(i,n) for (int i=0;i<n;++i)
#define REP1(i,n) for (int i=1;i<=n;++i)
#define FOR(i,b,l,k) for (int i=b;i!=l;i+=k)
#define forEach(i,v) for (auto i:v)

/*
yungyao so weeeeeeeeeeeeeeeeeeeeeeeeeeak
8e7 so dian
FHVirus so dian
youou so dian
KYW so dian
hubert so dian
jass so dian
tingyu so dian
panda so dian
*/

//IO
#include <iostream>
#define theyRSOOOOOOOOODIAN ios_base::sync_with_stdio(false),cin.tie(0);
//testing some stuff, still under construction
//a lot more useful while debug
inline void print(vector <int> v){
    for (auto i:v)
        cout << i << ' ';
    cout << '\n';
}
inline void print(vector <int> v,char sep,char end){
    for (auto i:v)
        cout << i << sep;
    cout << end;
}

//constants
#include <climits>
const int maxn = 2020,mod = 0;

//workspace

inline void solve(){
    int n;
    pair<double,double> seg[maxn]{};

    cin >> n;
    REP1(i,n){
        int t;

        cin >> t >> seg[i].F >> seg[i].S;
        if (t == 2) seg[i].S -= 0.1;
        else if (t == 3) seg[i].F += 0.1;
        else if (t == 4) seg[i].S -= 0.1,seg[i].F += 0.1;
    }

    int cnt = 0;
    REP1(i,n){
        for (int j=i+1;j<=n;++j){
            auto [l1,r1] = seg[i]; auto [l2,r2] = seg[j];

            if (l1 <= r2 && l2 <= r1) ++cnt;
        }
    }
    cout << cnt;
}

signed main(){
    theyRSOOOOOOOOODIAN
    //for (int ;cin;)//use in multi-testcases and end in EOF problems
    //int t,i=1;for (cin >> t;i<=t;++i)//use in multi-testcases problems
    //cout << "Case #" << i << ": ",//use in Google, FB competitions
    solve();//always used
    return 0;
}
#include <bits/stdc++.h>
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//#include <atcoder/all>
using namespace std;
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a; i > -1; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a; i > b; --i)
#define repl(i, a) for (ll i = (ll)0; i < (ll)a; ++i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define fi first
#define se second
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll mod_998244353 = 998244353;
constexpr ll INF = 1LL << 60;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

// using lll=boost::multiprecision::cpp_int;
// using
// Double=boost::multiprecision::number<boost::multiprecision::cpp_dec_float<128>>;//仮数部が1024桁
template <class T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}

ll mypow(ll x, ll n, const ll &p = -1)
{ // x^nをmodで割った余り

  if (p != -1)
  {
    x = (x % p + p) % p;
  }
  ll ret = 1;
  while (n > 0)
  {
    if (n & 1)
    {
      if (p != -1)
        ret = (ret * x) % p;
      else
        ret *= x;
    }
    if (p != -1)
      x = (x * x) % p;
    else
      x *= x;
    n >>= 1;
  }
  return ret;
}

struct myrand{
  random_device seed;
  mt19937 mt;
  myrand():mt(seed()){}
  int operator()(int a,int b){//[a,b)
    uniform_int_distribution<int>dist(a,b-1);
    return dist(mt);
  }
};

// using namespace atcoder;

//------------------------
//------------------------
//------------------------
//------------------------
//------------------------

template<typename T>
struct edge{
  int to;
  T c,d;
  edge(int to,T c,T d):to(to),c(c),d(d){}
};


template<typename T>
vector<T> dijkstra(vector<vector<edge<T>>> &g,int s){//sは始点
  const auto INFTY=numeric_limits<T>::max();//2回以上使うときはオーバーフローに気をつける(INFTYのときはif文で弾くなど)
  vector<T>dist(g.size(),INFTY);
  using P=pair<T,int>;//firstは最短距離,secondは頂点の番号
  priority_queue<P,vector<P>,greater<P>>pq;
  dist[s]=0;
  pq.emplace(dist[s],s);

  while(!pq.empty()){
    auto x=pq.top();pq.pop();
    int v=x.second;
    if(dist[v]<x.first)continue;//最短距離でなければ飛ばす

    for(auto e:g[v]){
      ll stop=max(0LL,ll(sqrt(e.d)-1));
      if(chmin(dist[e.to],dist[v]+e.c+e.d/(dist[v]+1))){
        pq.emplace(dist[e.to],e.to);
      }
      rep(i,2){
        if(dist[v]<stop&&chmin(dist[e.to],stop+e.c+e.d/(stop+1))){
          pq.emplace(dist[e.to],e.to);
        }
        stop++;
      }
    }
  }
  return dist;
}



void solve()
{
  int n,m;
  cin>>n>>m;
  vector<vector<edge<ll>>>g(n);
  rep(i,m){
    int a,b;
    ll c,d;
    cin>>a>>b>>c>>d;
    a--;b--;
    g[a].eb(b,c,d);
    g[b].eb(a,c,d);
  }
  auto d=dijkstra<ll>(g,0);
  if(d[n-1]==numeric_limits<ll>::max()){
    cout<<-1;
  }else{
    cout<<d[n-1]<<"\n";
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}