#include<algorithm>
using namespace std;
#include<vector>
#define pb push_back
#define mp make_pair
#define poz first.first
#define x first.second.first
#define y first.second.second
#define c1 second.first
#define c2 second.second
#define DIM 200005
vector <pair <pair<int,pair<int,int> >,pair<int,int> > > lst;
int n,m,t[DIM],k;
struct cmp
{
bool operator ()(const pair <pair<int,pair<int,int> >,pair<int,int> > &a,const pair <pair<int,pair<int,int> >,pair<int,int> > &b) const
{
return a.c1<b.c1 || (a.c1==b.c1 && a.c2>=b.c2);
}
};
inline int find (int i)
{
for(;t[i]!=0;i=t[i]);
return i;
}
int main ()
{
freopen("lazy.in","r",stdin);
freopen("lazy.out","w",stdout);
int i,nr1,nr2,nr3,nr4;
scanf("%d%d",&n,&m);
for(i=1;i<=m;++i)
{
scanf("%d%d%d%d",&nr1,&nr2,&nr3,&nr4);
lst.pb (mp(mp(i,mp(nr1,nr2)),mp(nr3,nr4)));
}
sort(lst.begin (),lst.end (),cmp ());
for(i=0;i<(int)lst.size ();++i)
if(find (lst[i].x)!=find (lst[i].y))
{
printf("%d\n",lst[i].poz);
t[lst[i].x]=lst[i].y;
if(++k==n-1)
return 0;
}
//printf("%d %d %d %d %d\n",lst[i].poz,lst[i].x,lst[i].y,lst[i].c1,lst[i].c2);
return 0;
}