One test case failed! pls explain

#include
#include<math.h>
using namespace std;

int main() {

long long x;
cin>>x;

long long ans=0;

int a=0;
long long orignal=x;
long long temp=x;
int count=0;

while(temp>0){
	temp/=10;
	count++;
}

long long AX=orignal/pow(10,count-1);
int y;
y=AX;


while(x>9){
	int lastdigit=x%10;
	if(lastdigit>9-lastdigit) lastdigit=9-lastdigit;
	ans+=lastdigit*pow(10,a);
	a++;
	x/=10;
}

ans=ans+y*pow(10,count-1);

cout<<ans;

return 0;

}

@anshul3558_e8c9aa486c392ca3, ans for 8 is 1, dont change first digit only when if its not 9 else u can change