T148457
#include<cstdio>
using namespace std;
const int N=1e6+7;
char *p1,*p2,buf[N];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read(){
int x=0,f=1;
char ch=nc();
while(ch<48||ch>57){
if(ch=='-') f=-1;
ch=nc();
}
while(ch>=48&&ch<=57) x=x*10+ch-48,ch=nc();
return x*f;
}
int a[N];
int ans=1e6;
int S[N],V[N];
int n,m;
#define min_sum(a,b) (a<b ? a : b)
void dfs(int floor,int r,int h,int s,int v){
int highy=h;
if(!floor){
if(v==n){
ans=min_sum(ans,s);
}
return ;
}
if(s+S[floor-1]>=ans) return ;
if(2*(n-v)/r+s>=ans) return ;
if(v+V[floor-1]>n) return ;
for(int i=r-1;i>=floor;i--){
if(floor==m){
s=i*i;
}
highy=min_sum(h-1,(n-V[floor-1]-v)/i/i);
for(int j=highy;j>=floor;j--){
dfs(floor-1,i,j,s+2*i*j,v+i*i*j);
}
}
}
int main(){
n=read(),m=read();
for(int i=1;i<=m;i++){
S[i]=S[i-1]+2*i*i;
V[i]=V[i-1]+i*i*i;
}
dfs(m,n,n,0,0);
if(ans==1e6) puts("-1");
else printf("%d",ans);
return 0;
}