#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#define nmax_def 110000
#define Cmax_def 2200
#define mod 1000000007
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define graph_from1 true
#define graph_tree 1
#define graph_weight false
#define graph_digraph false

class Graph_func
{
public:
	int Vnum;
	int Enum;
	vector<pair<int, int> > edgelist;
	vector<ll> edgeweight;
	vector<vector<int> > adjlist;
	vector<vector<ll> > adjlistweight;
	vector<int> adjnum;

	vector<ll> xdist;

	vi anslist;

	vector<bool> x_visited;
	vector<int> x_prqarr;
	vector<bool> x_filled;

	void read_edge();
	void adjini();
	void visited_ini();
	void adjsort();
	vector<bool> graph_num_BFS(int BFS_ini, vector<bool> filled);
	int graph_num();
	void graph_BFS(ll BFS_ini);
	void graph_DFS(ll DFS_ini);
	void xydist_tree(int x);   // ex. xydist_tree(3)
	void xydist(int x);   // ex. xydist(3, 0)
	ll diameter();
	int dim_max();

	bool xydist_def = false;
};

void Graph_func::read_edge()
{
	cin >> Vnum;
	if (graph_tree)
		Enum = Vnum - 1;// graph
	else
		cin >> Enum;
	edgelist.resize(Enum);
	edgeweight.resize(Enum);
	adjnum.resize(Vnum);
	xdist.resize(Vnum);
	adjlist.resize(Vnum);
	adjlistweight.resize(Vnum);
	anslist.resize(Enum);
	for (int i = 0; i < Enum; i++)
	{
		if (graph_weight)
			cin >> edgelist[i].first >> edgelist[i].second >> edgeweight[i];
		else
			cin >> edgelist[i].first >> edgelist[i].second;
		edgeweight[i] = i;
		if (graph_from1)
		{
			edgelist[i].first--;
			edgelist[i].second--;
		}
	}
	return;
}

void Graph_func::adjini()
{
	for (int i = 0; i < Vnum; i++)
		adjnum[i] = 0;
	for (int i = 0; i < Enum; i++)
	{
		int x = edgelist[i].first;
		int y = edgelist[i].second;
		adjlist[x].push_back(y);
		if (!graph_digraph)
			adjlist[y].push_back(x);
		ll xyweight = edgeweight[i];
		adjlistweight[x].push_back(xyweight);
		if (!graph_digraph)
			adjlistweight[y].push_back(xyweight);
		adjnum[x]++;
		if (!graph_digraph)
			adjnum[y]++;
	}
	x_visited.resize(Vnum);
	visited_ini();

	return;
}

void Graph_func::visited_ini()
{
	for (int i = 0; i < Vnum; i++)
		x_visited[i] = false;
	x_prqarr.resize(0);
}

void Graph_func::graph_BFS(ll BFS_ini)
{
	visited_ini();
	x_prqarr.push_back(BFS_ini);
	x_visited[BFS_ini] = true;
	vi ansprev(Vnum, -1);
	while (!x_prqarr.empty())
	{
		int Ntmp = x_prqarr.size();
		for (int a = 0; a < Ntmp; a++)
		{
			int x = x_prqarr[0];
			x_prqarr.erase(x_prqarr.begin());
			int count = 0;
			for (int i = 0; i < adjnum[x]; i++)
			{
				int x_new = adjlist[x][i];
				if (!x_visited[x_new])
				{
					count++;
					if (ansprev[x] == count)
						count++;
					x_prqarr.push_back(x_new);
					x_visited[x_new] = true;
					int etmp = adjlistweight[x][i];
					anslist[etmp] = count;
					ansprev[x_new] = count;
				}
			}
		}
	}
	return;
}

int Graph_func::dim_max()
{
	int anstmp = 0;
	for (int i = 0; i < Vnum; i++)
		anstmp = max(anstmp, adjnum[i]);
	return anstmp;
}

int main(void)
{
	Graph_func graph;
	ll ans = 0;
	graph.read_edge();
	graph.adjini();
	ll Vnum = graph.Vnum;
	ll Enum = graph.Enum;
	ans = graph.dim_max();
	graph.graph_BFS(0);

	cout << ans << endl;
	rep(i, Enum) {
		cout << graph.anslist[i] << endl;
	}
	return 0;
}

Measured memory_bytes: 15616