#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
typedef long long ll;
template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const vector<T>&v){stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,const vector<T>&v){if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.first>>v.second;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.first<<","<<v.second;}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>ll suma(const vector<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;}

#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) { cerr << head; dump(); }
template <typename Head, typename... Tail> void dump(Head &&head, Tail &&... tail) { cerr << head << ", "; dump(forward<Tail>(tail)...); }
#define debug(...) do { cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; dump(__VA_ARGS__); } while (false)
#else
#define dump(...)
#define debug(...)
#endif

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;

const ll LINF = 1LL << 60;
const int INF = 1001001001;

/////////////////////////////////////////////////////////////////////

int main()
{
  int n,k; cin>>n>>k;
  vector<vector<int>> t(n, vector<int>(n));
  rep(i, n) rep(j, n) cin>>t[i][j];

  vector<int> a(n-1);
  rep(i, n-1) a[i] = i+1;

  int ans = 0;
  do {
    int sum = t[0][a[0]] + t[a[n-2]][0];
    rep(i, n-2) {
      sum += t[a[i]][a[i+1]];
    }
    if (sum == k) ans++;
  } while (next_permutation(a.begin(), a.end()));
  cout << ans << "\n";

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using iPair = pair<int,int>;
using lPair = pair<ll, ll>;
using ivector = vector<int>;
using lvector = vector<ll>;
using istack = stack<int>;
using iqueue = queue<int>;
using ivv = vector<vector<int>>;
using lvv = vector<vector<ll>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
vector<iPair> dir = {{1,0}, {-1,0}, {0,1}, {0,-1}};
#define dump(x) cout << #x << " = " << (x) << endl
#define ALL(x) begin(x),end(x)
#define rep(i,s,e) for(ll i=(s), i_stop=(e); i<i_stop; ++i)
#define repRev(i,s,e) for(ll i=(e)-1, i_stop=(s); i>=i_stop; --i)
#define range(i,s,n) for(ll i=(s), i_stop=(s)+(n); i<i_stop; ++i)
#define rangeRev(i,s,n) for(ll i=(s), i_stop=(s)-(n); i>i_stop; --i)
#define foreach(x,container) for(auto &&x:container)
template<typename T> bool chmax(T& a, const T b) {if(a<b) {a=b;return true;} return false;}
template<typename T> bool chmin(T& a, const T b) {if(a>b) {a=b;return true;} return false;}
template<typename T> void printArr(vector<T> &arr){
    for(auto &x:arr) {cout << x << " ";} cout << endl;
}


/*


## 關鍵詞：

## 題意：


## 思路：

## 雜多：


*/

void solve() {
    int n,m; cin>>n>>m;
    vector<vector<tuple<int,ll,ll>>> edge(n+1);

    range(i, 1, m) {
        int a,b; ll c,d; cin>>a>>b>>c>>d;
        edge[a].emplace_back(b,c,d);
        edge[b].emplace_back(a,c,d);
    }

    using P = pair<ll,int>;
    priority_queue<P, vector<P>, greater<P>> pq;
    lvector dist(n+1, LINF);
    dist[1] = 0; pq.emplace(0, 1);
    while(pq.size()) {
        auto [t, u] = pq.top(); pq.pop();
        if(dist[u] < t) continue;

        if(u == n) {
            cout << t << endl;
            return;
        }

        // 到達頂點u所需的最短時刻爲t
        for(auto [v,c,d]: edge[u]) {
            // 對每個鄰點v求最短時刻v_t
            // time of u->v:
            // = t + c + floor(d/t+1)
            // = floor(t + c + d/(t+1))
            // = floor(t + 1 + d/(t+1) + (c-1))
            // 所以答案大致在滿足 (t+1)^2 = d 的t附近
            double _x = sqrt((double)d); _x -= 1;
            int x = (int)_x;
            int bestx = 0; ll ans = LINF;
            for(int i = max(0, x-2); i <= x+2; ++i) {
                if(chmin(ans, i+c+d/(i+1))) bestx = i;
            }

            // 當 t = bestx 的時候這條路的經過時間取到最小值
            if(t >= bestx) { // 立即出發
                if(chmin(dist[v], t+c+d/(t+1)))
                    pq.emplace(dist[v], v);
            }else{ // 待機到bestx
                if(chmin(dist[v], bestx+c+d/(bestx+1)))
                    pq.emplace(dist[v], v);
            }
        }
    }

    cout << "-1" << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
    return 0;
}