#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

typedef complex<double> P;
namespace std {
  bool operator < (const P& a, const P& b) {
    // if (abs(a-b)<EPS) return 0;
    return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
  }
}
double cross(const P& a, const P& b) {
  return imag(conj(a)*b);
}
double dot(const P& a, const P& b) {
  return real(conj(a)*b);
}
struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a); push_back(b);
  }
  L() {}
};
typedef vector<P> G;
#define curr(P, i) P[i]
#define next(P, i) P[(i+1)%P.size()]
struct C {
  P p; double r;
  C(const P &p, double r) : p(p), r(r) { }
};

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

bool intersectLL(const L &l, const L &m) {
  return abs(cross(l[1]-l[0], m[1]-m[0])) > EPS || // non-parallel
         abs(cross(l[1]-l[0], m[0]-l[0])) < EPS;   // same line
}
bool intersectLS(const L &l, const L &s) {
  return cross(l[1]-l[0], s[0]-l[0])*       // s[0] is left of l
         cross(l[1]-l[0], s[1]-l[0]) < EPS; // s[1] is right of l
}
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 intersectSS2(const L &s, const L &t) { // 接しているやつは交差と考えない
  REP(i, 2) {
    if (ccw(s[0], s[1], t[i]) == 0) {
      int c = ccw(s[0],s[1],t[!i]);
      if (s[0] == t[i]) {
        if (c!=-2&&c) return 0;
      } else if (s[1] == t[i]) {
        if (c!=2&&c) return 0;
      } else if (abs(c)==1) return 0;
    }
  }
  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 intersectSP(const L &s, const P &p) {
  return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}
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]);
}
P reflection(const L &l, const P &p) {
  return p + P(2,0) * (projection(l, p) - p);
}
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));
}
bool convex_contain(const G &g, const P &p) { // 半時計回りを仮定
  REP(i,g.size())
    if (ccw(g[i], next(g, i), p) == -1 && distanceSP(L(g[i],next(g,i)), p) > 0.01) return 0;
  return 1;
}

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])));
}

bool intersectGG(const G &g1, const G &g2) {
  if (convex_contain(g1, g2[0])) return 1;
  if (convex_contain(g2, g1[0])) return 1;
  REP(i,g1.size()) REP(j,g2.size()) {
    if (distanceSS(L(g1[i], next(g1, i)), L(g2[j], next(g2, j))) <= 0.01) return 1;
  }
  return 0;
}

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) :
    src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

void visit(const Graph &g, int v, vector< vector<int> >& scc,
    stack<int> &S, vector<bool> &inS,
    vector<int> &low, vector<int> &num, int& time) {
  low[v] = num[v] = ++time;
  S.push(v); inS[v] = true;
  FOR(e, g[v]) {
    int w = e->dst;
    if (num[w] == 0) {
      visit(g, w, scc, S, inS, low, num, time);
      low[v] = min(low[v], low[w]);
    } else if (inS[w])
      low[v] = min(low[v], num[w]);
  }
  if (low[v] == num[v]) {
    scc.push_back(vector<int>());
    while (1) {
      int w = S.top(); S.pop(); inS[w] = false;
      scc.back().push_back(w);
      if (v == w) break;
    }
  }
}
void stronglyConnectedComponents(const Graph& g,
    vector< vector<int> >& scc) {
  const int n = g.size();
  vector<int> num(n), low(n);
  stack<int> S;
  vector<bool> inS(n);
  int time = 0;
  REP(u, n) if (num[u] == 0)
    visit(g, u, scc, S, inS, low, num, time);
}

// 強連結成分を縮約する
Graph edgeContractionAndSort(const Graph &g) {
  int n = g.size();
  vector<vector<int> > scc;
  stronglyConnectedComponents(g, scc);
  int m = scc.size();
  vector<int> cmp(n);
  REP(i,m) FOR(jt, scc[i])
    cmp[*jt] = i;
  map<pii, bool> mp;
  Graph res(m);
  REP(i,n) {
    int a = cmp[i];
    FOR(it,g[i]) {
      int b = cmp[it->dst];
      if (a != b) {
        if (mp.count(pii(a,b)) == 0) {
          mp[pii(a,b)] = 1;
          res[a].push_back(Edge(a,b,1));
        }
      }
    }
  }
  return res;
}


G triangle[100];
int deg[100];

int main() {
  int n, d;
  while(cin>>n>>d,n||d) {
    REP(i,n) {
      triangle[i].clear();
      REP(j,3) {
        P p; cin >> p.real() >> p.imag();
        triangle[i].push_back(p);
      }
      if (abs(abs(triangle[i][0]-triangle[i][1]) - abs(triangle[i][0]-triangle[i][2])) < EPS) {
        swap(triangle[i][0], triangle[i][2]);
      }
      if (abs(abs(triangle[i][1]-triangle[i][0]) - abs(triangle[i][1]-triangle[i][2])) < EPS) {
        swap(triangle[i][1], triangle[i][2]);
      }
      if (cross(triangle[i][1]-triangle[i][0], triangle[i][2]-triangle[i][0]) < 0) {
        swap(triangle[i][0], triangle[i][1]);
      }
      // REP(j,3) cout << triangle[i][j] << " ";
      // cout << endl;
    }
    Graph g(n);
    REP(i,n) {
      G rect;
      P p = triangle[i][1] - triangle[i][0];
      p /= abs(p);
      p *= P(0,1);
      p *= d;
      rect.push_back(triangle[i][0]);
      rect.push_back(triangle[i][1]);
      rect.push_back(triangle[i][1] + p);
      rect.push_back(triangle[i][0] + p);
      REP(j,n) {
        if (i == j) continue;
        if (intersectGG(rect, triangle[j])) {
          //cout << i << " " << j << endl;
          g[i].push_back(Edge(i,j,0));
        }
      }
    }
    Graph g2 = edgeContractionAndSort(g);
    memset(deg,0,sizeof(deg));
    FOR(it, g2) {
      FOR(e, *it) {
        deg[e->dst]++;
      }
    }
    int ans = 0;
    REP(i,g2.size()) {
      if (deg[i] == 0) ans++;
    }
    cout << ans << endl;
  }
}

Measured memory_bytes: 1304