Don not copy the "Then solve for m,q " part. @ vv is trying to explain to you what to do.
Part 1: Define n and dist, as below.
n:=4;
dist:=add( (- m*x[i] - q + y[i])^2, i=1..n);
Part 2: Sovle for m and q, with the command below:
diff(dist,m)=0, diff(dist,q)=0;
So, this is what you would copy and paste into your worksheet:
n:=4;
dist:=add( (- m*x[i] - q + y[i])^2, i=1..n);
diff(dist,m)=0, diff(dist,q)=0;
Regards,
Georgios