#pragma GCC optimize("O3,inline")
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct bra {
int i = -1, c = 1e16, dir = 0;
bra() {}
bra(int _i, int _c, int _dir) : i(_i), c(_c), dir(_dir) {}
bool operator<(const bra other) const {
return c < other.c;
}
};
struct ket {
bra l, r;
int c = 1e16;
ket() {}
ket(bra _l, bra _r) : l(_l), r(_r), c(_l.c + _r.c) {}
bool operator<(const ket other) const {
return c < other.c;
}
};
vector<bra> guys;
struct node {
int s, e, m, mf, lazy, edge;
ket c1, c2, c3;
bra bl, br, fl, fr;
node* l, * r;
void push() {
if (s == e || lazy == 0) return;
l->mf += lazy;
r->mf += lazy;
l->edge += lazy;
r->edge += lazy;
l->lazy += lazy;
r->lazy += lazy;
lazy = 0;
}
void pull() {
mf = min(min(l->mf, r->mf), edge);
bl = min(l->bl, r->bl);
br = min(l->br, r->br);
fl = (mf == l->mf ? l->fl : l->bl), fr = (mf == r->mf ? r->fr : r->br);
if (mf != edge && mf != l->mf) fl = min(fl, (mf == r->mf ? r->fl : r->bl));
if (mf != edge && mf != r->mf) fr = min(fr, (mf == l->mf ? l->fr : l->br));
c1 = min(min(l->c1, r->c1), ket(l->bl, r->br));
c2 = min(min(l->c2, r->c2), ket(l->br, r->bl));
c3 = min((mf == l->mf ? l->c3 : l->c2), (mf == r->mf ? r->c3 : r->c2));
if (mf != edge) c3 = min(c3, ket((mf == l->mf ? l->fr : l->br), (mf == r->mf ? r->fl : r->bl)));
}
node(int _s, int _e) : s(_s), e(_e), m((_s + _e) / 2), mf(0), lazy(0), edge(0) {
if (s > e) return;
if (s == e) {
if (guys[s].dir == 0) fl = bl = guys[s];
else fr = br = guys[s];
mf = edge = 1e16;
return;
}
l = new node(s, m), r = new node(m + 1, e);
pull();
}
void upd(int a, int b, int x) {
if (b < s || a > e) return;
if (a <= s && b >= e) {
mf += x;
edge += x;
lazy += x;
return;
}
push();
if (a <= m && b > m) edge += x;
l->upd(a, b, x);
r->upd(a, b, x);
pull();
}
void use(int n) {
if (s == e) {
bl = br = fl = fr = bra();
c1 = c2 = c3 = ket();
return;
}
push();
if (n <= m) l->use(n);
else r->use(n);
pull();
}
ket get() {
if (s > e) return ket();
ket res; int delta = -1;
if (mf > 0 && c2 < c1) res = c2;
else if (mf == 0 && c3 < c1) res = c3;
else {
res = c1;
delta = 1;
}
if (res.c >= 1e15) return res;
int idx1 = res.l.i, idx2 = res.r.i;
upd(min(idx1, idx2), max(idx1, idx2), delta);
use(idx1); use(idx2);
return res;
}
} *root;
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N, K, a, b, c, d, ans = 1e15; cin >> N >> K;
vector<pair<pair<int, int>, int>> xb, yb;
for (int i = 0; i < N; i++) {
cin >> a >> b >> c >> d;
if (a == 2) b--;
if (a == 4) c--;
if (a == 1 || a == 2) xb.push_back({{b, d}, a == 1});
else yb.push_back({{c, d}, a == 3});
}
sort(xb.begin(), xb.end());
sort(yb.begin(), yb.end());
vector<int> v1 = {0}, v2 = {0};
for (int i = 0; i < xb.size(); i++) guys.push_back(bra(i, xb[i].first.second, xb[i].second));
root = new node(0, guys.size() - 1);
int ccost = 0;
ket crnt;
while ((crnt = root->get()).c < 1e15) {
ccost += crnt.c;
v1.push_back(ccost);
}
guys.clear();
for (int i = 0; i < yb.size(); i++) guys.push_back(bra(i, yb[i].first.second, yb[i].second));
root = new node(0, guys.size() - 1);
ccost = 0;
while ((crnt = root->get()).c < 1e15) {
ccost += crnt.c;
v2.push_back(ccost);
}
for (int i = 0; i < v1.size(); i++) {
if (K - i < 0 || K - i >= v2.size()) continue;
ans = min(ans, v1[i] + v2[K - i]);
}
cout << (ans == 1e15 ? -1 : ans) << '\n';
}