#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int main(){
int a, b;
while (cin>>a >> b)
{
if (a == 0 && b == 0)
return 0;
int res = 1;
while (b)
{
if (b & 1)
{
res *= a;
res %= 1000;
}
a *= a;
a %= 1000;
b >>= 1;
}
cout << res%1000 << endl;
}
return 0;
}