#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e5+10;
const int P=17;
const int inf=1e18;
int dist[P][P],dp[N];
vector<pair<int,int>> ma[N];
bool vis[N];
char ans1[N];
bool cal[N];
void dfs(int x,char c='B')
{
vis[x]=1;
ans1[x]=c;
if(c=='B')
c='D';
else
c='B';
for(auto [w,y]:ma[x])
{
if(!vis[y])
{
dfs(y,c);
}
}
}
void dfs_(int x){
cal[x]=1;
for(auto [w,y]:ma[x])
{
if(!cal[y])
dfs_(y);
if(ans1[x]!=ans1[y])
{
dp[x]=min(dp[x],w);
}
else{
dp[x]=min(dp[x],dp[y]+w);
}
}
}
signed main()
{
srand(time(0));
int n,m;
cin>>n>>m;
for(int j=0;j<m;j++)
{
int x,y,c;
cin>>x>>y>>c;
ma[x].push_back({c,y});
ma[y].push_back({c,x});
}
if(n>16)
{
exit(-1);
return;
}
for(int i=1;i<=n;i++)
sort(begin(ma[i]),end(ma[i]));
dfs(1);
int ans=0;
for(int v=1;v<=n;v++)
dp[v]=inf;
dfs_(1);
for(int v=1;v<=n;v++)
ans=max(ans,dp[v]);
int val=ans;
string ap;
for(int j=1;j<=n;j++)
ap+=ans1[j];
for(int trap=1;trap<=10;trap++)
{
int x=rand()%n;;
x++;
for(int j=1;j<=n;j++)
{
vis[j]=0;
cal[j]=0;
dp[j]=inf;
}
ans=0;
dfs(x);
dfs_(x);
for(int tp=1;tp<=n;tp++)
ans=max(ans,dp[tp]);
if(ans<val)
{
val=ans;
ap="";
for(int j=1;j<=n;j++)
ap+=ans1[j];
}
}
cout<<val<<endl;
cout<<ap<<endl;
return 0;
}