#include<cstdio>
#include<vector>
#include<complex>
#include<cstring>
#include<algorithm>

#define rep(i,a) for(int i=0;i<a;i++)
#define EQ(a,b) (abs((a)-(b))<EPS)
#define pb push_back
#define fs first
#define sc second
using namespace std;

typedef double D;
typedef complex<D> P;
typedef pair<P,P> L;
typedef vector<P> Poly;

const D EPS = 1e-8;

namespace std{
  bool operator<(const P &a,const P &b){
    return EQ(real(a),real(b))?imag(a)<imag(b):real(a)<real(b);
  }
}

vector<int> g[110];
vector<int> rg[110];
vector<int> vs;
bool use[110];
int ord[110];
int n,d;
Poly tri[110];
Poly rect[110];

P unit(P p){return p/abs(p);}
D dot(P x,P y){return real(conj(x)*y);}
D cross(P x,P y){return imag(conj(x)*y);}

vector<P> convex_hull(vector<P> v){
  int n = v.size(), k = 0;
  sort(v.begin(),v.end());
  vector<P> r(2*n);
  for(int i=0;i<n;i++){
    while(k>1 && cross(r[k-1]-r[k-2],v[i]-r[k-1]) <= EPS)k--;
    r[k++] = v[i];
  }
  for(int i=n-2,t=k;i>=0;i--){
    while(k>t && cross(r[k-1]-r[k-2],v[i]-r[k-1]) <= EPS)k--;
    r[k++] = v[i];
  }
  r.resize(k-1);
  return r;
}

int ccw(P a,P b,P c){
  b -= a; c -= a;
  if(cross(b,c)>EPS)return 1;
  if(cross(b,c)<-EPS)return -1;
  if(dot(b,c)<-EPS)return 2;
  if(abs(b)<abs(c))return -2;
  return 0;
}

bool is_cp(L a,L b){
  if(ccw(a.fs,a.sc,b.fs)*ccw(a.fs,a.sc,b.sc)<=0)
    if(ccw(b.fs,b.sc,a.fs)*ccw(b.fs,b.sc,a.sc)<=0)return true;
  return false;
}

bool inter_pos(Poly &p,P &x){
  int s = p.size();
  if(!s)return false;
  rep(i,s)if(ccw(p[i],p[(i+1)%s],x)==-1)return false;
  return true;
}

bool cross_poly(Poly &a, Poly &b){
  int n1 = a.size(), n2 = b.size();
  rep(i,n2)if(inter_pos(a,b[i]))return true;
  rep(i,n1)if(inter_pos(b,a[i]))return true;
  rep(i,n1)rep(j,n2)
    if(is_cp(L(a[i],a[(i+1)%n1]),L(b[j],b[(j+1)%n2])))return true;
  return false;
}

void fdfs(int v){
  use[v] = true;
  for(int i=0;i<g[v].size();i++){
    if(!use[g[v][i]])fdfs(g[v][i]);
  }
  vs.pb(v);
}

void rdfs(int v,int k){
  use[v] = true;
  ord[v] = k;
  for(int i=0;i<rg[v].size();i++){
    if(!use[rg[v][i]])rdfs(rg[v][i],k);
  }
}

int scc(){
  memset(use,0,sizeof(use));
  vs.clear();
  for(int i=0;i<n;i++){
    if(!use[i])fdfs(i);
  }

  memset(use,0,sizeof(use));
  int k = 0;
  for(int i=vs.size()-1;i>=0;i--){
    if(!use[vs[i]])rdfs(vs[i],k++);
  }
  return k;
}

int main(){
  while(scanf("%d%d",&n,&d), n+d){
    rep(i,n){
      tri[i].resize(3);
      scanf("%lf%lf",&tri[i][0].real(),&tri[i][0].imag());
      scanf("%lf%lf",&tri[i][1].real(),&tri[i][1].imag());
      scanf("%lf%lf",&tri[i][2].real(),&tri[i][2].imag());
      tri[i] = convex_hull(tri[i]);

      rect[i].clear();
      rep(j,3){
	int n1 = (j+1)%3, n2 = (j+2)%3;
	if(EQ(abs(tri[i][n1]-tri[i][j]),abs(tri[i][n2]-tri[i][j]))){
	  rect[i].pb(tri[i][n1]); rect[i].pb(tri[i][n2]); break;
	}
      }

      P ver = (D)d*unit((rect[i][0]-rect[i][1])*P(0,-1));
      rect[i].pb(ver+rect[i][1]); rect[i].pb(ver+rect[i][0]);
      rect[i] = convex_hull(rect[i]);
    }

    rep(i,n){g[i].clear(); rg[i].clear();}
    rep(i,n){
      rep(j,n){
	if(i==j)continue;
	if(cross_poly(rect[i],tri[j])){g[i].pb(j); rg[j].pb(i);}
      }
    }


    int cmp = scc();

    int in[110] = {0};
    rep(i,n){
      rep(j,g[i].size())if(ord[g[i][j]] != ord[i])in[ord[g[i][j]]]++;
    }
  
    int res = 0;
    rep(i,cmp)if(!in[i])res++;
    printf("%d\n",res);
      
  }
}

Measured memory_bytes: 1148