#include<bits/stdc++.h>
using namespace std;
int n,k,c=1,max_=-9999999,x,y,head[110000],L=-9999999,d[110000];
bool flag[210000],flag1;
struct xzh
{
int next,to,w;
}edge[210000];
void add(int u,int v)
{
c++;
edge[c].next=head[u];
edge[c].to=v;
edge[c].w=1;
head[u]=c;
}
void dfs1(int now,int fa,int sum)
{
for(int i=head[now];i;i=edge[i].next)
{
int v=edge[i].to;
if(v!=fa)dfs1(v,now,sum+edge[i].w);
}
if(sum>max_)
{
max_=sum;
x=now;
}
}
void dfs2(int now,int fa,int sum)
{
for(int i=head[now];i;i=edge[i].next)
{
int v=edge[i].to;
if(v!=fa)dfs2(v,now,sum+edge[i].w);
}
if(sum>max_)
{
max_=sum;
y=now;
}
}
void dfss(int now,int fa)
{
if(flag1)return;
for(int i=head[now];i;i=edge[i].next)
{
if(flag1)return ;
int v=edge[i].to;
if(v!=fa)
{
edge[i].w=-1;
edge[i^1].w=-1;
if(v==y)
{
flag1=true;
return;
}
dfss(v,now);
if(flag1)return;
edge[i].w=1;
edge[i^1].w=1;
}
}
}
void dp(int now,int fa)
{
for(int i=head[now];i;i=edge[i].next)
{
int v=edge[i].to;
if(v!=fa)
{
dp(v,now);
L=max(L,d[x]+d[v]+edge[i].w);
d[x]=max(d[x],d[v]+edge[i].w);
}
}
}
int main()
{
cin>>n>>k;
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
add(u,v);
add(v,u);
}
dfs1(1,0,0);
max_=-9999999;
dfs2(x,0,0);
if(k==1)
{
cout<<2*(n-1)-(max_-1);
return 0;
}
dfss(x,0);
dp(1,0);
cout<<2*n-max_-L;
return 0;
}